Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit cad256e

Browse files
smolamcuadros
authored andcommitted
utils/fs: add Remove(). (#94)
1 parent d45eb04 commit cad256e

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

utils/fs/fs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Filesystem interface {
2222
ReadDir(path string) ([]FileInfo, error)
2323
TempFile(dir, prefix string) (File, error)
2424
Rename(from, to string) error
25+
Remove(filename string) error
2526
Join(elem ...string) string
2627
Dir(path string) Filesystem
2728
Base() string

utils/fs/os/os.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ func (fs *OS) Stat(filename string) (FileInfo, error) {
100100
return os.Stat(fullpath)
101101
}
102102

103+
func (fs *OS) Remove(filename string) error {
104+
fullpath := fs.Join(fs.base, filename)
105+
return os.Remove(fullpath)
106+
}
107+
103108
func (fs *OS) TempFile(dir, prefix string) (File, error) {
104109
fullpath := fs.Join(fs.base, dir)
105110
if err := fs.createDir(fullpath + string(os.PathSeparator)); err != nil {

utils/fs/test/fs_suite.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,28 @@ func (s *FilesystemSuite) TestOpenAndStat(c *C) {
149149
c.Assert(stat.Name(), Equals, "foo")
150150
}
151151

152+
func (s *FilesystemSuite) TestRemove(c *C) {
153+
f, err := s.Fs.Create("foo")
154+
c.Assert(err, IsNil)
155+
c.Assert(f.Close(), IsNil)
156+
157+
err = s.Fs.Remove("foo")
158+
c.Assert(err, IsNil)
159+
}
160+
161+
func (s *FilesystemSuite) TestRemoveNonExisting(c *C) {
162+
c.Assert(s.Fs.Remove("NON-EXISTING"), NotNil)
163+
}
164+
165+
func (s *FilesystemSuite) TestRemoveTempFile(c *C) {
166+
f, err := s.Fs.TempFile("test-dir", "test-prefix")
167+
fn := f.Filename()
168+
c.Assert(err, IsNil)
169+
c.Assert(f.Close(), IsNil)
170+
171+
c.Assert(s.Fs.Remove(fn), IsNil)
172+
}
173+
152174
func (s *FilesystemSuite) TestJoin(c *C) {
153175
c.Assert(s.Fs.Join("foo", "bar"), Equals, "foo/bar")
154176
}

0 commit comments

Comments
 (0)