From 3884c5f47d3f20e97f7630293e7a29d9eb2e9a18 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 22 Mar 2023 13:51:37 +0100 Subject: [PATCH] Unit tests: remove leftover files (#2134) --- pkg/cwhub/cwhub_test.go | 41 ++++++++++++++++++++++++--------------- pkg/cwhub/helpers_test.go | 9 ++++++--- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pkg/cwhub/cwhub_test.go b/pkg/cwhub/cwhub_test.go index 6aa943196..b0e300aa7 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() @@ -54,7 +55,7 @@ func TestItemStatus(t *testing.T) { item.Tainted = false txt, _, _, _ := ItemStatus(*item) if txt != "enabled,update-available" { - log.Fatalf("got '%s'", txt) + t.Fatalf("got '%s'", txt) } item.Installed = false @@ -63,7 +64,7 @@ func TestItemStatus(t *testing.T) { item.Tainted = false txt, _, _, _ = ItemStatus(*item) if txt != "disabled,local" { - log.Fatalf("got '%s'", txt) + t.Fatalf("got '%s'", txt) } break @@ -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 4cb116524..143967002 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)