More tests, better errors.

This commit is contained in:
jregan
2019-11-26 18:13:15 -08:00
parent a2b84fce86
commit f3e735153f
22 changed files with 112 additions and 59 deletions

View File

@@ -27,7 +27,7 @@ func (fi fileInfo) Mode() os.FileMode { return 0777 }
// ModTime returns a bogus time
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
// IsADir returns if it is a directory
// IsDir returns true if it is a directory
func (fi fileInfo) IsDir() bool { return fi.dir }
// Sys should return underlying data source, but it now returns nil

View File

@@ -11,7 +11,8 @@ import (
const (
Separator = string(filepath.Separator)
doubleSep = Separator + Separator
DotDir = "."
SelfDir = "."
ParentDir = ".."
)
// FileSystem groups basic os filesystem methods.
@@ -41,7 +42,8 @@ type FileSystem interface {
Glob(pattern string) ([]string, error)
// ReadFile returns the contents of the file at the given path.
ReadFile(path string) ([]byte, error)
// WriteFile writes the data to a file at the given path.
// WriteFile writes the data to a file at the given path,
// overwriting anything that's already there.
WriteFile(path string, data []byte) error
// Walk walks the file system with the given WalkFunc.
Walk(path string, walkFn filepath.WalkFunc) error

View File

@@ -120,7 +120,7 @@ func (fs *fsInMemory) ReadFile(name string) ([]byte, error) {
if ff, found := fs.m[name]; found {
return ff.content, nil
}
return nil, fmt.Errorf("cannot read file %q", name)
return nil, fmt.Errorf("'%s' doesn't exist", name)
}
// WriteFile always succeeds and does nothing.

View File

@@ -19,3 +19,12 @@ func StripTrailingSeps(s string) string {
}
return s[:k]
}
// StripLeadingSeps trims leading filepath separators from input.
func StripLeadingSeps(s string) string {
k := 0
for k < len(s) && s[k] == filepath.Separator {
k++
}
return s[k:]
}

View File

@@ -84,9 +84,9 @@ func TestFilePathSplit(t *testing.T) {
file: "",
},
{
full: DotDir,
full: SelfDir,
dir: "",
file: DotDir,
file: SelfDir,
},
{
full: "rabbit.jpg",
@@ -163,3 +163,43 @@ func TestStripTrailingSeps(t *testing.T) {
}
}
}
func TestStripLeadingSeps(t *testing.T) {
cases := []struct {
full string
rem string
}{
{
full: "foo",
rem: "foo",
},
{
full: "",
rem: "",
},
{
full: "/foo",
rem: "foo",
},
{
full: "///foo///bar///",
rem: "foo///bar///",
},
{
full: "/////",
rem: "",
},
{
full: "/",
rem: "",
},
}
for _, p := range cases {
dir := StripLeadingSeps(p.full)
if dir != p.rem {
t.Fatalf(
"in '%s', got dir='%s' (expected '%s')",
p.full, dir, p.rem)
}
}
}