Skip to content

Commit

Permalink
Merge branch 'master' into o_excl
Browse files Browse the repository at this point in the history
mcuadros authored Mar 10, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 78517ac + b791567 commit c862faa
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions memfs/memory.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"

@@ -121,6 +122,12 @@ func (fs *Memory) Lstat(filename string) (os.FileInfo, error) {
return f.Stat()
}

type ByName []os.FileInfo

func (a ByName) Len() int { return len(a) }
func (a ByName) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
if f, has := fs.s.Get(path); has {
if target, isLink := fs.resolveLink(path, f); isLink {
@@ -134,6 +141,8 @@ func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
entries = append(entries, fi)
}

sort.Sort(ByName(entries))

return entries, nil
}

24 changes: 24 additions & 0 deletions memfs/memory_test.go
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@ func (s *MemorySuite) TestNegativeOffsets(c *C) {
c.Assert(err, ErrorMatches, "writeat negative: negative offset")
}


func (s *MemorySuite) TestExclusive(c *C) {
f, err := s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
c.Assert(err, IsNil)
@@ -58,4 +59,27 @@ func (s *MemorySuite) TestExclusive(c *C) {

_, err = s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
c.Assert(err, ErrorMatches, os.ErrExist.Error())

func (s *MemorySuite) TestOrder(c *C) {
var err error

files := []string{
"a",
"b",
"c",
}
for _, f := range files {
_, err = s.FS.Create(f)
c.Assert(err, IsNil)
}

attemps := 30
for n := 0; n < attemps; n++ {
actual, err := s.FS.ReadDir("")
c.Assert(err, IsNil)

for i, f := range files {
c.Assert(actual[i].Name(), Equals, f)
}
}
}

0 comments on commit c862faa

Please sign in to comment.