You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

79 lines
1.8 KiB

package fs
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestExists(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)
}
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)
}
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)
}
}