Skip to content

Commit 25ba510

Browse files
committed
path/filepath: simplify chdir and error handling in tests
This change simplifies the test code for changing directories by utilizing t.TempDir() within chtmpdir function. It also replaces os.Exit(1) with t.Fatalf to handle errors more consistently with Go's testing practice. The cleanup process is now managed with t.Cleanup, ensuring that any created temporary directories are removed without the need for a deferred restore function. Updates #45182
1 parent a27a525 commit 25ba510

File tree

2 files changed

+20
-94
lines changed

2 files changed

+20
-94
lines changed

src/path/filepath/match_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -337,20 +337,8 @@ func TestWindowsGlob(t *testing.T) {
337337
}
338338

339339
// test relative paths
340-
wd, err := os.Getwd()
341-
if err != nil {
342-
t.Fatal(err)
343-
}
344-
err = os.Chdir(tmpDir)
345-
if err != nil {
346-
t.Fatal(err)
347-
}
348-
defer func() {
349-
err := os.Chdir(wd)
350-
if err != nil {
351-
t.Fatal(err)
352-
}
353-
}()
340+
chdir(t, tmpDir)
341+
354342
for _, test := range tests {
355343
err := test.globRel("")
356344
if err != nil {

src/path/filepath/path_test.go

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -544,30 +544,18 @@ func chdir(t *testing.T, dir string) {
544544

545545
t.Cleanup(func() {
546546
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)
549548
}
550549
})
551550
}
552551

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() {
569556
os.RemoveAll(d)
570-
}
557+
})
558+
return d
571559
}
572560

573561
// tempDirCanonical returns a temporary directory for the test to use, ensuring
@@ -597,21 +585,7 @@ func TestWalkDir(t *testing.T) {
597585
}
598586

599587
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)
615589

616590
makeTree(t)
617591
errors := make([]error, 0, 10)
@@ -620,7 +594,7 @@ func testWalk(t *testing.T, walk func(string, fs.WalkDirFunc) error, errVisit in
620594
return mark(d, err, &errors, clear)
621595
}
622596
// Expect no errors.
623-
err = walk(tree.name, markFn)
597+
err := walk(tree.name, markFn)
624598
if err != nil {
625599
t.Fatalf("no error expected, found: %s", err)
626600
}
@@ -1249,7 +1223,7 @@ func TestEvalSymlinks(t *testing.T) {
12491223
func TestEvalSymlinksIsNotExist(t *testing.T) {
12501224
testenv.MustHaveSymlink(t)
12511225

1252-
defer chtmpdir(t)()
1226+
chtmpdir(t)
12531227

12541228
_, err := filepath.EvalSymlinks("notexist")
12551229
if !os.IsNotExist(err) {
@@ -1332,8 +1306,7 @@ func TestRelativeSymlinkToAbsolute(t *testing.T) {
13321306
testenv.MustHaveSymlink(t)
13331307
// Not parallel: uses os.Chdir.
13341308

1335-
tmpDir := t.TempDir()
1336-
chdir(t, tmpDir)
1309+
tmpDir := chtmpdir(t)
13371310

13381311
// Create "link" in the current working directory as a symlink to an arbitrary
13391312
// absolute path. On macOS, this path is likely to begin with a symlink
@@ -1385,19 +1358,10 @@ var absTests = []string{
13851358
}
13861359

13871360
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)
13981362

13991363
for _, dir := range absTestDirs {
1400-
err = os.Mkdir(dir, 0777)
1364+
err := os.Mkdir(dir, 0777)
14011365
if err != nil {
14021366
t.Fatal("Mkdir failed: ", err)
14031367
}
@@ -1416,7 +1380,7 @@ func TestAbs(t *testing.T) {
14161380
absTests = append(absTests, extra...)
14171381
}
14181382

1419-
err = os.Chdir(absTestDirs[0])
1383+
err := os.Chdir(absTestDirs[0])
14201384
if err != nil {
14211385
t.Fatal("chdir failed: ", err)
14221386
}
@@ -1451,17 +1415,7 @@ func TestAbs(t *testing.T) {
14511415
// We test it separately from all other absTests because the empty string is not
14521416
// a valid path, so it can't be used with os.Stat.
14531417
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)
14651419

14661420
info, err := os.Stat(root)
14671421
if err != nil {
@@ -1687,20 +1641,9 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
16871641
}
16881642

16891643
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)
17021645

1703-
err = mklink(tmpdir, "link")
1646+
err := mklink(tmpdir, "link")
17041647
if err != nil {
17051648
t.Fatal(err)
17061649
}
@@ -1814,12 +1757,7 @@ func TestEvalSymlinksAboveRoot(t *testing.T) {
18141757
func TestEvalSymlinksAboveRootChdir(t *testing.T) {
18151758
testenv.MustHaveSymlink(t)
18161759

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)
18231761

18241762
subdir := filepath.Join("a", "b")
18251763
if err := os.MkdirAll(subdir, 0777); err != nil {

0 commit comments

Comments
 (0)