aboutsummaryrefslogtreecommitdiffstats
path: root/utils/fs/test/fs_suite.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-10-24 15:14:22 +0200
committerGitHub <noreply@github.com>2016-10-24 15:14:22 +0200
commitd45eb0402b2f3dace2ed1f91ee53e2c591a7ba3c (patch)
tree7e52ed37c12ec51c6efa3c33c0f38d135bd2c122 /utils/fs/test/fs_suite.go
parent8cd772a53e8ecd2687b739eea110fa9b179f1e0f (diff)
downloadgo-git-d45eb0402b2f3dace2ed1f91ee53e2c591a7ba3c.tar.gz
utils/fs: move 'os' and 'test' to separate packages. (#93)
* create utils/fs/test package to expose generic test suite to 3rd party fs implementations. * move 'os' to its own package to avoid cyclic dependency (test -> fs -> test, becomes test -> fs, os -> test, os -> fs). * remove TestCreateAndWrite: some of our implementations cannot read a file that was just created, written and not closed yet.
Diffstat (limited to 'utils/fs/test/fs_suite.go')
-rw-r--r--utils/fs/test/fs_suite.go158
1 files changed, 158 insertions, 0 deletions
diff --git a/utils/fs/test/fs_suite.go b/utils/fs/test/fs_suite.go
new file mode 100644
index 0000000..109311f
--- /dev/null
+++ b/utils/fs/test/fs_suite.go
@@ -0,0 +1,158 @@
+package test
+
+import (
+ "fmt"
+ "io/ioutil"
+ "strings"
+ "testing"
+
+ . "gopkg.in/check.v1"
+ . "gopkg.in/src-d/go-git.v4/utils/fs"
+)
+
+func Test(t *testing.T) { TestingT(t) }
+
+type FilesystemSuite struct {
+ Fs Filesystem
+}
+
+func (s *FilesystemSuite) TestCreate(c *C) {
+ f, err := s.Fs.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Filename(), Equals, "foo")
+}
+
+func (s *FilesystemSuite) TestCreateDepth(c *C) {
+ f, err := s.Fs.Create("bar/foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Filename(), Equals, "bar/foo")
+}
+
+func (s *FilesystemSuite) TestCreateDepthAbsolute(c *C) {
+ f, err := s.Fs.Create("/bar/foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Filename(), Equals, "bar/foo")
+}
+
+func (s *FilesystemSuite) TestCreateOverwrite(c *C) {
+ for i := 0; i < 2; i++ {
+ f, err := s.Fs.Create("foo")
+ c.Assert(err, IsNil)
+
+ l, err := f.Write([]byte("foo"))
+ c.Assert(err, IsNil)
+ c.Assert(l, Equals, 3)
+
+ err = f.Close()
+ c.Assert(err, IsNil)
+ }
+
+ f, err := s.Fs.Open("foo")
+ c.Assert(err, IsNil)
+
+ wrote, err := ioutil.ReadAll(f)
+ c.Assert(err, IsNil)
+ c.Assert(wrote, DeepEquals, []byte("foo"))
+}
+
+func (s *FilesystemSuite) TestCreateClose(c *C) {
+ f, err := s.Fs.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.IsClosed(), Equals, false)
+
+ f.Write([]byte("foo"))
+ c.Assert(f.Close(), IsNil)
+
+ file, err := s.Fs.Open(f.Filename())
+ c.Assert(err, IsNil)
+
+ wrote, err := ioutil.ReadAll(file)
+ c.Assert(err, IsNil)
+ c.Assert(wrote, DeepEquals, []byte("foo"))
+
+ c.Assert(f.IsClosed(), Equals, true)
+}
+
+func (s *FilesystemSuite) TestReadDirAndDir(c *C) {
+ files := []string{"foo", "bar", "qux/baz", "qux/qux"}
+ for _, name := range files {
+ f, err := s.Fs.Create(name)
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+ }
+
+ info, err := s.Fs.ReadDir("/")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 3)
+
+ info, err = s.Fs.ReadDir("/qux")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 2)
+
+ qux := s.Fs.Dir("/qux")
+ info, err = qux.ReadDir("/")
+ c.Assert(err, IsNil)
+ c.Assert(info, HasLen, 2)
+}
+
+func (s *FilesystemSuite) TestRename(c *C) {
+ f, err := s.Fs.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+
+ err = s.Fs.Rename("foo", "bar")
+ c.Assert(err, IsNil)
+
+ foo, err := s.Fs.Stat("foo")
+ c.Assert(foo, IsNil)
+ c.Assert(err, NotNil)
+
+ bar, err := s.Fs.Stat("bar")
+ c.Assert(bar, NotNil)
+ c.Assert(err, IsNil)
+}
+
+func (s *FilesystemSuite) TestTempFile(c *C) {
+ f, err := s.Fs.TempFile("", "bar")
+ c.Assert(err, IsNil)
+
+ c.Assert(strings.HasPrefix(f.Filename(), "bar"), Equals, true)
+}
+
+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)
+}
+
+func (s *FilesystemSuite) TestOpenAndStat(c *C) {
+ f, err := s.Fs.Create("foo")
+ c.Assert(err, IsNil)
+ c.Assert(f.Close(), IsNil)
+
+ foo, err := s.Fs.Open("foo")
+ c.Assert(foo, NotNil)
+ c.Assert(foo.Filename(), Equals, "foo")
+ c.Assert(err, IsNil)
+
+ stat, err := s.Fs.Stat("foo")
+ c.Assert(stat, NotNil)
+ c.Assert(err, IsNil)
+ c.Assert(stat.Name(), Equals, "foo")
+}
+
+func (s *FilesystemSuite) TestJoin(c *C) {
+ c.Assert(s.Fs.Join("foo", "bar"), Equals, "foo/bar")
+}
+
+func (s *FilesystemSuite) TestBase(c *C) {
+ c.Assert(s.Fs.Base(), Not(Equals), "")
+}