Added IsDir and more tests
This commit is contained in:
parent
d65b52fedb
commit
ec32af71be
15
fs.go
15
fs.go
@ -4,10 +4,19 @@ import "os"
|
||||
|
||||
// Exists returns if a file exists
|
||||
func Exists(path string) bool {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return false
|
||||
} else if os.IsNotExist(err) {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func IsDir(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if info.IsDir() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
50
fs_test.go
50
fs_test.go
@ -9,8 +9,11 @@ import (
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
tmp, err := ioutil.TempDir("", "fs-test-")
|
||||
defer func() {
|
||||
os.RemoveAll(tmp)
|
||||
}()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
t.Fatalf("couldn't create temp dir: %v", err)
|
||||
}
|
||||
|
||||
filename := filepath.Join(tmp, "foo")
|
||||
@ -28,4 +31,49 @@ func TestExists(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user