From b56ff5618941dd6f2f58479e5c0e5350f8530083 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Thu, 5 May 2022 11:38:37 +0200 Subject: [PATCH] tests: use TestMain() instead of Init() to compile mock-dnf-json Lets us have teardown code so we can clean up the temporary directory where the binary was built. --- internal/client/unit_test.go | 5 ++++- internal/kojiapi/server_test.go | 9 ++++++++- internal/weldr/api_test.go | 9 ++++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/internal/client/unit_test.go b/internal/client/unit_test.go index 176455e14..9bda6e152 100644 --- a/internal/client/unit_test.go +++ b/internal/client/unit_test.go @@ -31,7 +31,7 @@ import ( var testState *TestState var dnfjsonPath string -func init() { +func setupDNFJSON() string { // compile the mock-dnf-json binary to speed up tests tmpdir, err := os.MkdirTemp("", "") if err != nil { @@ -42,6 +42,7 @@ func init() { if err := cmd.Run(); err != nil { panic(err) } + return tmpdir } func executeTests(m *testing.M) int { @@ -111,5 +112,7 @@ func executeTests(m *testing.M) int { } func TestMain(m *testing.M) { + tmpdir := setupDNFJSON() + defer os.RemoveAll(tmpdir) os.Exit(executeTests(m)) } diff --git a/internal/kojiapi/server_test.go b/internal/kojiapi/server_test.go index 095841a51..75c98c615 100644 --- a/internal/kojiapi/server_test.go +++ b/internal/kojiapi/server_test.go @@ -30,7 +30,7 @@ import ( var dnfjsonPath string -func init() { +func setupDNFJSON() { // compile the mock-dnf-json binary to speed up tests tmpdir, err := os.MkdirTemp("", "") if err != nil { @@ -573,3 +573,10 @@ func TestJobTypeValidation(t *testing.T) { test.TestRoute(t, handler, false, "GET", fmt.Sprintf("/api/composer-koji/v1/compose/%s%s", badID, path), ``, http.StatusNotFound, string(resp)) } } + +func TestMain(m *testing.M) { + setupDNFJSON() + defer os.RemoveAll(dnfjsonPath) + code := m.Run() + os.Exit(code) +} diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index caf97888e..cb6a2dba3 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -39,7 +39,7 @@ import ( var dnfjsonPath string -func init() { +func setupDNFJSON() { // compile the mock-dnf-json binary to speed up tests tmpdir, err := os.MkdirTemp("", "") if err != nil { @@ -1843,3 +1843,10 @@ func TestComposePOST_ImageTypeDenylist(t *testing.T) { } } } + +func TestMain(m *testing.M) { + setupDNFJSON() + defer os.RemoveAll(dnfjsonPath) + code := m.Run() + os.Exit(code) +}