fs/fs_test.go

80 lines
1.8 KiB
Go
Raw Normal View History

2015-08-09 16:20:47 -07:00
package fs
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestExists(t *testing.T) {
tmp, err := ioutil.TempDir("", "fs-test-")
2015-08-09 20:18:41 -07:00
defer func() {
os.RemoveAll(tmp)
}()
2015-08-09 16:20:47 -07:00
if err != nil {
2015-08-09 20:18:41 -07:00
t.Fatalf("couldn't create temp dir: %v", err)
2015-08-09 16:20:47 -07:00
}
filename := filepath.Join(tmp, "foo")
if Exists(filename) {
t.Errorf("shouldn't have been able to find non-existant file: %s", filename)
}
f, err := os.Create(filename)
if err != nil {
t.Errorf("problem opening fresh file (%q): %v", filename, err)
}
f.Close()
if !Exists(filename) {
t.Errorf("failure to find existant file: %s", filename)
}
2015-08-09 20:18:41 -07:00
dirname := filepath.Join(tmp, "somedir")
if err := os.Mkdir(dirname, 0777); err != nil {
t.Errorf("problem creating directory: %v", err)
}
if !Exists(dirname) {
t.Errorf("failure to find existant directory: %s", dirname)
}
}
func TestIsDir(t *testing.T) {
tmp, err := ioutil.TempDir("", "fs-test-")
defer func() {
os.RemoveAll(tmp)
}()
if err != nil {
t.Fatalf("couldn't create temp dir: %v", err)
}
dirname := filepath.Join(tmp, "somedir")
if err := os.Mkdir(dirname, 0777); err != nil {
t.Errorf("problem creating directory: %v", err)
}
if isdir := IsDir(filepath.Join(tmp, "notexist")); isdir {
t.Errorf("failed to correctly interpret non-existant file: got %t, want %t", isdir, false)
}
if !Exists(dirname) {
t.Errorf("failure to find existant directory: %s", dirname)
}
if !IsDir(dirname) {
t.Errorf("failure to detect directory: %s", dirname)
}
filename := filepath.Join(tmp, "foo")
f, err := os.Create(filename)
if err != nil {
t.Errorf("problem opening fresh file (%q): %v", filename, err)
}
f.Close()
if isdir := IsDir(filename); isdir {
t.Errorf("failure to correctly interpret file as directory; got %t, want %t", isdir, false)
}
2015-08-09 16:20:47 -07:00
}