From 323a3281377defb365ca0fb9e24984b936efb5cc Mon Sep 17 00:00:00 2001 From: Marco Mariani Date: Tue, 21 Mar 2023 16:35:00 +0100 Subject: [PATCH] Unit tests: remove leftover files --- pkg/cwhub/cwhub_test.go | 35 ++++++++++++++++++++++------------- pkg/cwhub/helpers_test.go | 9 ++++++--- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/pkg/cwhub/cwhub_test.go b/pkg/cwhub/cwhub_test.go index 6aa94319619..068fcbac8ca 100644 --- a/pkg/cwhub/cwhub_test.go +++ b/pkg/cwhub/cwhub_test.go @@ -25,7 +25,8 @@ import ( var responseByPath map[string]string func TestItemStatus(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) err := UpdateHubIdx(cfg.Hub) //DownloadHubIdx() @@ -72,7 +73,8 @@ func TestItemStatus(t *testing.T) { } func TestGetters(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) err := UpdateHubIdx(cfg.Hub) //DownloadHubIdx() @@ -132,7 +134,8 @@ func TestGetters(t *testing.T) { } func TestIndexDownload(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) err := UpdateHubIdx(cfg.Hub) //DownloadHubIdx() @@ -152,24 +155,17 @@ func getTestCfg() (cfg *csconfig.Config) { return } -func test_prepenv() *csconfig.Config { +func envSetup() *csconfig.Config { resetResponseByPath() log.SetLevel(log.DebugLevel) cfg := getTestCfg() //Mock the http client http.DefaultClient.Transport = newMockTransport() - if err := os.RemoveAll(cfg.Hub.ConfigDir); err != nil { - log.Fatalf("failed to remove %s : %s", cfg.Hub.ConfigDir, err) - } - if err := os.MkdirAll(cfg.Hub.ConfigDir, 0700); err != nil { log.Fatalf("mkdir : %s", err) } - if err := os.RemoveAll(cfg.Hub.HubDir); err != nil { - log.Fatalf("failed to remove %s : %s", cfg.Hub.HubDir, err) - } if err := os.MkdirAll(cfg.Hub.HubDir, 0700); err != nil { log.Fatalf("failed to mkdir %s : %s", cfg.Hub.HubDir, err) } @@ -185,9 +181,20 @@ func test_prepenv() *csconfig.Config { // log.Fatalf("failed to mkdir %s : %s", cfg.Hub.InstallDir, err) // } return cfg +} + +func envTearDown(cfg *csconfig.Config) { + if err := os.RemoveAll(cfg.Hub.ConfigDir); err != nil { + log.Fatalf("failed to remove %s : %s", cfg.Hub.ConfigDir, err) + } + + if err := os.RemoveAll(cfg.Hub.HubDir); err != nil { + log.Fatalf("failed to remove %s : %s", cfg.Hub.HubDir, err) + } } + func testInstallItem(cfg *csconfig.Hub, t *testing.T, item Item) { //Install the parser @@ -314,7 +321,8 @@ func TestInstallParser(t *testing.T) { - check its status - remove it */ - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) getHubIdxOrFail(t) //map iteration is random by itself @@ -345,7 +353,8 @@ func TestInstallCollection(t *testing.T) { - check its status - remove it */ - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) getHubIdxOrFail(t) //map iteration is random by itself diff --git a/pkg/cwhub/helpers_test.go b/pkg/cwhub/helpers_test.go index 4cb11652408..143967002c0 100644 --- a/pkg/cwhub/helpers_test.go +++ b/pkg/cwhub/helpers_test.go @@ -9,7 +9,8 @@ import ( //Download index, install collection. Add scenario to collection (hub-side), update index, upgrade collection // We expect the new scenario to be installed func TestUpgradeConfigNewScenarioInCollection(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) // fresh install of collection getHubIdxOrFail(t) @@ -54,7 +55,8 @@ func TestUpgradeConfigNewScenarioInCollection(t *testing.T) { // Install a collection, disable a scenario. // Upgrade should install should not enable/download the disabled scenario. func TestUpgradeConfigInDisabledSceanarioShouldNotBeInstalled(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) // fresh install of collection getHubIdxOrFail(t) @@ -101,7 +103,8 @@ func getHubIdxOrFail(t *testing.T) { // Upgrade should not enable/download the disabled scenario. // Upgrade should install and enable the newly added scenario. func TestUpgradeConfigNewScenarioIsInstalledWhenReferencedScenarioIsDisabled(t *testing.T) { - cfg := test_prepenv() + cfg := envSetup() + defer envTearDown(cfg) // fresh install of collection getHubIdxOrFail(t)