diff --git a/osfs/os_test.go b/osfs/os_test.go index 2bb5388..767dc74 100644 --- a/osfs/os_test.go +++ b/osfs/os_test.go @@ -20,7 +20,7 @@ type OSSuite struct { var _ = Suite(&OSSuite{}) func (s *OSSuite) SetUpTest(c *C) { - s.path, _ = ioutil.TempDir(os.TempDir(), "go-git-os-fs-test") + s.path, _ = ioutil.TempDir(os.TempDir(), "go-billy-osfs-test") s.FilesystemSuite.FS = New(s.path) } diff --git a/subdir/file.go b/subdir/file.go index 34fbb19..d2e4f27 100644 --- a/subdir/file.go +++ b/subdir/file.go @@ -3,7 +3,6 @@ package subdir import ( "io" "path/filepath" - "strings" "gopkg.in/src-d/go-billy.v2" ) @@ -14,9 +13,12 @@ type file struct { f billy.File } -func newFile(f billy.File, filename string) billy.File { +func newFile(fs billy.Filesystem, f billy.File, filename string) billy.File { + filename = fs.Join(fs.Base(), filename) + filename, _ = filepath.Rel(fs.Base(), filename) + return &file{ - BaseFile: billy.BaseFile{BaseFilename: resolve(filename)}, + BaseFile: billy.BaseFile{BaseFilename: filename}, f: f, } } @@ -46,14 +48,3 @@ func (f *file) Close() error { defer func() { f.Closed = true }() return f.f.Close() } - -func resolve(path string) string { - rp := filepath.Clean(path) - if rp == "/" { - rp = "." - } else if strings.HasPrefix(rp, "/") { - rp = rp[1:] - } - - return rp -} diff --git a/subdir/subdir.go b/subdir/subdir.go index d52cb8d..5722292 100644 --- a/subdir/subdir.go +++ b/subdir/subdir.go @@ -33,7 +33,7 @@ func (s *subdirFs) Create(filename string) (billy.File, error) { return nil, err } - return newFile(f, filename), nil + return newFile(s, f, filename), nil } func (s *subdirFs) Open(filename string) (billy.File, error) { @@ -42,7 +42,7 @@ func (s *subdirFs) Open(filename string) (billy.File, error) { return nil, err } - return newFile(f, filename), nil + return newFile(s, f, filename), nil } func (s *subdirFs) OpenFile(filename string, flag int, mode os.FileMode) ( @@ -53,7 +53,7 @@ func (s *subdirFs) OpenFile(filename string, flag int, mode os.FileMode) ( return nil, err } - return newFile(f, filename), nil + return newFile(s, f, filename), nil } func (s *subdirFs) TempFile(dir, prefix string) (billy.File, error) { @@ -62,7 +62,7 @@ func (s *subdirFs) TempFile(dir, prefix string) (billy.File, error) { return nil, err } - return newFile(f, s.Join(dir, filepath.Base(f.Filename()))), nil + return newFile(s, f, s.Join(dir, filepath.Base(f.Filename()))), nil } func (s *subdirFs) Rename(from, to string) error { @@ -79,8 +79,13 @@ func (s *subdirFs) MkdirAll(filename string, perm os.FileMode) error { } func (s *subdirFs) Stat(filename string) (billy.FileInfo, error) { - filename = removeLeadingSlash(filename) - fi, err := s.underlying.Stat(s.underlyingPath(filename)) + fullpath := s.underlyingPath(filename) + fi, err := s.underlying.Stat(fullpath) + if err != nil { + return nil, err + } + + filename, err = filepath.Rel(s.Base(), fullpath) if err != nil { return nil, err } @@ -107,17 +112,9 @@ func (s *subdirFs) Join(elem ...string) string { } func (s *subdirFs) Dir(path string) billy.Filesystem { - return New(s, removeLeadingSlash(path)) + return New(s.underlying, s.underlyingPath(path)) } func (s *subdirFs) Base() string { return s.base } - -func removeLeadingSlash(path string) string { - if strings.HasPrefix(path, "/") { - return path[1:] - } - - return path -} diff --git a/subdir/suite_test.go b/subdir/subdir_test.go similarity index 83% rename from subdir/suite_test.go rename to subdir/subdir_test.go index e2b2214..197913b 100644 --- a/subdir/suite_test.go +++ b/subdir/subdir_test.go @@ -23,9 +23,10 @@ type FilesystemSuite struct { var _ = Suite(&FilesystemSuite{}) func (s *FilesystemSuite) SetUpTest(c *C) { - s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-git-fs-test") - osFs := osfs.New(s.path) - s.cfs = New(osFs, "test-subdir") + s.path, _ = ioutil.TempDir(stdos.TempDir(), "go-billy-subdirfs-test") + fs := osfs.New(s.path) + + s.cfs = New(fs, "test-subdir") s.FilesystemSuite.FS = s.cfs } diff --git a/test/fs_suite.go b/test/fs_suite.go index 16ff18c..637ab33 100644 --- a/test/fs_suite.go +++ b/test/fs_suite.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "os" + "path/filepath" "strings" "testing" @@ -23,6 +24,7 @@ func (s *FilesystemSuite) TestCreate(c *C) { f, err := s.FS.Create("foo") c.Assert(err, IsNil) c.Assert(f.Filename(), Equals, "foo") + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestOpen(c *C) { @@ -34,6 +36,7 @@ func (s *FilesystemSuite) TestOpen(c *C) { f, err = s.FS.Open("foo") c.Assert(err, IsNil) c.Assert(f.Filename(), Equals, "foo") + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestOpenNotExists(c *C) { @@ -54,13 +57,15 @@ func (s *FilesystemSuite) TestCreateDir(c *C) { func (s *FilesystemSuite) TestCreateDepth(c *C) { f, err := s.FS.Create("bar/foo") c.Assert(err, IsNil) - c.Assert(f.Filename(), Equals, "bar/foo") + c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo")) + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestCreateDepthAbsolute(c *C) { f, err := s.FS.Create("/bar/foo") c.Assert(err, IsNil) - c.Assert(f.Filename(), Equals, "bar/foo") + c.Assert(f.Filename(), Equals, s.FS.Join("bar", "foo")) + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestCreateOverwrite(c *C) { @@ -82,6 +87,7 @@ func (s *FilesystemSuite) TestCreateOverwrite(c *C) { wrote, err := ioutil.ReadAll(f) c.Assert(err, IsNil) c.Assert(string(wrote), DeepEquals, "foo2") + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestCreateClose(c *C) { @@ -199,13 +205,13 @@ func (s *FilesystemSuite) TestOpenFileReadWrite(c *C) { } func (s *FilesystemSuite) TestOpenFileWithModes(c *C) { - f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755) + f, err := s.FS.OpenFile("foo", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, customMode) c.Assert(err, IsNil) c.Assert(f.Close(), IsNil) fi, err := s.FS.Stat("foo") c.Assert(err, IsNil) - c.Assert(fi.Mode(), Equals, os.FileMode(0755)) + c.Assert(fi.Mode(), Equals, os.FileMode(customMode)) } func (s *FilesystemSuite) testWriteClose(c *C, f File, content string) { @@ -281,6 +287,8 @@ func (s *FilesystemSuite) TestFileNonRead(c *C) { _, err = ioutil.ReadAll(f) c.Assert(err, NotNil) + + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestFileSeekstart(c *C) { @@ -478,11 +486,14 @@ func (s *FilesystemSuite) TestDirStat(c *C) { } func (s *FilesystemSuite) TestCreateInDir(c *C) { - dir := s.FS.Dir("foo") - f, err := dir.Create("bar") + f, err := s.FS.Dir("foo").Create("bar") c.Assert(err, IsNil) c.Assert(f.Close(), IsNil) c.Assert(f.Filename(), Equals, "bar") + + f, err = s.FS.Open("foo/bar") + c.Assert(f.Filename(), Equals, s.FS.Join("foo", "bar")) + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestRename(c *C) { @@ -531,6 +542,7 @@ func (s *FilesystemSuite) TestRenameDir(c *C) { func (s *FilesystemSuite) TestTempFile(c *C) { f, err := s.FS.TempFile("", "bar") c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) c.Assert(strings.HasPrefix(f.Filename(), "bar"), Equals, true) } @@ -538,12 +550,16 @@ func (s *FilesystemSuite) TestTempFile(c *C) { func (s *FilesystemSuite) TestTempFileWithPath(c *C) { f, err := s.FS.TempFile("foo", "bar") c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) + 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) + c.Assert(f.Close(), IsNil) + c.Assert(strings.HasPrefix(f.Filename(), s.FS.Join("foo", "bar")), Equals, true) } @@ -558,6 +574,8 @@ func (s *FilesystemSuite) TestOpenAndWrite(c *C) { n, err := foo.Write([]byte("foo")) c.Assert(err, NotNil) c.Assert(n, Equals, 0) + + c.Assert(foo.Close(), IsNil) } func (s *FilesystemSuite) TestOpenAndStat(c *C) { @@ -568,6 +586,7 @@ func (s *FilesystemSuite) TestOpenAndStat(c *C) { c.Assert(foo, NotNil) c.Assert(foo.Filename(), Equals, "foo") c.Assert(err, IsNil) + c.Assert(foo.Close(), IsNil) stat, err := s.FS.Stat("foo") c.Assert(stat, NotNil) @@ -611,7 +630,7 @@ func (s *FilesystemSuite) TestRemoveTempFile(c *C) { } func (s *FilesystemSuite) TestJoin(c *C) { - c.Assert(s.FS.Join("foo", "bar"), Equals, "foo/bar") + c.Assert(s.FS.Join("foo", "bar"), Equals, fmt.Sprintf("foo%cbar", filepath.Separator)) } func (s *FilesystemSuite) TestBase(c *C) { @@ -663,14 +682,14 @@ func (s *FilesystemSuite) TestReadWriteLargeFile(c *C) { c.Assert(err, IsNil) c.Assert(n, Equals, size) - err = f.Close() - c.Assert(err, IsNil) + c.Assert(f.Close(), IsNil) f, err = s.FS.Open("foo") c.Assert(err, IsNil) b, err := ioutil.ReadAll(f) c.Assert(err, IsNil) c.Assert(len(b), Equals, size) + c.Assert(f.Close(), IsNil) } func (s *FilesystemSuite) TestRemoveAllNonExistent(c *C) { @@ -806,4 +825,6 @@ func (s *FilesystemSuite) TestWriteFile(c *C) { wrote, err := ioutil.ReadAll(f) c.Assert(err, IsNil) c.Assert(string(wrote), DeepEquals, "bar") + + c.Assert(f.Close(), IsNil) } diff --git a/test/fs_suite_linux.go b/test/fs_suite_linux.go new file mode 100644 index 0000000..8f51534 --- /dev/null +++ b/test/fs_suite_linux.go @@ -0,0 +1,7 @@ +// +build !windows + +package test + +import "os" + +var customMode os.FileMode = 0755 diff --git a/test/fs_suite_windows.go b/test/fs_suite_windows.go new file mode 100644 index 0000000..cc289f8 --- /dev/null +++ b/test/fs_suite_windows.go @@ -0,0 +1,7 @@ +// +build windows + +package test + +import "os" + +var customMode os.FileMode = 0666