]> Cypherpunks repositories - gostls13.git/commitdiff
testing/fstest: fix the Glob test when dir entries are out of order
authorAchille Roussel <achille.roussel@gmail.com>
Wed, 14 Jun 2023 04:39:54 +0000 (21:39 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 16 Jun 2023 18:21:40 +0000 (18:21 +0000)
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 <gobot@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/testing/fstest/testfs.go
src/testing/fstest/testfs_test.go

index ddb608088233ec7b8efd4e359388358f90afbfc2..78b0b82640d0bcb909d3e1c8eff72d67d9fed309 100644 (file)
@@ -270,7 +270,7 @@ func (t *fsTester) checkDir(dir string) {
                }
        }
 
-       t.checkGlob(dir, list)
+       t.checkGlob(dir, list2)
 }
 
 // formatEntry formats an fs.DirEntry into a string for error messages and comparison.
index aefb4b33617ab119e30d259517d37694defbe560..a48c597ff43e320f3cca7d83aec1c310a408f37e 100644 (file)
@@ -6,8 +6,10 @@ package fstest
 
 import (
        "internal/testenv"
+       "io/fs"
        "os"
        "path/filepath"
+       "sort"
        "testing"
 )
 
@@ -38,3 +40,39 @@ func TestDash(t *testing.T) {
                t.Error(err)
        }
 }
+
+type shuffledFS MapFS
+
+func (fsys shuffledFS) Open(name string) (fs.File, error) {
+       f, err := MapFS(fsys).Open(name)
+       if err != nil {
+               return nil, err
+       }
+       return &shuffledFile{File: f}, nil
+}
+
+type shuffledFile struct{ fs.File }
+
+func (f *shuffledFile) ReadDir(n int) ([]fs.DirEntry, error) {
+       dirents, err := f.File.(fs.ReadDirFile).ReadDir(n)
+       // Shuffle in a deterministic way, all we care about is making sure that the
+       // list of directory entries is not is the lexicographic order.
+       //
+       // We do this to make sure that the TestFS test suite is not affected by the
+       // order of directory entries.
+       sort.Slice(dirents, func(i, j int) bool {
+               return dirents[i].Name() > dirents[j].Name()
+       })
+       return dirents, err
+}
+
+func TestShuffledFS(t *testing.T) {
+       fsys := shuffledFS{
+               "tmp/one":   {Data: []byte("1")},
+               "tmp/two":   {Data: []byte("2")},
+               "tmp/three": {Data: []byte("3")},
+       }
+       if err := TestFS(fsys, "tmp/one", "tmp/two", "tmp/three"); err != nil {
+               t.Error(err)
+       }
+}