Skip to content

Commit

Permalink
subdirfs: fix FileInfo.Stat
Browse files Browse the repository at this point in the history
  • Loading branch information
mcuadros committed Apr 17, 2017
1 parent ddfaadb commit 0f02306
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
7 changes: 1 addition & 6 deletions subdir/subdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ func (s *subdirFs) Stat(filename string) (billy.FileInfo, error) {
return nil, err
}

filename, err = filepath.Rel(s.Base(), fullpath)
if err != nil {
return nil, err
}

return newFileInfo(filename, fi), nil
return newFileInfo(filepath.Base(fullpath), fi), nil
}

func (s *subdirFs) ReadDir(path string) ([]billy.FileInfo, error) {
Expand Down
43 changes: 43 additions & 0 deletions test/fs_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,29 @@ func (s *FilesystemSuite) TestReadDirFileInfoDirs(c *C) {
c.Assert(info[0].Mode(), Not(Equals), 0)
}

func (s *FilesystemSuite) TestStat(c *C) {
WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)

fi, err := s.FS.Stat("foo/bar")
c.Assert(err, IsNil)
c.Assert(fi.Name(), Equals, "bar")
c.Assert(fi.Size(), Equals, int64(3))
c.Assert(fi.Mode(), Equals, customMode)
c.Assert(fi.ModTime().IsZero(), Equals, false)
c.Assert(fi.IsDir(), Equals, false)
}

func (s *FilesystemSuite) TestStatDir(c *C) {
s.FS.MkdirAll("foo/bar", 0644)

fi, err := s.FS.Stat("foo/bar")
c.Assert(err, IsNil)
c.Assert(fi.Name(), Equals, "bar")
c.Assert(fi.Mode().IsDir(), Equals, true)
c.Assert(fi.ModTime().IsZero(), Equals, false)
c.Assert(fi.IsDir(), Equals, true)
}

func (s *FilesystemSuite) TestStatNonExistent(c *C) {
fi, err := s.FS.Stat("non-existent")
comment := Commentf("error: %s", err)
Expand Down Expand Up @@ -512,6 +535,26 @@ func (s *FilesystemSuite) TestRename(c *C) {
c.Assert(err, IsNil)
}

func (s *FilesystemSuite) TestRenameToDir(c *C) {
err := WriteFile(s.FS, "foo", nil, 0644)
c.Assert(err, IsNil)

err = s.FS.Rename("foo", "bar/qux")
c.Assert(err, IsNil)

old, err := s.FS.Stat("foo")
c.Assert(old, IsNil)
c.Assert(os.IsNotExist(err), Equals, true)

dir, err := s.FS.Stat("bar")
c.Assert(dir, NotNil)
c.Assert(err, IsNil)

file, err := s.FS.Stat("bar/qux")
c.Assert(file.Name(), Equals, "qux")
c.Assert(err, IsNil)
}

func (s *FilesystemSuite) TestRenameDir(c *C) {
err := s.FS.MkdirAll("foo", 0644)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 0f02306

Please sign in to comment.