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 #37.
  • Loading branch information
weberc2-tempus committed Nov 17, 2023
1 parent b30fba3 commit 28a448a
Show file tree
Hide file tree
Showing 2 changed files with 16 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
12 changes: 12 additions & 0 deletions memfs/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"testing"

"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -85,6 +86,17 @@ func (s *MemorySuite) TestOrder(c *C) {
}
}

func (s *MemorySuite) TestNotFound(c *C) {
files, err := s.FS.ReadDir("asdf")
c.Assert(files, HasLen, 0)
// JS / wasip have this error message captalised.
msg := "open /asdf: (N|n)o such file or directory"
if runtime.GOOS == "windows" {
msg = "open \\\\asdf: The system cannot find the file specified\."

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, windows-latest)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

unknown escape sequence

Check failure on line 95 in memfs/memory_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, windows-latest)

unknown escape sequence
}
c.Assert(err, ErrorMatches, msg)
}

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 28a448a

Please sign in to comment.