Skip to content

Commit

Permalink
simplify setup() function in tests
Browse files Browse the repository at this point in the history
- change the current directory in the function
- do not mix `t.Fatal` and `panic`
  • Loading branch information
rhysd committed Sep 1, 2024
1 parent 92ed4e5 commit 25055da
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions zglob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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))
Expand Down

0 comments on commit 25055da

Please sign in to comment.