diff options
author | Santiago M. Mola <santi@mola.io> | 2016-10-31 16:11:07 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-31 15:11:07 +0000 |
commit | 5078f52a9f2217027b0f475d5a91e677b3228588 (patch) | |
tree | ad42e162e131456052938d77977c1030281126b9 /utils/fs/test/fs_suite.go | |
parent | 659386309f36c482ddc0bb9e854ebda3da216491 (diff) | |
download | go-git-5078f52a9f2217027b0f475d5a91e677b3228588.tar.gz |
utils/fs: add OpenFile method to filesystem interface. (#104)
* utils/fs: add OpenFile method to filesystem interface.
* added OpenFile to fs.Filesystem interface.
* added OpenFile implementation to 'os' filesystem.
* bring back BaseFile.
* utils/fs/os: do not use wildcard import.
* utils/fs/os: implement Open and Create using OpenFile.
Diffstat (limited to 'utils/fs/test/fs_suite.go')
-rw-r--r-- | utils/fs/test/fs_suite.go | 131 |
1 files changed, 120 insertions, 11 deletions
diff --git a/utils/fs/test/fs_suite.go b/utils/fs/test/fs_suite.go index a809309..4c0fd09 100644 --- a/utils/fs/test/fs_suite.go +++ b/utils/fs/test/fs_suite.go @@ -3,6 +3,7 @@ package test import ( "fmt" "io/ioutil" + "os" "strings" "testing" @@ -35,13 +36,13 @@ func (s *FilesystemSuite) TestCreateDepthAbsolute(c *C) { } func (s *FilesystemSuite) TestCreateOverwrite(c *C) { - for i := 0; i < 2; i++ { + for i := 0; i < 3; i++ { f, err := s.Fs.Create("foo") c.Assert(err, IsNil) - l, err := f.Write([]byte("foo")) + l, err := f.Write([]byte(fmt.Sprintf("foo%d", i))) c.Assert(err, IsNil) - c.Assert(l, Equals, 3) + c.Assert(l, Equals, 4) err = f.Close() c.Assert(err, IsNil) @@ -52,7 +53,7 @@ func (s *FilesystemSuite) TestCreateOverwrite(c *C) { wrote, err := ioutil.ReadAll(f) c.Assert(err, IsNil) - c.Assert(wrote, DeepEquals, []byte("foo")) + c.Assert(string(wrote), DeepEquals, "foo2") } func (s *FilesystemSuite) TestCreateClose(c *C) { @@ -60,17 +61,127 @@ func (s *FilesystemSuite) TestCreateClose(c *C) { c.Assert(err, IsNil) c.Assert(f.IsClosed(), Equals, false) - f.Write([]byte("foo")) + _, err = f.Write([]byte("foo")) + c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) + + f, err = s.Fs.Open(f.Filename()) + c.Assert(err, IsNil) + + wrote, err := ioutil.ReadAll(f) + c.Assert(err, IsNil) + c.Assert(string(wrote), DeepEquals, "foo") c.Assert(f.Close(), IsNil) +} + +func (s *FilesystemSuite) TestOpenFileNoTruncate(c *C) { + defaultMode := os.FileMode(0666) + + // Create when it does not exist + f, err := s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "foo1") + + f, err = s.Fs.OpenFile("foo1", os.O_RDONLY, defaultMode) + c.Assert(err, IsNil) + s.testReadClose(c, f, "foo1") + + // Create when it does exist + f, err = s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "bar") + + f, err = s.Fs.OpenFile("foo1", os.O_RDONLY, defaultMode) + c.Assert(err, IsNil) + s.testReadClose(c, f, "bar1") +} + +func (s *FilesystemSuite) TestOpenFileAppend(c *C) { + defaultMode := os.FileMode(0666) + + f, err := s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_APPEND, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "foo1") + + f, err = s.Fs.OpenFile("foo1", os.O_WRONLY|os.O_APPEND, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "bar1") + + f, err = s.Fs.OpenFile("foo1", os.O_RDONLY, defaultMode) + c.Assert(err, IsNil) + s.testReadClose(c, f, "foo1bar1") +} + +func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) { + defaultMode := os.FileMode(0666) - file, err := s.Fs.Open(f.Filename()) + f, err := s.Fs.OpenFile("foo1", os.O_CREATE|os.O_TRUNC|os.O_RDWR, defaultMode) c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") - wrote, err := ioutil.ReadAll(file) + written, err := f.Write([]byte("foobar")) + c.Assert(written, Equals, 6) c.Assert(err, IsNil) - c.Assert(wrote, DeepEquals, []byte("foo")) - c.Assert(f.IsClosed(), Equals, true) + _, err = f.Seek(0, os.SEEK_SET) + c.Assert(err, IsNil) + + written, err = f.Write([]byte("qux")) + c.Assert(written, Equals, 3) + c.Assert(err, IsNil) + + _, err = f.Seek(0, os.SEEK_SET) + c.Assert(err, IsNil) + + s.testReadClose(c, f, "quxbar") +} + +func (s *FilesystemSuite) TestOpenFile(c *C) { + defaultMode := os.FileMode(0666) + + f, err := s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode) + c.Assert(err, IsNil) + s.testWriteClose(c, f, "foo1") + + // Truncate if it exists + f, err = s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "foo1overwritten") + + // Read-only if it exists + f, err = s.Fs.OpenFile("foo1", os.O_RDONLY, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testReadClose(c, f, "foo1overwritten") + + // Create when it does exist + f, err = s.Fs.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Filename(), Equals, "foo1") + s.testWriteClose(c, f, "bar") + + f, err = s.Fs.OpenFile("foo1", os.O_RDONLY, defaultMode) + c.Assert(err, IsNil) + s.testReadClose(c, f, "bar") +} + +func (s *FilesystemSuite) testWriteClose(c *C, f File, content string) { + written, err := f.Write([]byte(content)) + c.Assert(written, Equals, len(content)) + c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) +} + +func (s *FilesystemSuite) testReadClose(c *C, f File, content string) { + read, err := ioutil.ReadAll(f) + c.Assert(err, IsNil) + c.Assert(string(read), Equals, content) + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestReadDirAndDir(c *C) { @@ -148,14 +259,12 @@ func (s *FilesystemSuite) TestTempFile(c *C) { func (s *FilesystemSuite) TestTempFileWithPath(c *C) { f, err := s.Fs.TempFile("foo", "bar") c.Assert(err, IsNil) - fmt.Printf("f: %s\n", f.Filename()) c.Assert(strings.HasPrefix(f.Filename(), s.Fs.Join("foo", "bar")), Equals, true) } func (s *FilesystemSuite) TestTempFileFullWithPath(c *C) { f, err := s.Fs.TempFile("/foo", "bar") c.Assert(err, IsNil) - fmt.Printf("f: %s\n", f.Filename()) c.Assert(strings.HasPrefix(f.Filename(), s.Fs.Join("foo", "bar")), Equals, true) } |