1
0
mirror of https://github.com/golang/go synced 2024-11-26 04:07:59 -07:00

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>
This commit is contained in:
Josh Bleecher Snyder 2021-06-15 16:01:25 -07:00
parent c04a32e59a
commit 213e157d3a
2 changed files with 31 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}
}