@@ -544,30 +544,18 @@ func chdir(t *testing.T, dir string) {
544
544
545
545
t .Cleanup (func () {
546
546
if err := os .Chdir (olddir ); err != nil {
547
- t .Errorf ("restore original working directory %s: %v" , olddir , err )
548
- os .Exit (1 )
547
+ t .Fatalf ("restore original working directory %s: %v" , olddir , err )
549
548
}
550
549
})
551
550
}
552
551
553
- func chtmpdir (t * testing.T ) (restore func ()) {
554
- oldwd , err := os .Getwd ()
555
- if err != nil {
556
- t .Fatalf ("chtmpdir: %v" , err )
557
- }
558
- d , err := os .MkdirTemp ("" , "test" )
559
- if err != nil {
560
- t .Fatalf ("chtmpdir: %v" , err )
561
- }
562
- if err := os .Chdir (d ); err != nil {
563
- t .Fatalf ("chtmpdir: %v" , err )
564
- }
565
- return func () {
566
- if err := os .Chdir (oldwd ); err != nil {
567
- t .Fatalf ("chtmpdir: %v" , err )
568
- }
552
+ func chtmpdir (t * testing.T ) string {
553
+ d := t .TempDir ()
554
+ chdir (t , d )
555
+ t .Cleanup (func () {
569
556
os .RemoveAll (d )
570
- }
557
+ })
558
+ return d
571
559
}
572
560
573
561
// tempDirCanonical returns a temporary directory for the test to use, ensuring
@@ -597,21 +585,7 @@ func TestWalkDir(t *testing.T) {
597
585
}
598
586
599
587
func testWalk (t * testing.T , walk func (string , fs.WalkDirFunc ) error , errVisit int ) {
600
- if runtime .GOOS == "ios" {
601
- restore := chtmpdir (t )
602
- defer restore ()
603
- }
604
-
605
- tmpDir := t .TempDir ()
606
-
607
- origDir , err := os .Getwd ()
608
- if err != nil {
609
- t .Fatal ("finding working dir:" , err )
610
- }
611
- if err = os .Chdir (tmpDir ); err != nil {
612
- t .Fatal ("entering temp dir:" , err )
613
- }
614
- defer os .Chdir (origDir )
588
+ chtmpdir (t )
615
589
616
590
makeTree (t )
617
591
errors := make ([]error , 0 , 10 )
@@ -620,7 +594,7 @@ func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit in
620
594
return mark (d , err , & errors , clear )
621
595
}
622
596
// Expect no errors.
623
- err = walk (tree .name , markFn )
597
+ err : = walk (tree .name , markFn )
624
598
if err != nil {
625
599
t .Fatalf ("no error expected, found: %s" , err )
626
600
}
@@ -1249,7 +1223,7 @@ func TestEvalSymlinks(t *testing.T) {
1249
1223
func TestEvalSymlinksIsNotExist (t * testing.T ) {
1250
1224
testenv .MustHaveSymlink (t )
1251
1225
1252
- defer chtmpdir (t )( )
1226
+ chtmpdir (t )
1253
1227
1254
1228
_ , err := filepath .EvalSymlinks ("notexist" )
1255
1229
if ! os .IsNotExist (err ) {
@@ -1332,8 +1306,7 @@ func TestRelativeSymlinkToAbsolute(t *testing.T) {
1332
1306
testenv .MustHaveSymlink (t )
1333
1307
// Not parallel: uses os.Chdir.
1334
1308
1335
- tmpDir := t .TempDir ()
1336
- chdir (t , tmpDir )
1309
+ tmpDir := chtmpdir (t )
1337
1310
1338
1311
// Create "link" in the current working directory as a symlink to an arbitrary
1339
1312
// absolute path. On macOS, this path is likely to begin with a symlink
@@ -1385,19 +1358,10 @@ var absTests = []string{
1385
1358
}
1386
1359
1387
1360
func TestAbs (t * testing.T ) {
1388
- root := t .TempDir ()
1389
- wd , err := os .Getwd ()
1390
- if err != nil {
1391
- t .Fatal ("getwd failed: " , err )
1392
- }
1393
- err = os .Chdir (root )
1394
- if err != nil {
1395
- t .Fatal ("chdir failed: " , err )
1396
- }
1397
- defer os .Chdir (wd )
1361
+ root := chtmpdir (t )
1398
1362
1399
1363
for _ , dir := range absTestDirs {
1400
- err = os .Mkdir (dir , 0777 )
1364
+ err : = os .Mkdir (dir , 0777 )
1401
1365
if err != nil {
1402
1366
t .Fatal ("Mkdir failed: " , err )
1403
1367
}
@@ -1416,7 +1380,7 @@ func TestAbs(t *testing.T) {
1416
1380
absTests = append (absTests , extra ... )
1417
1381
}
1418
1382
1419
- err = os .Chdir (absTestDirs [0 ])
1383
+ err : = os .Chdir (absTestDirs [0 ])
1420
1384
if err != nil {
1421
1385
t .Fatal ("chdir failed: " , err )
1422
1386
}
@@ -1451,17 +1415,7 @@ func TestAbs(t *testing.T) {
1451
1415
// We test it separately from all other absTests because the empty string is not
1452
1416
// a valid path, so it can't be used with os.Stat.
1453
1417
func TestAbsEmptyString (t * testing.T ) {
1454
- root := t .TempDir ()
1455
-
1456
- wd , err := os .Getwd ()
1457
- if err != nil {
1458
- t .Fatal ("getwd failed: " , err )
1459
- }
1460
- err = os .Chdir (root )
1461
- if err != nil {
1462
- t .Fatal ("chdir failed: " , err )
1463
- }
1464
- defer os .Chdir (wd )
1418
+ root := chtmpdir (t )
1465
1419
1466
1420
info , err := os .Stat (root )
1467
1421
if err != nil {
@@ -1687,20 +1641,9 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
1687
1641
}
1688
1642
1689
1643
func testWalkSymlink (t * testing.T , mklink func (target , link string ) error ) {
1690
- tmpdir := t .TempDir ()
1691
-
1692
- wd , err := os .Getwd ()
1693
- if err != nil {
1694
- t .Fatal (err )
1695
- }
1696
- defer os .Chdir (wd )
1697
-
1698
- err = os .Chdir (tmpdir )
1699
- if err != nil {
1700
- t .Fatal (err )
1701
- }
1644
+ tmpdir := chtmpdir (t )
1702
1645
1703
- err = mklink (tmpdir , "link" )
1646
+ err : = mklink (tmpdir , "link" )
1704
1647
if err != nil {
1705
1648
t .Fatal (err )
1706
1649
}
@@ -1814,12 +1757,7 @@ func TestEvalSymlinksAboveRoot(t *testing.T) {
1814
1757
func TestEvalSymlinksAboveRootChdir (t * testing.T ) {
1815
1758
testenv .MustHaveSymlink (t )
1816
1759
1817
- tmpDir , err := os .MkdirTemp ("" , "TestEvalSymlinksAboveRootChdir" )
1818
- if err != nil {
1819
- t .Fatal (err )
1820
- }
1821
- defer os .RemoveAll (tmpDir )
1822
- chdir (t , tmpDir )
1760
+ chtmpdir (t )
1823
1761
1824
1762
subdir := filepath .Join ("a" , "b" )
1825
1763
if err := os .MkdirAll (subdir , 0777 ); err != nil {
0 commit comments