Skip to content

Commit 866cff5

Browse files
committed
Fix TestMain temp directory cleanup
1 parent 9c1fa03 commit 866cff5

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

cmd/go-yaml/cli_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ func TestMain(m *testing.M) {
2323
fmt.Fprintf(os.Stderr, "Failed to create temp dir: %v\n", err)
2424
os.Exit(1)
2525
}
26-
defer os.RemoveAll(tmpDir)
2726

2827
// Build the binary once
2928
testBinary = filepath.Join(tmpDir, "go-yaml")
3029
cmd := exec.Command("go", "build", "-o", testBinary, ".")
3130
cmd.Stderr = os.Stderr
3231
if err := cmd.Run(); err != nil {
3332
fmt.Fprintf(os.Stderr, "Failed to build test binary: %v\n", err)
33+
os.RemoveAll(tmpDir)
3434
os.Exit(1)
3535
}
3636

3737
// Run tests
38-
os.Exit(m.Run())
38+
code := m.Run()
39+
40+
// Cleanup before exit (defer won't run with os.Exit)
41+
os.RemoveAll(tmpDir)
42+
os.Exit(code)
3943
}
4044

4145
// TestCase represents a single test case from a test file

0 commit comments

Comments
 (0)