Fixes #64366
Change-Id: Ie78ab2cb9e11c0766665cd37fd7a26d36a1c24fa
GitHub-Last-Rev:
3cb3bb84a0ccccc88ba30258f52f699ac1364a7b
GitHub-Pull-Request: golang/go#64372
Reviewed-on: https://go-review.googlesource.com/c/go/+/544835
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Jes Cok <xigua67damn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
package fs_test
import (
+ "errors"
. "io/fs"
"os"
"testing"
})
}
}
+
+func errorPath(err error) string {
+ var perr *PathError
+ if !errors.As(err, &perr) {
+ return ""
+ }
+ return perr.Path
+}
+
+func TestReadDirPath(t *testing.T) {
+ fsys := os.DirFS(t.TempDir())
+ _, err1 := ReadDir(fsys, "non-existent")
+ _, err2 := ReadDir(struct{ FS }{fsys}, "non-existent")
+ if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
+ t.Fatalf("s1: %s != s2: %s", s1, s2)
+ }
+}
import (
. "io/fs"
+ "os"
"testing"
"testing/fstest"
"time"
t.Fatalf(`ReadFile(sub(.), "hello.txt") = %q, %v, want %q, nil`, data, err, "hello, world")
}
}
+
+func TestReadFilePath(t *testing.T) {
+ fsys := os.DirFS(t.TempDir())
+ _, err1 := ReadFile(fsys, "non-existent")
+ _, err2 := ReadFile(struct{ FS }{fsys}, "non-existent")
+ if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
+ t.Fatalf("s1: %s != s2: %s", s1, s2)
+ }
+}
if err != nil {
return nil, &PathError{Op: "readfile", Path: name, Err: err}
}
- return ReadFile(fullname)
+ b, err := ReadFile(fullname)
+ if err != nil {
+ if e, ok := err.(*PathError); ok {
+ // See comment in dirFS.Open.
+ e.Path = name
+ }
+ return nil, err
+ }
+ return b, nil
}
// ReadDir reads the named directory, returning all its directory entries sorted
if err != nil {
return nil, &PathError{Op: "readdir", Path: name, Err: err}
}
- return ReadDir(fullname)
+ entries, err := ReadDir(fullname)
+ if err != nil {
+ if e, ok := err.(*PathError); ok {
+ // See comment in dirFS.Open.
+ e.Path = name
+ }
+ return nil, err
+ }
+ return entries, nil
}
func (dir dirFS) Stat(name string) (fs.FileInfo, error) {