1
0
mirror of https://github.com/golang/go synced 2024-09-29 16:14:28 -06:00

os: if dirFS.Open fails, undo use of backslashes in error message

This fixes a bug introduced by CL 426094 that caused the
golang.org/x/website/internal/web tests to fail.

Fixes #56034

Change-Id: Ic64967c6d440ad260b7283a18972b20023320ab6
Reviewed-on: https://go-review.googlesource.com/c/go/+/437976
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ian Lance Taylor 2022-10-03 20:08:11 -07:00 committed by Gopher Robot
parent c433cf1893
commit 755a2927d8
2 changed files with 21 additions and 2 deletions

View File

@ -644,6 +644,13 @@ func (dir dirFS) Open(name string) (fs.File, error) {
}
f, err := Open(dir.join(name))
if err != nil {
if runtime.GOOS == "windows" {
// Undo the backslash conversion done by dir.join.
perr := err.(*PathError)
if containsAny(perr.Path, `\`) {
perr.Path = string(dir) + "/" + name
}
}
return nil, err // nil fs.File
}
return f, nil

View File

@ -2712,13 +2712,25 @@ func TestDirFS(t *testing.T) {
t.Fatal(err)
}
}
if err := fstest.TestFS(DirFS("./testdata/dirfs"), "a", "b", "dir/x"); err != nil {
fs := DirFS("./testdata/dirfs")
if err := fstest.TestFS(fs, "a", "b", "dir/x"); err != nil {
t.Fatal(err)
}
// Test that the error message does not contain a backslash.
const nonesuch = "dir/nonesuch"
_, err := fs.Open(nonesuch)
if err == nil {
t.Error("fs.Open of nonexistent file succeeded")
} else {
if !strings.Contains(err.Error(), nonesuch) {
t.Errorf("error %q does not contain %q", err, nonesuch)
}
}
// Test that Open does not accept backslash as separator.
d := DirFS(".")
_, err := d.Open(`testdata\dirfs`)
_, err = d.Open(`testdata\dirfs`)
if err == nil {
t.Fatalf(`Open testdata\dirfs succeeded`)
}