Add removeAll to fakeFs

This commit is contained in:
jregan
2018-11-22 06:50:43 -08:00
parent 621ed52bab
commit eed16afb00
4 changed files with 52 additions and 6 deletions

View File

@@ -77,6 +77,21 @@ func (fs *fakeFs) MkdirAll(name string) error {
return fs.Mkdir(name)
}
// RemoveAll presumably does rm -r on a path.
// There's no error.
func (fs *fakeFs) RemoveAll(name string) error {
var toRemove []string
for k := range fs.m {
if strings.HasPrefix(k, name) {
toRemove = append(toRemove, k)
}
}
for _, k := range toRemove {
delete(fs.m, k)
}
return nil
}
// Open returns a fake file in the open state.
func (fs *fakeFs) Open(name string) (File, error) {
if _, found := fs.m[name]; !found {

View File

@@ -40,14 +40,41 @@ func TestIsDir(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !x.Exists(expectedName) {
t.Fatalf(expectedName + " should exist")
}
shouldExist(t, x, expectedName)
if !x.IsDir(expectedName) {
t.Fatalf(expectedName + " should be a dir")
}
}
func shouldExist(t *testing.T, fs FileSystem, name string) {
if !fs.Exists(name) {
t.Fatalf(name + " should exist")
}
}
func shouldNotExist(t *testing.T, fs FileSystem, name string) {
if fs.Exists(name) {
t.Fatalf(name + " should not exist")
}
}
func TestRemoveAll(t *testing.T) {
x := MakeFakeFS()
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
x.WriteFile("/foo/project/subdir/file.yaml", []byte("Unused"))
x.WriteFile("/foo/apple/subdir/file.yaml", []byte("Unused"))
shouldExist(t, x, "/foo/project/file.yaml")
shouldExist(t, x, "/foo/project/subdir/file.yaml")
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
err := x.RemoveAll("/foo/project")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
shouldNotExist(t, x, "/foo/project/file.yaml")
shouldNotExist(t, x, "/foo/project/subdir/file.yaml")
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
}
func TestIsDirDeeper(t *testing.T) {
x := MakeFakeFS()
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
@@ -78,9 +105,7 @@ func TestCreate(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error")
}
if !x.Exists("foo") {
t.Fatalf("expected foo to exist")
}
shouldExist(t, x, "foo")
}
func TestReadFile(t *testing.T) {

View File

@@ -27,6 +27,7 @@ type FileSystem interface {
Create(name string) (File, error)
Mkdir(name string) error
MkdirAll(name string) error
RemoveAll(name string) error
Open(name string) (File, error)
IsDir(name string) bool
Exists(name string) bool

View File

@@ -45,6 +45,11 @@ func (realFS) MkdirAll(name string) error {
return os.MkdirAll(name, 0777|os.ModeDir)
}
// RemoveAll delegates to os.RemoveAll.
func (realFS) RemoveAll(name string) error {
return os.RemoveAll(name)
}
// Open delegates to os.Open.
func (realFS) Open(name string) (File, error) { return os.Open(name) }