From 14e829fdb83b61ef34ce31278d5f8b46cf9c6eab Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Fri, 30 Aug 2024 12:26:07 +0200 Subject: [PATCH] refactor: small improvements to tests - More usage of testify - Expand a test case to attempt to reproduce an issue Signed-off-by: Flavio Castelli --- internal/finder/kubectl_finder_test.go | 53 +++++++++----------------- internal/finder/versioner_test.go | 4 +- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/internal/finder/kubectl_finder_test.go b/internal/finder/kubectl_finder_test.go index ab91e62..447a41d 100644 --- a/internal/finder/kubectl_finder_test.go +++ b/internal/finder/kubectl_finder_test.go @@ -4,6 +4,9 @@ import ( "fmt" "os" "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type localCacheTestData struct { @@ -46,9 +49,7 @@ func teardownFilesystemTest(td localCacheTestData) error { func TestAllKubectlBinaries(t *testing.T) { td, err := setupFilesystemTest() - if err != nil { - t.Errorf("Unexpeted failure: %v", err) - } + require.NoError(t, err) defer func() { if err = teardownFilesystemTest(td); err != nil { panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err)) @@ -59,41 +60,31 @@ func TestAllKubectlBinaries(t *testing.T) { td.FakeHome, []string{"1.4.2"}, &localKubectlNamer{}) - if err = createFakeKubectlBinaries(localBins); err != nil { - t.Error(err) - } + require.NoError(t, createFakeKubectlBinaries(localBins)) systemBins := fakeKubectlBinaries( td.FakeSysBinPath, []string{"2.1.3"}, &systemKubectlNamer{}) - if err = createFakeKubectlBinaries(systemBins); err != nil { - t.Error(err) - } + require.NoError(t, createFakeKubectlBinaries(systemBins)) //nolint: gocritic // append returns a different array on purpose, we're joining two arrays expected := append(systemBins, localBins...) actual := td.Finder.AllKubectlBinaries(true) - if len(expected) != len(actual) { - t.Errorf("Expected %+v, got %+v instead", expected, actual) - } + assert.Equal(t, expected, actual, "Expected %+v, got %+v instead", expected, actual) for i, expectedBin := range expected { - if !actual[i].Version.Equals(expectedBin.Version) { - t.Errorf("Got %+v instead of %+v", actual[i].Version, expectedBin.Version) - } - if actual[i].Path != expectedBin.Path { - t.Errorf("Got %+v instead of %+v", actual[i].Path, expectedBin.Path) - } + assert.Condition(t, func() bool { + return actual[i].Version.Equals(expectedBin.Version) + }, "Expected %+v, got %+v instead", expectedBin.Version, actual[i].Version) + assert.Equal(t, expectedBin.Path, actual[i].Path, "Expected %s, got %s instead", expectedBin.Path, actual[i].Path) } } func TestLocalKubectlVersionsEmptyCache(t *testing.T) { td, err := setupFilesystemTest() - if err != nil { - t.Errorf("Unexpeted failure: %v", err) - } + require.NoError(t, err) defer func() { if err = teardownFilesystemTest(td); err != nil { panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err)) @@ -101,19 +92,13 @@ func TestLocalKubectlVersionsEmptyCache(t *testing.T) { }() bins, err := td.Finder.LocalKubectlBinaries() - if err != nil { - t.Errorf("Got unexpected error %v", err) - } - if len(bins) != 0 { - t.Errorf("Expected empty list") - } + require.NoError(t, err) + assert.Empty(t, bins) } func TestLocalKubectlVersionsDownloadDirNotCreated(t *testing.T) { td, err := setupFilesystemTest() - if err != nil { - t.Errorf("Unexpeted failure: %v", err) - } + require.NoError(t, err) defer func() { if err = teardownFilesystemTest(td); err != nil { panic(fmt.Sprintf("Error while tearing down test filesystem: %v\n", err)) @@ -121,10 +106,6 @@ func TestLocalKubectlVersionsDownloadDirNotCreated(t *testing.T) { }() bins, err := td.Finder.LocalKubectlBinaries() - if err != nil { - t.Errorf("Got unexpected error %v", err) - } - if len(bins) != 0 { - t.Errorf("Expected empty list") - } + require.NoError(t, err) + assert.Empty(t, bins) } diff --git a/internal/finder/versioner_test.go b/internal/finder/versioner_test.go index 6062cbd..fae9d89 100644 --- a/internal/finder/versioner_test.go +++ b/internal/finder/versioner_test.go @@ -37,8 +37,8 @@ func TestEnsureCompatibleKubectlAvailableDownloadsKubectlBinaryWhenNeeded(t *tes }{ { name: "requested version can be satisfied by already downloaded kubectl binary", - kubectlAvailableVersions: []string{"1.9.8"}, - requestedVersion: semver.MustParse("1.9.0"), + kubectlAvailableVersions: []string{"1.29.0", "1.26.0"}, + requestedVersion: semver.MustParse("1.30.2"), expectedToMakeDownloads: false, downloadAllowed: true, expectsError: false,