Skip to content

Commit 0f02306

Browse files
committed
subdirfs: fix FileInfo.Stat
1 parent ddfaadb commit 0f02306

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

subdir/subdir.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ func (s *subdirFs) Stat(filename string) (billy.FileInfo, error) {
8585
return nil, err
8686
}
8787

88-
filename, err = filepath.Rel(s.Base(), fullpath)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
return newFileInfo(filename, fi), nil
88+
return newFileInfo(filepath.Base(fullpath), fi), nil
9489
}
9590

9691
func (s *subdirFs) ReadDir(path string) ([]billy.FileInfo, error) {

test/fs_suite.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,29 @@ func (s *FilesystemSuite) TestReadDirFileInfoDirs(c *C) {
447447
c.Assert(info[0].Mode(), Not(Equals), 0)
448448
}
449449

450+
func (s *FilesystemSuite) TestStat(c *C) {
451+
WriteFile(s.FS, "foo/bar", []byte("foo"), customMode)
452+
453+
fi, err := s.FS.Stat("foo/bar")
454+
c.Assert(err, IsNil)
455+
c.Assert(fi.Name(), Equals, "bar")
456+
c.Assert(fi.Size(), Equals, int64(3))
457+
c.Assert(fi.Mode(), Equals, customMode)
458+
c.Assert(fi.ModTime().IsZero(), Equals, false)
459+
c.Assert(fi.IsDir(), Equals, false)
460+
}
461+
462+
func (s *FilesystemSuite) TestStatDir(c *C) {
463+
s.FS.MkdirAll("foo/bar", 0644)
464+
465+
fi, err := s.FS.Stat("foo/bar")
466+
c.Assert(err, IsNil)
467+
c.Assert(fi.Name(), Equals, "bar")
468+
c.Assert(fi.Mode().IsDir(), Equals, true)
469+
c.Assert(fi.ModTime().IsZero(), Equals, false)
470+
c.Assert(fi.IsDir(), Equals, true)
471+
}
472+
450473
func (s *FilesystemSuite) TestStatNonExistent(c *C) {
451474
fi, err := s.FS.Stat("non-existent")
452475
comment := Commentf("error: %s", err)
@@ -512,6 +535,26 @@ func (s *FilesystemSuite) TestRename(c *C) {
512535
c.Assert(err, IsNil)
513536
}
514537

538+
func (s *FilesystemSuite) TestRenameToDir(c *C) {
539+
err := WriteFile(s.FS, "foo", nil, 0644)
540+
c.Assert(err, IsNil)
541+
542+
err = s.FS.Rename("foo", "bar/qux")
543+
c.Assert(err, IsNil)
544+
545+
old, err := s.FS.Stat("foo")
546+
c.Assert(old, IsNil)
547+
c.Assert(os.IsNotExist(err), Equals, true)
548+
549+
dir, err := s.FS.Stat("bar")
550+
c.Assert(dir, NotNil)
551+
c.Assert(err, IsNil)
552+
553+
file, err := s.FS.Stat("bar/qux")
554+
c.Assert(file.Name(), Equals, "qux")
555+
c.Assert(err, IsNil)
556+
}
557+
515558
func (s *FilesystemSuite) TestRenameDir(c *C) {
516559
err := s.FS.MkdirAll("foo", 0644)
517560
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)