Skip to content

Commit

Permalink
Memory.ReadDir() should return an error when path isn't found.
Browse files Browse the repository at this point in the history
Presently, it returns an empty list of files and a `nil` error value.
This PR aims to fix go-git#37
  • Loading branch information
weberc2-tempus committed Nov 14, 2023
1 parent b30fba3 commit 7c7fd18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sort"
"strings"
"syscall"
"time"

"github.com/go-git/go-billy/v5"
Expand All @@ -25,7 +26,7 @@ type Memory struct {
tempCount int
}

//New returns a new Memory filesystem.
// New returns a new Memory filesystem.
func New() billy.Filesystem {
fs := &Memory{s: newStorage()}
return chroot.New(fs, string(separator))
Expand Down Expand Up @@ -133,6 +134,8 @@ func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
if target, isLink := fs.resolveLink(path, f); isLink {
return fs.ReadDir(target)
}
} else {
return nil, &os.PathError{Op: "open", Path: path, Err: syscall.ENOENT}
}

var entries []os.FileInfo
Expand Down
6 changes: 6 additions & 0 deletions memfs/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func (s *MemorySuite) TestOrder(c *C) {
}
}

func (s *MemorySuite) TestNotFound(c *C) {
files, err := s.FS.ReadDir("asdf")
c.Assert(files, HasLen, 0)
c.Assert(err, ErrorMatches, "open /asdf: no such file or directory")
}

func (s *MemorySuite) TestTruncateAppend(c *C) {
err := util.WriteFile(s.FS, "truncate_append", []byte("file-content"), 0666)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 7c7fd18

Please sign in to comment.