Skip to content

Commit c862faa

Browse files
authored
Merge branch 'master' into o_excl
2 parents 78517ac + b791567 commit c862faa

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

memfs/memory.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"sort"
1011
"strings"
1112
"time"
1213

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

125+
type ByName []os.FileInfo
126+
127+
func (a ByName) Len() int { return len(a) }
128+
func (a ByName) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
129+
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
130+
124131
func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
125132
if f, has := fs.s.Get(path); has {
126133
if target, isLink := fs.resolveLink(path, f); isLink {
@@ -134,6 +141,8 @@ func (fs *Memory) ReadDir(path string) ([]os.FileInfo, error) {
134141
entries = append(entries, fi)
135142
}
136143

144+
sort.Sort(ByName(entries))
145+
137146
return entries, nil
138147
}
139148

memfs/memory_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (s *MemorySuite) TestNegativeOffsets(c *C) {
4747
c.Assert(err, ErrorMatches, "writeat negative: negative offset")
4848
}
4949

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

5960
_, err = s.FS.OpenFile("exclusive", os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
6061
c.Assert(err, ErrorMatches, os.ErrExist.Error())
62+
63+
func (s *MemorySuite) TestOrder(c *C) {
64+
var err error
65+
66+
files := []string{
67+
"a",
68+
"b",
69+
"c",
70+
}
71+
for _, f := range files {
72+
_, err = s.FS.Create(f)
73+
c.Assert(err, IsNil)
74+
}
75+
76+
attemps := 30
77+
for n := 0; n < attemps; n++ {
78+
actual, err := s.FS.ReadDir("")
79+
c.Assert(err, IsNil)
80+
81+
for i, f := range files {
82+
c.Assert(actual[i].Name(), Equals, f)
83+
}
84+
}
6185
}

0 commit comments

Comments
 (0)