aboutsummaryrefslogtreecommitdiff
path: root/src/path/filepath/path_windows_test.go
diff options
context:
space:
mode:
authorManlio Perillo <manlio.perillo@gmail.com>2021-04-07 16:48:41 +0200
committerTobias Klauser <tobias.klauser@gmail.com>2021-04-10 08:13:32 +0000
commit11f159456b1dba3ec499da916852dd188d1e04a7 (patch)
tree6db4787c1913a1df154b01091c940e82f136169c /src/path/filepath/path_windows_test.go
parent6382ec1aba1b1c7380cb525217c1bd645c4fd41b (diff)
downloadgo-11f159456b1dba3ec499da916852dd188d1e04a7.tar.xz
path/filepath: replace os.MkdirTemp with T.TempDir
Add the tempDirCanonical function, for tests that need a temporary directory that does not contain symlinks. Updates #45402 Change-Id: I3d08ef32ef911331544acce3d7d013b4c3382960 Reviewed-on: https://go-review.googlesource.com/c/go/+/308011 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Diffstat (limited to 'src/path/filepath/path_windows_test.go')
-rw-r--r--src/path/filepath/path_windows_test.go74
1 files changed, 10 insertions, 64 deletions
diff --git a/src/path/filepath/path_windows_test.go b/src/path/filepath/path_windows_test.go
index 1c3d84c62d..76a459ac96 100644
--- a/src/path/filepath/path_windows_test.go
+++ b/src/path/filepath/path_windows_test.go
@@ -37,12 +37,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
perm fs.FileMode = 0700
)
- tmp, err := os.MkdirTemp("", "testWinSplitListTestIsValid")
- if err != nil {
- t.Fatalf("TempDir failed: %v", err)
- }
- defer os.RemoveAll(tmp)
-
+ tmp := t.TempDir()
for i, d := range tt.result {
if d == "" {
continue
@@ -57,12 +52,12 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
t.Errorf("%d,%d: %#q already exists", ti, i, d)
return
}
- if err = os.MkdirAll(dd, perm); err != nil {
+ if err := os.MkdirAll(dd, perm); err != nil {
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
return
}
fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
- if err = os.WriteFile(fn, data, perm); err != nil {
+ if err := os.WriteFile(fn, data, perm); err != nil {
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
return
}
@@ -103,18 +98,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
func TestWindowsEvalSymlinks(t *testing.T) {
testenv.MustHaveSymlink(t)
- tmpDir, err := os.MkdirTemp("", "TestWindowsEvalSymlinks")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
-
- // /tmp may itself be a symlink! Avoid the confusion, although
- // it means trusting the thing we're testing.
- tmpDir, err = filepath.EvalSymlinks(tmpDir)
- if err != nil {
- t.Fatal(err)
- }
+ tmpDir := tempDirCanonical(t)
if len(tmpDir) < 3 {
t.Fatalf("tmpDir path %q is too short", tmpDir)
@@ -161,18 +145,7 @@ func TestWindowsEvalSymlinks(t *testing.T) {
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
// returns "canonical" path names on windows.
func TestEvalSymlinksCanonicalNames(t *testing.T) {
- tmp, err := os.MkdirTemp("", "evalsymlinkcanonical")
- if err != nil {
- t.Fatal("creating temp dir:", err)
- }
- defer os.RemoveAll(tmp)
-
- // os.MkdirTemp might return "non-canonical" name.
- cTmpName, err := filepath.EvalSymlinks(tmp)
- if err != nil {
- t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
- }
-
+ ctmp := tempDirCanonical(t)
dirs := []string{
"test",
"test/dir",
@@ -181,7 +154,7 @@ func TestEvalSymlinksCanonicalNames(t *testing.T) {
}
for _, d := range dirs {
- dir := filepath.Join(cTmpName, d)
+ dir := filepath.Join(ctmp, d)
err := os.Mkdir(dir, 0755)
if err != nil {
t.Fatal(err)
@@ -417,25 +390,8 @@ func TestToNorm(t *testing.T) {
{".", `\\localhost\c$`, `\\localhost\c$`},
}
- tmp, err := os.MkdirTemp("", "testToNorm")
- if err != nil {
- t.Fatal(err)
- }
- defer func() {
- err := os.RemoveAll(tmp)
- if err != nil {
- t.Fatal(err)
- }
- }()
-
- // os.MkdirTemp might return "non-canonical" name.
- ctmp, err := filepath.EvalSymlinks(tmp)
- if err != nil {
- t.Fatal(err)
- }
-
- err = os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777)
- if err != nil {
+ ctmp := tempDirCanonical(t)
+ if err := os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777); err != nil {
t.Fatal(err)
}
@@ -526,20 +482,10 @@ func TestNTNamespaceSymlink(t *testing.T) {
t.Skip("skipping test because mklink command does not support junctions")
}
- tmpdir, err := os.MkdirTemp("", "TestNTNamespaceSymlink")
- if err != nil {
- t.Fatal(err)
- }
- defer os.RemoveAll(tmpdir)
-
- // Make sure tmpdir is not a symlink, otherwise tests will fail.
- tmpdir, err = filepath.EvalSymlinks(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
+ tmpdir := tempDirCanonical(t)
vol := filepath.VolumeName(tmpdir)
- output, err = exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
+ output, err := exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
if err != nil {
t.Fatalf("failed to run mountvol %v /L: %v %q", vol, err, output)
}