toolbox/fsutil/fsutil_test.go

63 lines
975 B
Go

package fsutil
import (
"io/ioutil"
"path/filepath"
"testing"
)
func TestPathExists(t *testing.T) {
if !PathExists(".") {
t.Fatal(".")
}
if PathExists("./i-dont-exist") {
t.Fatal("2")
}
}
func TestFileExists(t *testing.T) {
if FileExists(".") {
t.Fatal(".")
}
if FileExists("./i-dont-exist") {
t.Fatal("2")
}
if !FileExists("fsutil.go") {
t.Fatal("fsutil.go")
}
}
func TestDirExists(t *testing.T) {
if !DirExists(".") {
t.Fatal(".")
}
if DirExists("./i-dont-exist") {
t.Fatal("2")
}
if DirExists("fsutil.go") {
t.Fatal("fsutil.go")
}
}
func TestWriteFileAtomic(t *testing.T) {
dir, err := ioutil.TempDir("", "123")
if err != nil {
t.Fatal(err)
}
path := filepath.Join(dir, "some-file.dat")
data := []byte("Hello world!")
if err := WriteFileAtomic(data, path+".new", path); err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if string(b) != "Hello world!" {
t.Fatal(b)
}
}