From 25055da2aba3ddc9879e6bca2282c8dc59575c3d Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 1 Sep 2024 21:46:57 +0900 Subject: [PATCH] simplify `setup()` function in tests - change the current directory in the function - do not mix `t.Fatal` and `panic` --- zglob_test.go | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/zglob_test.go b/zglob_test.go index 8c2baa4..0690f5a 100644 --- a/zglob_test.go +++ b/zglob_test.go @@ -59,11 +59,9 @@ func fatalIf(err error) { } } -func setup(t *testing.T) string { +func setup() (string, string) { tmpdir, err := ioutil.TempDir("", "zglob") - if err != nil { - t.Fatal(err) - } + fatalIf(err) fatalIf(os.MkdirAll(filepath.Join(tmpdir, "foo/baz"), 0755)) fatalIf(os.MkdirAll(filepath.Join(tmpdir, "foo/bar"), 0755)) @@ -78,22 +76,17 @@ func setup(t *testing.T) string { fatalIf(os.MkdirAll(filepath.Join(tmpdir, "zzz/nar/{noo,x}"), 0755)) fatalIf(ioutil.WriteFile(filepath.Join(tmpdir, "zzz/nar/{noo,x}/joo.png"), []byte{}, 0644)) - return tmpdir + curdir, err := os.Getwd() + fatalIf(err) + fatalIf(os.Chdir(tmpdir)) + + return tmpdir, curdir } func TestGlob(t *testing.T) { - tmpdir := setup(t) + tmpdir, savedCwd := setup() defer os.RemoveAll(tmpdir) - - curdir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - err = os.Chdir(tmpdir) - if err != nil { - t.Fatal(err) - } - defer os.Chdir(curdir) + defer os.Chdir(savedCwd) tmpdir = "." for _, test := range testGlobs { @@ -115,18 +108,9 @@ func TestGlob(t *testing.T) { } func TestGlobAbs(t *testing.T) { - tmpdir := setup(t) + tmpdir, savedCwd := setup() defer os.RemoveAll(tmpdir) - - curdir, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - err = os.Chdir(tmpdir) - if err != nil { - t.Fatal(err) - } - defer os.Chdir(curdir) + defer os.Chdir(savedCwd) for _, test := range testGlobs { pattern := toSlash(path.Join(tmpdir, test.pattern))