From eed16afb00af1a419fa647bece67afd13840082c Mon Sep 17 00:00:00 2001 From: jregan Date: Thu, 22 Nov 2018 06:50:43 -0800 Subject: [PATCH] Add removeAll to fakeFs --- pkg/fs/fakefs.go | 15 +++++++++++++++ pkg/fs/fakefs_test.go | 37 +++++++++++++++++++++++++++++++------ pkg/fs/fs.go | 1 + pkg/fs/realfs.go | 5 +++++ 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/pkg/fs/fakefs.go b/pkg/fs/fakefs.go index e72ee3829..32ef34fa1 100644 --- a/pkg/fs/fakefs.go +++ b/pkg/fs/fakefs.go @@ -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 { diff --git a/pkg/fs/fakefs_test.go b/pkg/fs/fakefs_test.go index 9278d3cb5..d45cd81e7 100644 --- a/pkg/fs/fakefs_test.go +++ b/pkg/fs/fakefs_test.go @@ -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) { diff --git a/pkg/fs/fs.go b/pkg/fs/fs.go index 798fe572a..6d4d6d229 100644 --- a/pkg/fs/fs.go +++ b/pkg/fs/fs.go @@ -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 diff --git a/pkg/fs/realfs.go b/pkg/fs/realfs.go index 82bbb92ef..98a647d52 100644 --- a/pkg/fs/realfs.go +++ b/pkg/fs/realfs.go @@ -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) }