aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorJosh Bleecher Snyder <josharian@gmail.com>2021-06-15 16:01:25 -0700
committerJosh Bleecher Snyder <josharian@gmail.com>2021-08-16 22:27:47 +0000
commit213e157d3aad3fbb289d184a5cb4b18258162634 (patch)
tree593edfd34313a72560331bc08029c6a49099892d /src/testing
parentc04a32e59a001f0490082619bbe6a36e1e23ef99 (diff)
downloadgo-213e157d3aad3fbb289d184a5cb4b18258162634.tar.xz
testing/fstest: allow specifying file for "." in MapFS
Prior to this commit, specifying a file for "." in MapFS created an invalid fs.FS and caused infinite recursion in fs.WalkDir. Fixes #46776 Change-Id: Ia9e4ae1125355a74dba9ee6b36451b7fda75a862 Reviewed-on: https://go-review.googlesource.com/c/go/+/328409 Trust: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/fstest/mapfs.go4
-rw-r--r--src/testing/fstest/mapfs_test.go28
2 files changed, 31 insertions, 1 deletions
diff --git a/src/testing/fstest/mapfs.go b/src/testing/fstest/mapfs.go
index 9fef2f4696..056ef133fa 100644
--- a/src/testing/fstest/mapfs.go
+++ b/src/testing/fstest/mapfs.go
@@ -66,7 +66,9 @@ func (fsys MapFS) Open(name string) (fs.File, error) {
for fname, f := range fsys {
i := strings.Index(fname, "/")
if i < 0 {
- list = append(list, mapFileInfo{fname, f})
+ if fname != "." {
+ list = append(list, mapFileInfo{fname, f})
+ }
} else {
need[fname[:i]] = true
}
diff --git a/src/testing/fstest/mapfs_test.go b/src/testing/fstest/mapfs_test.go
index 2abedd6735..c8d29283b2 100644
--- a/src/testing/fstest/mapfs_test.go
+++ b/src/testing/fstest/mapfs_test.go
@@ -5,6 +5,9 @@
package fstest
import (
+ "fmt"
+ "io/fs"
+ "strings"
"testing"
)
@@ -17,3 +20,28 @@ func TestMapFS(t *testing.T) {
t.Fatal(err)
}
}
+
+func TestMapFSChmodDot(t *testing.T) {
+ m := MapFS{
+ "a/b.txt": &MapFile{Mode: 0666},
+ ".": &MapFile{Mode: 0777 | fs.ModeDir},
+ }
+ buf := new(strings.Builder)
+ fs.WalkDir(m, ".", func(path string, d fs.DirEntry, err error) error {
+ fi, err := d.Info()
+ if err != nil {
+ return err
+ }
+ fmt.Fprintf(buf, "%s: %v\n", path, fi.Mode())
+ return nil
+ })
+ want := `
+.: drwxrwxrwx
+a: d---------
+a/b.txt: -rw-rw-rw-
+`[1:]
+ got := buf.String()
+ if want != got {
+ t.Errorf("MapFS modes want:\n%s\ngot:\n%s\n", want, got)
+ }
+}