]> Cypherpunks repositories - gostls13.git/commitdiff
testing/fstest: allow specifying file for "." in MapFS
authorJosh Bleecher Snyder <josharian@gmail.com>
Tue, 15 Jun 2021 23:01:25 +0000 (16:01 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Mon, 16 Aug 2021 22:27:47 +0000 (22:27 +0000)
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>
src/testing/fstest/mapfs.go
src/testing/fstest/mapfs_test.go

index 9fef2f4696ec7944acc891eb3c69e86a3f376139..056ef133fac928e173c26ce8d34d7ab5f18b064e 100644 (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
                        }
index 2abedd67351fc4d4b258225c7ec79d5d02764fad..c8d29283b28989c942ac9b00d51c1311717876fa 100644 (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)
+       }
+}