Skip to content

Commit eed16af

Browse files
committed
Add removeAll to fakeFs
1 parent 621ed52 commit eed16af

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

pkg/fs/fakefs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ func (fs *fakeFs) MkdirAll(name string) error {
7777
return fs.Mkdir(name)
7878
}
7979

80+
// RemoveAll presumably does rm -r on a path.
81+
// There's no error.
82+
func (fs *fakeFs) RemoveAll(name string) error {
83+
var toRemove []string
84+
for k := range fs.m {
85+
if strings.HasPrefix(k, name) {
86+
toRemove = append(toRemove, k)
87+
}
88+
}
89+
for _, k := range toRemove {
90+
delete(fs.m, k)
91+
}
92+
return nil
93+
}
94+
8095
// Open returns a fake file in the open state.
8196
func (fs *fakeFs) Open(name string) (File, error) {
8297
if _, found := fs.m[name]; !found {

pkg/fs/fakefs_test.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,41 @@ func TestIsDir(t *testing.T) {
4040
if err != nil {
4141
t.Fatalf("unexpected error: %v", err)
4242
}
43-
if !x.Exists(expectedName) {
44-
t.Fatalf(expectedName + " should exist")
45-
}
43+
shouldExist(t, x, expectedName)
4644
if !x.IsDir(expectedName) {
4745
t.Fatalf(expectedName + " should be a dir")
4846
}
4947
}
5048

49+
func shouldExist(t *testing.T, fs FileSystem, name string) {
50+
if !fs.Exists(name) {
51+
t.Fatalf(name + " should exist")
52+
}
53+
}
54+
55+
func shouldNotExist(t *testing.T, fs FileSystem, name string) {
56+
if fs.Exists(name) {
57+
t.Fatalf(name + " should not exist")
58+
}
59+
}
60+
61+
func TestRemoveAll(t *testing.T) {
62+
x := MakeFakeFS()
63+
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
64+
x.WriteFile("/foo/project/subdir/file.yaml", []byte("Unused"))
65+
x.WriteFile("/foo/apple/subdir/file.yaml", []byte("Unused"))
66+
shouldExist(t, x, "/foo/project/file.yaml")
67+
shouldExist(t, x, "/foo/project/subdir/file.yaml")
68+
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
69+
err := x.RemoveAll("/foo/project")
70+
if err != nil {
71+
t.Fatalf("Unexpected error: %v", err)
72+
}
73+
shouldNotExist(t, x, "/foo/project/file.yaml")
74+
shouldNotExist(t, x, "/foo/project/subdir/file.yaml")
75+
shouldExist(t, x, "/foo/apple/subdir/file.yaml")
76+
}
77+
5178
func TestIsDirDeeper(t *testing.T) {
5279
x := MakeFakeFS()
5380
x.WriteFile("/foo/project/file.yaml", []byte("Unused"))
@@ -78,9 +105,7 @@ func TestCreate(t *testing.T) {
78105
if err != nil {
79106
t.Fatalf("unexpected error")
80107
}
81-
if !x.Exists("foo") {
82-
t.Fatalf("expected foo to exist")
83-
}
108+
shouldExist(t, x, "foo")
84109
}
85110

86111
func TestReadFile(t *testing.T) {

pkg/fs/fs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type FileSystem interface {
2727
Create(name string) (File, error)
2828
Mkdir(name string) error
2929
MkdirAll(name string) error
30+
RemoveAll(name string) error
3031
Open(name string) (File, error)
3132
IsDir(name string) bool
3233
Exists(name string) bool

pkg/fs/realfs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func (realFS) MkdirAll(name string) error {
4545
return os.MkdirAll(name, 0777|os.ModeDir)
4646
}
4747

48+
// RemoveAll delegates to os.RemoveAll.
49+
func (realFS) RemoveAll(name string) error {
50+
return os.RemoveAll(name)
51+
}
52+
4853
// Open delegates to os.Open.
4954
func (realFS) Open(name string) (File, error) { return os.Open(name) }
5055

0 commit comments

Comments
 (0)