Skip to content

Commit 11f1594

Browse files
perillotklauser
authored andcommitted
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 <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]>
1 parent 6382ec1 commit 11f1594

File tree

3 files changed

+28
-88
lines changed

3 files changed

+28
-88
lines changed

src/path/filepath/match_test.go

+5-24
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,7 @@ var globSymlinkTests = []struct {
181181
func TestGlobSymlink(t *testing.T) {
182182
testenv.MustHaveSymlink(t)
183183

184-
tmpDir, err := os.MkdirTemp("", "globsymlink")
185-
if err != nil {
186-
t.Fatal("creating temp dir:", err)
187-
}
188-
defer os.RemoveAll(tmpDir)
189-
184+
tmpDir := t.TempDir()
190185
for _, tt := range globSymlinkTests {
191186
path := Join(tmpDir, tt.path)
192187
dest := Join(tmpDir, tt.dest)
@@ -267,18 +262,7 @@ func TestWindowsGlob(t *testing.T) {
267262
t.Skipf("skipping windows specific test")
268263
}
269264

270-
tmpDir, err := os.MkdirTemp("", "TestWindowsGlob")
271-
if err != nil {
272-
t.Fatal(err)
273-
}
274-
defer os.RemoveAll(tmpDir)
275-
276-
// /tmp may itself be a symlink
277-
tmpDir, err = EvalSymlinks(tmpDir)
278-
if err != nil {
279-
t.Fatal("eval symlink for tmp dir:", err)
280-
}
281-
265+
tmpDir := tempDirCanonical(t)
282266
if len(tmpDir) < 3 {
283267
t.Fatalf("tmpDir path %q is too short", tmpDir)
284268
}
@@ -323,24 +307,21 @@ func TestWindowsGlob(t *testing.T) {
323307
// test absolute paths
324308
for _, test := range tests {
325309
var p string
326-
err = test.globAbs(tmpDir, tmpDir)
327-
if err != nil {
310+
if err := test.globAbs(tmpDir, tmpDir); err != nil {
328311
t.Error(err)
329312
}
330313
// test C:\*Documents and Settings\...
331314
p = tmpDir
332315
p = strings.Replace(p, `:\`, `:\*`, 1)
333-
err = test.globAbs(tmpDir, p)
334-
if err != nil {
316+
if err := test.globAbs(tmpDir, p); err != nil {
335317
t.Error(err)
336318
}
337319
// test C:\Documents and Settings*\...
338320
p = tmpDir
339321
p = strings.Replace(p, `:\`, `:`, 1)
340322
p = strings.Replace(p, `\`, `*\`, 1)
341323
p = strings.Replace(p, `:`, `:\`, 1)
342-
err = test.globAbs(tmpDir, p)
343-
if err != nil {
324+
if err := test.globAbs(tmpDir, p); err != nil {
344325
t.Error(err)
345326
}
346327
}

src/path/filepath/path_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,19 @@ func chtmpdir(t *testing.T) (restore func()) {
449449
}
450450
}
451451

452+
// tempDirCanonical returns a temporary directory for the test to use, ensuring
453+
// that the returned path does not contain symlinks.
454+
func tempDirCanonical(t *testing.T) string {
455+
dir := t.TempDir()
456+
457+
cdir, err := filepath.EvalSymlinks(dir)
458+
if err != nil {
459+
t.Errorf("tempDirCanonical: %v", err)
460+
}
461+
462+
return cdir
463+
}
464+
452465
func TestWalk(t *testing.T) {
453466
walk := func(root string, fn fs.WalkDirFunc) error {
454467
return filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {

src/path/filepath/path_windows_test.go

+10-64
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
3737
perm fs.FileMode = 0700
3838
)
3939

40-
tmp, err := os.MkdirTemp("", "testWinSplitListTestIsValid")
41-
if err != nil {
42-
t.Fatalf("TempDir failed: %v", err)
43-
}
44-
defer os.RemoveAll(tmp)
45-
40+
tmp := t.TempDir()
4641
for i, d := range tt.result {
4742
if d == "" {
4843
continue
@@ -57,12 +52,12 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
5752
t.Errorf("%d,%d: %#q already exists", ti, i, d)
5853
return
5954
}
60-
if err = os.MkdirAll(dd, perm); err != nil {
55+
if err := os.MkdirAll(dd, perm); err != nil {
6156
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
6257
return
6358
}
6459
fn, data := filepath.Join(dd, cmdfile), []byte("@echo "+d+"\r\n")
65-
if err = os.WriteFile(fn, data, perm); err != nil {
60+
if err := os.WriteFile(fn, data, perm); err != nil {
6661
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
6762
return
6863
}
@@ -103,18 +98,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
10398
func TestWindowsEvalSymlinks(t *testing.T) {
10499
testenv.MustHaveSymlink(t)
105100

106-
tmpDir, err := os.MkdirTemp("", "TestWindowsEvalSymlinks")
107-
if err != nil {
108-
t.Fatal(err)
109-
}
110-
defer os.RemoveAll(tmpDir)
111-
112-
// /tmp may itself be a symlink! Avoid the confusion, although
113-
// it means trusting the thing we're testing.
114-
tmpDir, err = filepath.EvalSymlinks(tmpDir)
115-
if err != nil {
116-
t.Fatal(err)
117-
}
101+
tmpDir := tempDirCanonical(t)
118102

119103
if len(tmpDir) < 3 {
120104
t.Fatalf("tmpDir path %q is too short", tmpDir)
@@ -161,18 +145,7 @@ func TestWindowsEvalSymlinks(t *testing.T) {
161145
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
162146
// returns "canonical" path names on windows.
163147
func TestEvalSymlinksCanonicalNames(t *testing.T) {
164-
tmp, err := os.MkdirTemp("", "evalsymlinkcanonical")
165-
if err != nil {
166-
t.Fatal("creating temp dir:", err)
167-
}
168-
defer os.RemoveAll(tmp)
169-
170-
// os.MkdirTemp might return "non-canonical" name.
171-
cTmpName, err := filepath.EvalSymlinks(tmp)
172-
if err != nil {
173-
t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
174-
}
175-
148+
ctmp := tempDirCanonical(t)
176149
dirs := []string{
177150
"test",
178151
"test/dir",
@@ -181,7 +154,7 @@ func TestEvalSymlinksCanonicalNames(t *testing.T) {
181154
}
182155

183156
for _, d := range dirs {
184-
dir := filepath.Join(cTmpName, d)
157+
dir := filepath.Join(ctmp, d)
185158
err := os.Mkdir(dir, 0755)
186159
if err != nil {
187160
t.Fatal(err)
@@ -417,25 +390,8 @@ func TestToNorm(t *testing.T) {
417390
{".", `\\localhost\c$`, `\\localhost\c$`},
418391
}
419392

420-
tmp, err := os.MkdirTemp("", "testToNorm")
421-
if err != nil {
422-
t.Fatal(err)
423-
}
424-
defer func() {
425-
err := os.RemoveAll(tmp)
426-
if err != nil {
427-
t.Fatal(err)
428-
}
429-
}()
430-
431-
// os.MkdirTemp might return "non-canonical" name.
432-
ctmp, err := filepath.EvalSymlinks(tmp)
433-
if err != nil {
434-
t.Fatal(err)
435-
}
436-
437-
err = os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777)
438-
if err != nil {
393+
ctmp := tempDirCanonical(t)
394+
if err := os.MkdirAll(strings.ReplaceAll(testPath, "{{tmp}}", ctmp), 0777); err != nil {
439395
t.Fatal(err)
440396
}
441397

@@ -526,20 +482,10 @@ func TestNTNamespaceSymlink(t *testing.T) {
526482
t.Skip("skipping test because mklink command does not support junctions")
527483
}
528484

529-
tmpdir, err := os.MkdirTemp("", "TestNTNamespaceSymlink")
530-
if err != nil {
531-
t.Fatal(err)
532-
}
533-
defer os.RemoveAll(tmpdir)
534-
535-
// Make sure tmpdir is not a symlink, otherwise tests will fail.
536-
tmpdir, err = filepath.EvalSymlinks(tmpdir)
537-
if err != nil {
538-
t.Fatal(err)
539-
}
485+
tmpdir := tempDirCanonical(t)
540486

541487
vol := filepath.VolumeName(tmpdir)
542-
output, err = exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
488+
output, err := exec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
543489
if err != nil {
544490
t.Fatalf("failed to run mountvol %v /L: %v %q", vol, err, output)
545491
}

0 commit comments

Comments
 (0)