aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/path
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2021-07-30 14:28:58 -0700
committerIan Lance Taylor <iant@golang.org>2021-08-12 20:23:07 -0700
commitc5b21c3f4c17b0649155035d2f9aa97b2da8a813 (patch)
treec6d3a68b503ba5b16182acbb958e3e5dbc95a43b /libgo/go/path
parent72be20e20299ec57b4bc9ba03d5b7d6bf10e97cc (diff)
downloadgcc-c5b21c3f4c17b0649155035d2f9aa97b2da8a813.zip
gcc-c5b21c3f4c17b0649155035d2f9aa97b2da8a813.tar.gz
gcc-c5b21c3f4c17b0649155035d2f9aa97b2da8a813.tar.bz2
libgo: update to Go1.17rc2
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/341629
Diffstat (limited to 'libgo/go/path')
-rw-r--r--libgo/go/path/filepath/example_unix_test.go1
-rw-r--r--libgo/go/path/filepath/example_unix_walk_test.go1
-rw-r--r--libgo/go/path/filepath/match_test.go29
-rw-r--r--libgo/go/path/filepath/path.go6
-rw-r--r--libgo/go/path/filepath/path_test.go125
-rw-r--r--libgo/go/path/filepath/path_unix.go1
-rw-r--r--libgo/go/path/filepath/symlink_unix.go1
7 files changed, 71 insertions, 93 deletions
diff --git a/libgo/go/path/filepath/example_unix_test.go b/libgo/go/path/filepath/example_unix_test.go
index c9d6944..4ce1009 100644
--- a/libgo/go/path/filepath/example_unix_test.go
+++ b/libgo/go/path/filepath/example_unix_test.go
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+//go:build !windows && !plan9
// +build !windows,!plan9
package filepath_test
diff --git a/libgo/go/path/filepath/example_unix_walk_test.go b/libgo/go/path/filepath/example_unix_walk_test.go
index c8a818f..d72efce 100644
--- a/libgo/go/path/filepath/example_unix_walk_test.go
+++ b/libgo/go/path/filepath/example_unix_walk_test.go
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+//go:build !windows && !plan9
// +build !windows,!plan9
package filepath_test
diff --git a/libgo/go/path/filepath/match_test.go b/libgo/go/path/filepath/match_test.go
index 0204225..3d90982 100644
--- a/libgo/go/path/filepath/match_test.go
+++ b/libgo/go/path/filepath/match_test.go
@@ -182,12 +182,7 @@ var globSymlinkTests = []struct {
func TestGlobSymlink(t *testing.T) {
testenv.MustHaveSymlink(t)
- tmpDir, err := os.MkdirTemp("", "globsymlink")
- if err != nil {
- t.Fatal("creating temp dir:", err)
- }
- defer os.RemoveAll(tmpDir)
-
+ tmpDir := t.TempDir()
for _, tt := range globSymlinkTests {
path := Join(tmpDir, tt.path)
dest := Join(tmpDir, tt.dest)
@@ -268,18 +263,7 @@ func TestWindowsGlob(t *testing.T) {
t.Skipf("skipping windows specific test")
}
- tmpDir, err := os.MkdirTemp("", "TestWindowsGlob")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
-
- // /tmp may itself be a symlink
- tmpDir, err = EvalSymlinks(tmpDir)
- if err != nil {
- t.Fatal("eval symlink for tmp dir:", err)
- }
-
+ tmpDir := tempDirCanonical(t)
if len(tmpDir) < 3 {
t.Fatalf("tmpDir path %q is too short", tmpDir)
}
@@ -324,15 +308,13 @@ func TestWindowsGlob(t *testing.T) {
// test absolute paths
for _, test := range tests {
var p string
- err = test.globAbs(tmpDir, tmpDir)
- if err != nil {
+ if err := test.globAbs(tmpDir, tmpDir); err != nil {
t.Error(err)
}
// test C:\*Documents and Settings\...
p = tmpDir
p = strings.Replace(p, `:\`, `:\*`, 1)
- err = test.globAbs(tmpDir, p)
- if err != nil {
+ if err := test.globAbs(tmpDir, p); err != nil {
t.Error(err)
}
// test C:\Documents and Settings*\...
@@ -340,8 +322,7 @@ func TestWindowsGlob(t *testing.T) {
p = strings.Replace(p, `:\`, `:`, 1)
p = strings.Replace(p, `\`, `*\`, 1)
p = strings.Replace(p, `:`, `:\`, 1)
- err = test.globAbs(tmpDir, p)
- if err != nil {
+ if err := test.globAbs(tmpDir, p); err != nil {
t.Error(err)
}
}
diff --git a/libgo/go/path/filepath/path.go b/libgo/go/path/filepath/path.go
index 2e7b439..b56534d 100644
--- a/libgo/go/path/filepath/path.go
+++ b/libgo/go/path/filepath/path.go
@@ -275,7 +275,11 @@ func Rel(basepath, targpath string) (string, error) {
targ = targ[len(targVol):]
if base == "." {
base = ""
+ } else if base == "" && volumeNameLen(baseVol) > 2 /* isUNC */ {
+ // Treat any targetpath matching `\\host\share` basepath as absolute path.
+ base = string(Separator)
}
+
// Can't use IsAbs - `\a` and `a` are both relative in Windows.
baseSlashed := len(base) > 0 && base[0] == Separator
targSlashed := len(targ) > 0 && targ[0] == Separator
@@ -336,7 +340,7 @@ func Rel(basepath, targpath string) (string, error) {
// as an error by any function.
var SkipDir error = fs.SkipDir
-// WalkFunc is the type of the function called by Walk to visit each each
+// WalkFunc is the type of the function called by Walk to visit each
// file or directory.
//
// The path argument contains the argument to Walk as a prefix.
diff --git a/libgo/go/path/filepath/path_test.go b/libgo/go/path/filepath/path_test.go
index 51ea684..1be17df 100644
--- a/libgo/go/path/filepath/path_test.go
+++ b/libgo/go/path/filepath/path_test.go
@@ -413,6 +413,25 @@ func mark(d fs.DirEntry, err error, errors *[]error, clear bool) error {
return nil
}
+// chdir changes the current working directory to the named directory,
+// and then restore the original working directory at the end of the test.
+func chdir(t *testing.T, dir string) {
+ olddir, err := os.Getwd()
+ if err != nil {
+ t.Fatalf("getwd %s: %v", dir, err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ t.Fatalf("chdir %s: %v", dir, err)
+ }
+
+ t.Cleanup(func() {
+ if err := os.Chdir(olddir); err != nil {
+ t.Errorf("restore original working directory %s: %v", olddir, err)
+ os.Exit(1)
+ }
+ })
+}
+
func chtmpdir(t *testing.T) (restore func()) {
oldwd, err := os.Getwd()
if err != nil {
@@ -433,6 +452,19 @@ func chtmpdir(t *testing.T) (restore func()) {
}
}
+// tempDirCanonical returns a temporary directory for the test to use, ensuring
+// that the returned path does not contain symlinks.
+func tempDirCanonical(t *testing.T) string {
+ dir := t.TempDir()
+
+ cdir, err := filepath.EvalSymlinks(dir)
+ if err != nil {
+ t.Errorf("tempDirCanonical: %v", err)
+ }
+
+ return cdir
+}
+
func TestWalk(t *testing.T) {
walk := func(root string, fn fs.WalkDirFunc) error {
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
@@ -461,11 +493,7 @@ func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit in
defer restore()
}
- tmpDir, err := os.MkdirTemp("", "TestWalk")
- if err != nil {
- t.Fatal("creating temp dir:", err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
origDir, err := os.Getwd()
if err != nil {
@@ -565,11 +593,7 @@ func touch(t *testing.T, name string) {
}
func TestWalkSkipDirOnFile(t *testing.T) {
- td, err := os.MkdirTemp("", "walktest")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(td)
+ td := t.TempDir()
if err := os.MkdirAll(filepath.Join(td, "dir"), 0755); err != nil {
t.Fatal(err)
@@ -593,7 +617,7 @@ func TestWalkSkipDirOnFile(t *testing.T) {
check := func(t *testing.T, walk func(root string) error, root string) {
t.Helper()
sawFoo2 = false
- err = walk(root)
+ err := walk(root)
if err != nil {
t.Fatal(err)
}
@@ -615,11 +639,7 @@ func TestWalkSkipDirOnFile(t *testing.T) {
}
func TestWalkFileError(t *testing.T) {
- td, err := os.MkdirTemp("", "walktest")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(td)
+ td := t.TempDir()
touch(t, filepath.Join(td, "foo"))
touch(t, filepath.Join(td, "bar"))
@@ -640,7 +660,7 @@ func TestWalkFileError(t *testing.T) {
return os.Lstat(path)
}
got := map[string]error{}
- err = filepath.Walk(td, func(path string, fi fs.FileInfo, err error) error {
+ err := filepath.Walk(td, func(path string, fi fs.FileInfo, err error) error {
rel, _ := filepath.Rel(td, path)
got[filepath.ToSlash(rel)] = err
return nil
@@ -894,14 +914,11 @@ func testEvalSymlinksAfterChdir(t *testing.T, wd, path, want string) {
func TestEvalSymlinks(t *testing.T) {
testenv.MustHaveSymlink(t)
- tmpDir, err := os.MkdirTemp("", "evalsymlink")
- if err != nil {
- t.Fatal("creating temp dir:", err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
// /tmp may itself be a symlink! Avoid the confusion, although
// it means trusting the thing we're testing.
+ var err error
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal("eval symlink for tmp dir:", err)
@@ -980,14 +997,10 @@ func TestEvalSymlinksIsNotExist(t *testing.T) {
func TestIssue13582(t *testing.T) {
testenv.MustHaveSymlink(t)
- tmpDir, err := os.MkdirTemp("", "issue13582")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
dir := filepath.Join(tmpDir, "dir")
- err = os.Mkdir(dir, 0755)
+ err := os.Mkdir(dir, 0755)
if err != nil {
t.Fatal(err)
}
@@ -1067,12 +1080,7 @@ var absTests = []string{
}
func TestAbs(t *testing.T) {
- root, err := os.MkdirTemp("", "TestAbs")
- if err != nil {
- t.Fatal("TempDir failed: ", err)
- }
- defer os.RemoveAll(root)
-
+ root := t.TempDir()
wd, err := os.Getwd()
if err != nil {
t.Fatal("getwd failed: ", err)
@@ -1138,11 +1146,7 @@ func TestAbs(t *testing.T) {
// We test it separately from all other absTests because the empty string is not
// a valid path, so it can't be used with os.Stat.
func TestAbsEmptyString(t *testing.T) {
- root, err := os.MkdirTemp("", "TestAbsEmptyString")
- if err != nil {
- t.Fatal("TempDir failed: ", err)
- }
- defer os.RemoveAll(root)
+ root := t.TempDir()
wd, err := os.Getwd()
if err != nil {
@@ -1230,6 +1234,7 @@ var winreltests = []RelTests{
{`C:\Projects`, `c:\projects\src`, `src`},
{`C:\Projects`, `c:\projects`, `.`},
{`C:\Projects\a\..`, `c:\projects`, `.`},
+ {`\\host\share`, `\\host\share\file.txt`, `file.txt`},
}
func TestRel(t *testing.T) {
@@ -1360,11 +1365,7 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
}
func testWalkSymlink(t *testing.T, mklink func(target, link string) error) {
- tmpdir, err := os.MkdirTemp("", "testWalkSymlink")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
+ tmpdir := t.TempDir()
wd, err := os.Getwd()
if err != nil {
@@ -1410,14 +1411,10 @@ func TestWalkSymlink(t *testing.T) {
}
func TestIssue29372(t *testing.T) {
- tmpDir, err := os.MkdirTemp("", "TestIssue29372")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
path := filepath.Join(tmpDir, "file.txt")
- err = os.WriteFile(path, nil, 0644)
+ err := os.WriteFile(path, nil, 0644)
if err != nil {
t.Fatal(err)
}
@@ -1446,11 +1443,7 @@ func TestEvalSymlinksAboveRoot(t *testing.T) {
t.Parallel()
- tmpDir, err := os.MkdirTemp("", "TestEvalSymlinksAboveRoot")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
evalTmpDir, err := filepath.EvalSymlinks(tmpDir)
if err != nil {
@@ -1480,11 +1473,16 @@ func TestEvalSymlinksAboveRoot(t *testing.T) {
// Try different numbers of "..".
for _, i := range []int{c, c + 1, c + 2} {
check := strings.Join([]string{evalTmpDir, strings.Join(dd[:i], string(os.PathSeparator)), evalTmpDir[len(vol)+1:], "b", "file"}, string(os.PathSeparator))
- if resolved, err := filepath.EvalSymlinks(check); err != nil {
+ resolved, err := filepath.EvalSymlinks(check)
+ switch {
+ case runtime.GOOS == "darwin" && errors.Is(err, fs.ErrNotExist):
+ // On darwin, the temp dir is sometimes cleaned up mid-test (issue 37910).
+ testenv.SkipFlaky(t, 37910)
+ case err != nil:
t.Errorf("EvalSymlinks(%q) failed: %v", check, err)
- } else if !strings.HasSuffix(resolved, wantSuffix) {
+ case !strings.HasSuffix(resolved, wantSuffix):
t.Errorf("EvalSymlinks(%q) = %q does not end with %q", check, resolved, wantSuffix)
- } else {
+ default:
t.Logf("EvalSymlinks(%q) = %q", check, resolved)
}
}
@@ -1499,16 +1497,7 @@ func TestEvalSymlinksAboveRootChdir(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
-
- if err := os.Chdir(tmpDir); err != nil {
- t.Fatal(err)
- }
+ chdir(t, tmpDir)
subdir := filepath.Join("a", "b")
if err := os.MkdirAll(subdir, 0777); err != nil {
diff --git a/libgo/go/path/filepath/path_unix.go b/libgo/go/path/filepath/path_unix.go
index 57341ed..b0a6391 100644
--- a/libgo/go/path/filepath/path_unix.go
+++ b/libgo/go/path/filepath/path_unix.go
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+//go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd hurd js,wasm linux netbsd openbsd solaris
package filepath
diff --git a/libgo/go/path/filepath/symlink_unix.go b/libgo/go/path/filepath/symlink_unix.go
index d20e63a..657945a 100644
--- a/libgo/go/path/filepath/symlink_unix.go
+++ b/libgo/go/path/filepath/symlink_unix.go
@@ -1,3 +1,4 @@
+//go:build !windows
// +build !windows
package filepath