Skip to content

Commit 6dc2d2a

Browse files
achille-rousselgopherbot
authored andcommitted
testing/fstest: fix the Glob test when dir entries are out of order
This change adds a test highlighting an issue with the fstest.TestFS test suite which occurred when the fs.FS implementation would expose directories returning unordered directory entries from their ReadDir method. --- FAIL: TestShuffledFS (0.00s) testfs_test.go:76: testing fs.Sub(fsys, tmp): TestFS found errors: .: Glob(`*e*`): wrong output: extra: one missing: one The issue came from having the wrong variable passed to the checkGlob method. There are two variables named list and list2, the latter is sorted, and the checkGlob method expects a sorted list but was passed list instead of list2. Change-Id: I5e49dccf14077e7d1fee51687eb6a5eeb0330c16 Reviewed-on: https://go-review.googlesource.com/c/go/+/503175 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 2b0ff4b commit 6dc2d2a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/testing/fstest/testfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func (t *fsTester) checkDir(dir string) {
270270
}
271271
}
272272

273-
t.checkGlob(dir, list)
273+
t.checkGlob(dir, list2)
274274
}
275275

276276
// formatEntry formats an fs.DirEntry into a string for error messages and comparison.

src/testing/fstest/testfs_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package fstest
66

77
import (
88
"internal/testenv"
9+
"io/fs"
910
"os"
1011
"path/filepath"
12+
"sort"
1113
"testing"
1214
)
1315

@@ -38,3 +40,39 @@ func TestDash(t *testing.T) {
3840
t.Error(err)
3941
}
4042
}
43+
44+
type shuffledFS MapFS
45+
46+
func (fsys shuffledFS) Open(name string) (fs.File, error) {
47+
f, err := MapFS(fsys).Open(name)
48+
if err != nil {
49+
return nil, err
50+
}
51+
return &shuffledFile{File: f}, nil
52+
}
53+
54+
type shuffledFile struct{ fs.File }
55+
56+
func (f *shuffledFile) ReadDir(n int) ([]fs.DirEntry, error) {
57+
dirents, err := f.File.(fs.ReadDirFile).ReadDir(n)
58+
// Shuffle in a deterministic way, all we care about is making sure that the
59+
// list of directory entries is not is the lexicographic order.
60+
//
61+
// We do this to make sure that the TestFS test suite is not affected by the
62+
// order of directory entries.
63+
sort.Slice(dirents, func(i, j int) bool {
64+
return dirents[i].Name() > dirents[j].Name()
65+
})
66+
return dirents, err
67+
}
68+
69+
func TestShuffledFS(t *testing.T) {
70+
fsys := shuffledFS{
71+
"tmp/one": {Data: []byte("1")},
72+
"tmp/two": {Data: []byte("2")},
73+
"tmp/three": {Data: []byte("3")},
74+
}
75+
if err := TestFS(fsys, "tmp/one", "tmp/two", "tmp/three"); err != nil {
76+
t.Error(err)
77+
}
78+
}

0 commit comments

Comments
 (0)