@@ -37,12 +37,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
37
37
perm fs.FileMode = 0700
38
38
)
39
39
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 ()
46
41
for i , d := range tt .result {
47
42
if d == "" {
48
43
continue
@@ -57,12 +52,12 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
57
52
t .Errorf ("%d,%d: %#q already exists" , ti , i , d )
58
53
return
59
54
}
60
- if err = os .MkdirAll (dd , perm ); err != nil {
55
+ if err : = os .MkdirAll (dd , perm ); err != nil {
61
56
t .Errorf ("%d,%d: MkdirAll(%#q) failed: %v" , ti , i , dd , err )
62
57
return
63
58
}
64
59
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 {
66
61
t .Errorf ("%d,%d: WriteFile(%#q) failed: %v" , ti , i , fn , err )
67
62
return
68
63
}
@@ -103,18 +98,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
103
98
func TestWindowsEvalSymlinks (t * testing.T ) {
104
99
testenv .MustHaveSymlink (t )
105
100
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 )
118
102
119
103
if len (tmpDir ) < 3 {
120
104
t .Fatalf ("tmpDir path %q is too short" , tmpDir )
@@ -161,18 +145,7 @@ func TestWindowsEvalSymlinks(t *testing.T) {
161
145
// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
162
146
// returns "canonical" path names on windows.
163
147
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 )
176
149
dirs := []string {
177
150
"test" ,
178
151
"test/dir" ,
@@ -181,7 +154,7 @@ func TestEvalSymlinksCanonicalNames(t *testing.T) {
181
154
}
182
155
183
156
for _ , d := range dirs {
184
- dir := filepath .Join (cTmpName , d )
157
+ dir := filepath .Join (ctmp , d )
185
158
err := os .Mkdir (dir , 0755 )
186
159
if err != nil {
187
160
t .Fatal (err )
@@ -417,25 +390,8 @@ func TestToNorm(t *testing.T) {
417
390
{"." , `\\localhost\c$` , `\\localhost\c$` },
418
391
}
419
392
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 {
439
395
t .Fatal (err )
440
396
}
441
397
@@ -526,20 +482,10 @@ func TestNTNamespaceSymlink(t *testing.T) {
526
482
t .Skip ("skipping test because mklink command does not support junctions" )
527
483
}
528
484
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 )
540
486
541
487
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 ()
543
489
if err != nil {
544
490
t .Fatalf ("failed to run mountvol %v /L: %v %q" , vol , err , output )
545
491
}
0 commit comments