Skip to content

Commit cc7d2eb

Browse files
authored
fix: defer is not called on os.Exit, wrap main test function (#143)
1 parent 58e6df4 commit cc7d2eb

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

internal/controllers/main_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import (
1313

1414
// TestMain takes care of the test setup for this package.
1515
func TestMain(m *testing.M) {
16+
os.Exit(runTests(m))
17+
}
18+
19+
func runTests(m *testing.M) int {
1620
// Always remove the DB after running tests
1721
defer os.Remove("data/gorm.db")
1822

@@ -152,6 +156,5 @@ func TestMain(m *testing.M) {
152156

153157
models.DB.Create(&waterBillTransactionMar)
154158

155-
code := m.Run()
156-
os.Exit(code)
159+
return m.Run()
157160
}

internal/models/main_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111

1212
// TestMain takes care of the test setup for this package.
1313
func TestMain(m *testing.M) {
14+
os.Exit(runTests(m))
15+
}
16+
17+
func runTests(m *testing.M) int {
1418
// Always remove the DB after running tests
1519
defer os.Remove("data/gorm.db")
1620

@@ -24,6 +28,5 @@ func TestMain(m *testing.M) {
2428
log.Fatalf("Database migration failed with: %s", err.Error())
2529
}
2630

27-
code := m.Run()
28-
os.Exit(code)
31+
return m.Run()
2932
}

0 commit comments

Comments
 (0)