diff --git a/pkg/action/package_test.go b/pkg/action/package_test.go index 9984b508a..421f34b33 100644 --- a/pkg/action/package_test.go +++ b/pkg/action/package_test.go @@ -17,7 +17,6 @@ limitations under the License. package action import ( - "errors" "os" "path" "testing" @@ -35,18 +34,12 @@ func TestPassphraseFileFetcher(t *testing.T) { testPkg := NewPackage() fetcher, err := testPkg.passphraseFileFetcher(path.Join(directory, "passphrase-file"), nil) - if err != nil { - t.Fatal("Unable to create passphraseFileFetcher", err) - } + require.NoError(t, err, "Unable to create passphraseFileFetcher") passphrase, err := fetcher("key") - if err != nil { - t.Fatal("Unable to fetch passphrase") - } + require.NoError(t, err, "Unable to fetch passphrase") - if string(passphrase) != secret { - t.Errorf("Expected %s got %s", secret, string(passphrase)) - } + assert.Equal(t, secret, string(passphrase), "Expected %s got %s", secret, string(passphrase)) } func TestPassphraseFileFetcher_WithLineBreak(t *testing.T) { @@ -55,18 +48,12 @@ func TestPassphraseFileFetcher_WithLineBreak(t *testing.T) { testPkg := NewPackage() fetcher, err := testPkg.passphraseFileFetcher(path.Join(directory, "passphrase-file"), nil) - if err != nil { - t.Fatal("Unable to create passphraseFileFetcher", err) - } + require.NoError(t, err, "Unable to create passphraseFileFetcher") passphrase, err := fetcher("key") - if err != nil { - t.Fatal("Unable to fetch passphrase") - } + require.NoError(t, err, "Unable to fetch passphrase") - if string(passphrase) != secret { - t.Errorf("Expected %s got %s", secret, string(passphrase)) - } + assert.Equal(t, secret, string(passphrase), "Expected %s got %s", secret, string(passphrase)) } func TestPassphraseFileFetcher_WithInvalidStdin(t *testing.T) { @@ -74,21 +61,16 @@ func TestPassphraseFileFetcher_WithInvalidStdin(t *testing.T) { testPkg := NewPackage() stdin, err := os.CreateTemp(directory, "non-existing") - if err != nil { - t.Fatal("Unable to create test file", err) - } + require.NoError(t, err, "Unable to create test file") - if _, err := testPkg.passphraseFileFetcher("-", stdin); err == nil { - t.Error("Expected passphraseFileFetcher returning an error") - } + _, err = testPkg.passphraseFileFetcher("-", stdin) + assert.Error(t, err, "Expected passphraseFileFetcher returning an error") } func TestPassphraseFileFetcher_WithStdinAndMultipleFetches(t *testing.T) { testPkg := NewPackage() stdin, w, err := os.Pipe() - if err != nil { - t.Fatal("Unable to create pipe", err) - } + require.NoError(t, err, "Unable to create pipe") passphrase := "secret-from-stdin" @@ -99,18 +81,12 @@ func TestPassphraseFileFetcher_WithStdinAndMultipleFetches(t *testing.T) { for range 4 { fetcher, err := testPkg.passphraseFileFetcher("-", stdin) - if err != nil { - t.Errorf("Expected passphraseFileFetcher to not return an error, but got %v", err) - } + require.NoError(t, err, "Expected passphraseFileFetcher to not return an error") pass, err := fetcher("key") - if err != nil { - t.Errorf("Expected passphraseFileFetcher invocation to succeed, failed with %v", err) - } + require.NoError(t, err, "Expected passphraseFileFetcher invocation to succeed") - if string(pass) != string(passphrase) { - t.Errorf("Expected multiple passphrase fetch to return %q, got %q", passphrase, pass) - } + assert.Equal(t, string(passphrase), string(pass), "Expected multiple passphrase fetch to return %q, got %q", passphrase, pass) } } @@ -148,9 +124,7 @@ func TestValidateVersion(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := validateVersion(tt.args.ver); err != nil { - if !errors.Is(err, tt.wantErr) { - t.Errorf("Expected {%v}, got {%v}", tt.wantErr, err) - } + assert.ErrorIs(t, err, tt.wantErr) } }) } diff --git a/pkg/chart/v2/lint/rules/dependencies_test.go b/pkg/chart/v2/lint/rules/dependencies_test.go index c50fc4b2d..c800887bd 100644 --- a/pkg/chart/v2/lint/rules/dependencies_test.go +++ b/pkg/chart/v2/lint/rules/dependencies_test.go @@ -19,6 +19,9 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + chart "helm.sh/helm/v4/pkg/chart/v2" "helm.sh/helm/v4/pkg/chart/v2/lint/support" chartutil "helm.sh/helm/v4/pkg/chart/v2/util" @@ -62,18 +65,12 @@ func chartWithBadDependencies() chart.Chart { func TestValidateDependencyInChartsDir(t *testing.T) { c := chartWithBadDependencies() - - if err := validateDependencyInChartsDir(&c); err == nil { - t.Error("chart should have been flagged for missing deps in chart directory") - } + assert.Error(t, validateDependencyInChartsDir(&c), "chart should have been flagged for missing deps in chart directory") } func TestValidateDependencyInMetadata(t *testing.T) { c := chartWithBadDependencies() - - if err := validateDependencyInMetadata(&c); err == nil { - t.Error("chart should have been flagged for missing deps in chart metadata") - } + assert.Error(t, validateDependencyInMetadata(&c), "chart should have been flagged for missing deps in chart metadata") } func TestValidateDependenciesUnique(t *testing.T) { @@ -131,9 +128,7 @@ func TestValidateDependenciesUnique(t *testing.T) { } for _, tt := range tests { - if err := validateDependenciesUnique(&tt.chart); err == nil { - t.Error("chart should have been flagged for dependency shadowing") - } + assert.Error(t, validateDependenciesUnique(&tt.chart), "chart should have been flagged for dependency shadowing") } } @@ -141,10 +136,7 @@ func TestDependencies(t *testing.T) { tmp := t.TempDir() c := chartWithBadDependencies() - err := chartutil.SaveDir(&c, tmp) - if err != nil { - t.Fatal(err) - } + require.NoError(t, chartutil.SaveDir(&c, tmp)) linter := support.Linter{ChartDir: filepath.Join(tmp, c.Metadata.Name)} Dependencies(&linter) diff --git a/pkg/chart/v2/util/create_test.go b/pkg/chart/v2/util/create_test.go index 367b755d9..c85c4e3d4 100644 --- a/pkg/chart/v2/util/create_test.go +++ b/pkg/chart/v2/util/create_test.go @@ -22,6 +22,9 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + chart "helm.sh/helm/v4/pkg/chart/v2" "helm.sh/helm/v4/pkg/chart/v2/loader" ) @@ -30,20 +33,13 @@ func TestCreate(t *testing.T) { tdir := t.TempDir() c, err := Create("foo", tdir) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) dir := filepath.Join(tdir, "foo") mychart, err := loader.LoadDir(c) - if err != nil { - t.Fatalf("Failed to load newly created chart %q: %s", c, err) - } - - if mychart.Name() != "foo" { - t.Errorf("Expected name to be 'foo', got %q", mychart.Name()) - } + require.NoError(t, err, "Failed to load newly created chart %q", c) + assert.Equal(t, "foo", mychart.Name(), "Expected name to be 'foo', got %q", mychart.Name()) for _, f := range []string{ ChartfileName, @@ -58,9 +54,8 @@ func TestCreate(t *testing.T) { TestConnectionName, ValuesfileName, } { - if _, err := os.Stat(filepath.Join(dir, f)); err != nil { - t.Errorf("Expected %s file: %s", f, err) - } + _, err := os.Stat(filepath.Join(dir, f)) + assert.NoErrorf(t, err, "Expected %s file", f) } } @@ -74,38 +69,26 @@ func TestCreateFrom(t *testing.T) { } srcdir := "./testdata/frobnitz/charts/mariner" - if err := CreateFrom(cf, tdir, srcdir); err != nil { - t.Fatal(err) - } + require.NoError(t, CreateFrom(cf, tdir, srcdir)) dir := filepath.Join(tdir, "foo") c := filepath.Join(tdir, cf.Name) mychart, err := loader.LoadDir(c) - if err != nil { - t.Fatalf("Failed to load newly created chart %q: %s", c, err) - } - - if mychart.Name() != "foo" { - t.Errorf("Expected name to be 'foo', got %q", mychart.Name()) - } + require.NoError(t, err, "Failed to load newly created chart %q", c) + assert.Equal(t, "foo", mychart.Name(), "Expected name to be 'foo', got %q", mychart.Name()) for _, f := range []string{ ChartfileName, ValuesfileName, filepath.Join(TemplatesDir, "placeholder.tpl"), } { - if _, err := os.Stat(filepath.Join(dir, f)); err != nil { - t.Errorf("Expected %s file: %s", f, err) - } + _, err := os.Stat(filepath.Join(dir, f)) + require.NoErrorf(t, err, "Expected %s file", f) // Check each file to make sure has been replaced b, err := os.ReadFile(filepath.Join(dir, f)) - if err != nil { - t.Errorf("Unable to read file %s: %s", f, err) - } - if bytes.Contains(b, []byte("")) { - t.Errorf("File %s contains ", f) - } + require.NoError(t, err, "Unable to read file %s", f) + assert.Falsef(t, bytes.Contains(b, []byte("")), "File %s contains ", f) } } @@ -115,9 +98,8 @@ func TestCreate_Overwrite(t *testing.T) { var errlog bytes.Buffer - if _, err := Create("foo", tdir); err != nil { - t.Fatal(err) - } + _, err := Create("foo", tdir) + require.NoError(t, err) dir := filepath.Join(tdir, "foo") @@ -126,22 +108,13 @@ func TestCreate_Overwrite(t *testing.T) { // Now re-run the create Stderr = &errlog - if _, err := Create("foo", tdir); err != nil { - t.Fatal(err) - } + _, err = Create("foo", tdir) + require.NoError(t, err) data, err := os.ReadFile(tplname) - if err != nil { - t.Fatal(err) - } - - if string(data) == "FOO" { - t.Fatal("File that should have been modified was not.") - } - - if errlog.Len() == 0 { - t.Error("Expected warnings about overwriting files.") - } + require.NoError(t, err) + require.NotEqual(t, "FOO", string(data), "File that should have been modified was not.") + assert.NotEqual(t, 0, errlog.Len(), "Expected warnings about overwriting files.") } func TestValidateChartName(t *testing.T) { diff --git a/pkg/cmd/plugin_uninstall_test.go b/pkg/cmd/plugin_uninstall_test.go index f715b09c8..bd08f6f13 100644 --- a/pkg/cmd/plugin_uninstall_test.go +++ b/pkg/cmd/plugin_uninstall_test.go @@ -21,6 +21,9 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "helm.sh/helm/v4/internal/plugin" "helm.sh/helm/v4/internal/test/ensure" "helm.sh/helm/v4/pkg/cli" @@ -39,9 +42,7 @@ func TestPluginUninstallCleansUpVersionedFiles(t *testing.T) { // Create plugin directory pluginDir := filepath.Join(pluginsDir, pluginName) - if err := os.MkdirAll(pluginDir, 0o755); err != nil { - t.Fatal(err) - } + require.NoError(t, os.MkdirAll(pluginDir, 0o755)) // Create plugin.yaml pluginYAML := `name: test-plugin @@ -49,31 +50,21 @@ version: 1.2.3 description: Test plugin command: $HELM_PLUGIN_DIR/test-plugin ` - if err := os.WriteFile(filepath.Join(pluginDir, "plugin.yaml"), []byte(pluginYAML), 0o644); err != nil { - t.Fatal(err) - } + require.NoError(t, os.WriteFile(filepath.Join(pluginDir, "plugin.yaml"), []byte(pluginYAML), 0o644)) // Create versioned tarball and provenance files tarballFile := filepath.Join(pluginsDir, "test-plugin-1.2.3.tgz") provFile := filepath.Join(pluginsDir, "test-plugin-1.2.3.tgz.prov") otherVersionTarball := filepath.Join(pluginsDir, "test-plugin-2.0.0.tgz") - if err := os.WriteFile(tarballFile, []byte("fake tarball"), 0o644); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(provFile, []byte("fake provenance"), 0o644); err != nil { - t.Fatal(err) - } + require.NoError(t, os.WriteFile(tarballFile, []byte("fake tarball"), 0o644)) + require.NoError(t, os.WriteFile(provFile, []byte("fake provenance"), 0o644)) // Create another version that should NOT be removed - if err := os.WriteFile(otherVersionTarball, []byte("other version"), 0o644); err != nil { - t.Fatal(err) - } + require.NoError(t, os.WriteFile(otherVersionTarball, []byte("other version"), 0o644)) // Load the plugin p, err := plugin.LoadDir(pluginDir) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // Create a test uninstall function that uses our test settings testUninstallPlugin := func(plugin plugin.Plugin) error { @@ -112,35 +103,26 @@ command: $HELM_PLUGIN_DIR/test-plugin } // Verify files exist before uninstall - if _, err := os.Stat(tarballFile); os.IsNotExist(err) { - t.Fatal("tarball file should exist before uninstall") - } - if _, err := os.Stat(provFile); os.IsNotExist(err) { - t.Fatal("provenance file should exist before uninstall") - } - if _, err := os.Stat(otherVersionTarball); os.IsNotExist(err) { - t.Fatal("other version tarball should exist before uninstall") - } + _, err = os.Stat(tarballFile) + require.False(t, os.IsNotExist(err), "tarball file should exist before uninstall") + _, err = os.Stat(provFile) + require.False(t, os.IsNotExist(err), "provenance file should exist before uninstall") + _, err = os.Stat(otherVersionTarball) + require.False(t, os.IsNotExist(err), "other version tarball should exist before uninstall") // Uninstall the plugin - if err := testUninstallPlugin(p); err != nil { - t.Fatal(err) - } + require.NoError(t, testUninstallPlugin(p)) // Verify plugin directory is removed - if _, err := os.Stat(pluginDir); !os.IsNotExist(err) { - t.Error("plugin directory should be removed") - } + _, err = os.Stat(pluginDir) + assert.True(t, os.IsNotExist(err), "plugin directory should be removed") // Verify only exact version files are removed - if _, err := os.Stat(tarballFile); !os.IsNotExist(err) { - t.Error("versioned tarball file should be removed") - } - if _, err := os.Stat(provFile); !os.IsNotExist(err) { - t.Error("versioned provenance file should be removed") - } + _, err = os.Stat(tarballFile) + assert.True(t, os.IsNotExist(err), "versioned tarball file should be removed") + _, err = os.Stat(provFile) + assert.True(t, os.IsNotExist(err), "versioned provenance file should be removed") // Verify other version files are NOT removed - if _, err := os.Stat(otherVersionTarball); os.IsNotExist(err) { - t.Error("other version tarball should NOT be removed") - } + _, err = os.Stat(otherVersionTarball) + assert.False(t, os.IsNotExist(err), "other version tarball should NOT be removed") } diff --git a/pkg/cmd/repo_remove_test.go b/pkg/cmd/repo_remove_test.go index 3b0072a68..7d252c35d 100644 --- a/pkg/cmd/repo_remove_test.go +++ b/pkg/cmd/repo_remove_test.go @@ -21,9 +21,11 @@ import ( "fmt" "os" "path/filepath" - "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "helm.sh/helm/v4/pkg/helmpath" "helm.sh/helm/v4/pkg/repo/v1" "helm.sh/helm/v4/pkg/repo/v1/repotest" @@ -49,41 +51,29 @@ func TestRepoRemove(t *testing.T) { repoCache: rootDir, } - if err := rmOpts.run(os.Stderr); err == nil { - t.Errorf("Expected error removing %s, but did not get one.", testRepoName) - } + require.Errorf(t, rmOpts.run(os.Stderr), "Expected error removing %s, but did not get one.", testRepoName) o := &repoAddOptions{ name: testRepoName, url: ts.URL(), repoFile: repoFile, } - if err := o.run(os.Stderr); err != nil { - t.Error(err) - } + require.NoError(t, o.run(os.Stderr)) cacheIndexFile, cacheChartsFile := createCacheFiles(rootDir, testRepoName) // Reset the buffer before running repo remove b.Reset() - if err := rmOpts.run(b); err != nil { - t.Errorf("Error removing %s from repositories", testRepoName) - } - if !strings.Contains(b.String(), "has been removed") { - t.Errorf("Unexpected output: %s", b.String()) - } + require.NoErrorf(t, rmOpts.run(b), "Error removing %s from repositories", testRepoName) + assert.Contains(t, b.String(), "has been removed", "Unexpected output: %s", b.String()) testCacheFiles(t, cacheIndexFile, cacheChartsFile, testRepoName) f, err := repo.LoadFile(repoFile) - if err != nil { - t.Error(err) - } + require.NoError(t, err) - if f.Has(testRepoName) { - t.Errorf("%s was not successfully removed from repositories list", testRepoName) - } + assert.Falsef(t, f.Has(testRepoName), "%s was not successfully removed from repositories list", testRepoName) // Test removal of multiple repos in one go var testRepoNames = []string{"foo", "bar", "baz"} @@ -97,9 +87,7 @@ func TestRepoRemove(t *testing.T) { repoFile: repoFile, } - if err := o.run(os.Stderr); err != nil { - t.Error(err) - } + require.NoError(t, o.run(os.Stderr)) cacheIndex, cacheChart := createCacheFiles(rootDir, repoName) cacheFiles[repoName] = []string{cacheIndex, cacheChart} @@ -116,23 +104,15 @@ func TestRepoRemove(t *testing.T) { b.Reset() // Run repo remove command - if err := multiRmOpts.run(b); err != nil { - t.Errorf("Error removing list of repos from repositories: %q", testRepoNames) - } + require.NoErrorf(t, multiRmOpts.run(b), "Error removing list of repos from repositories: %q", testRepoNames) // Check that stuff were removed - if !strings.Contains(b.String(), "has been removed") { - t.Errorf("Unexpected output: %s", b.String()) - } + assert.Contains(t, b.String(), "has been removed", "Unexpected output: %s", b.String()) for _, repoName := range testRepoNames { f, err := repo.LoadFile(repoFile) - if err != nil { - t.Error(err) - } - if f.Has(repoName) { - t.Errorf("%s was not successfully removed from repositories list", repoName) - } + require.NoError(t, err) + assert.Falsef(t, f.Has(repoName), "%s was not successfully removed from repositories list", repoName) cacheIndex := cacheFiles[repoName][0] cacheChart := cacheFiles[repoName][1] testCacheFiles(t, cacheIndex, cacheChart, repoName) @@ -153,12 +133,10 @@ func createCacheFiles(rootDir string, repoName string) (cacheIndexFile string, c func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, repoName string) { t.Helper() - if _, err := os.Stat(cacheIndexFile); err == nil { - t.Errorf("Error cache index file was not removed for repository %s", repoName) - } - if _, err := os.Stat(cacheChartsFile); err == nil { - t.Errorf("Error cache chart file was not removed for repository %s", repoName) - } + _, err := os.Stat(cacheIndexFile) + require.Errorf(t, err, "Error cache index file was not removed for repository %s", repoName) + _, err = os.Stat(cacheChartsFile) + assert.Errorf(t, err, "Error cache chart file was not removed for repository %s", repoName) } func TestRepoRemoveCompletion(t *testing.T) { @@ -183,9 +161,7 @@ func TestRepoRemoveCompletion(t *testing.T) { repoFile: repoFile, } - if err := o.run(os.Stderr); err != nil { - t.Error(err) - } + require.NoError(t, o.run(os.Stderr)) } repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache) diff --git a/pkg/cmd/repo_update_test.go b/pkg/cmd/repo_update_test.go index 7aa4d414f..ccd8b89c6 100644 --- a/pkg/cmd/repo_update_test.go +++ b/pkg/cmd/repo_update_test.go @@ -24,6 +24,9 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "helm.sh/helm/v4/internal/test/ensure" "helm.sh/helm/v4/pkg/getter" "helm.sh/helm/v4/pkg/repo/v1" @@ -44,9 +47,7 @@ func TestUpdateCmd(t *testing.T) { update: updater, repoFile: "testdata/repositories.yaml", } - if err := o.run(&out); err != nil { - t.Fatal(err) - } + require.NoError(t, o.run(&out)) if got := out.String(); !strings.Contains(got, "charts") || !strings.Contains(got, "firstexample") || @@ -70,9 +71,7 @@ func TestUpdateCmdMultiple(t *testing.T) { repoFile: "testdata/repositories.yaml", names: []string{"firstexample", "charts"}, } - if err := o.run(&out); err != nil { - t.Fatal(err) - } + require.NoError(t, o.run(&out)) if got := out.String(); !strings.Contains(got, "charts") || !strings.Contains(got, "firstexample") || @@ -96,9 +95,7 @@ func TestUpdateCmdInvalid(t *testing.T) { repoFile: "testdata/repositories.yaml", names: []string{"firstexample", "invalid"}, } - if err := o.run(&out); err == nil { - t.Fatal("expected error but did not get one") - } + require.Error(t, o.run(&out), "expected error but did not get one") } func TestUpdateCustomCacheCmd(t *testing.T) { @@ -119,12 +116,9 @@ func TestUpdateCustomCacheCmd(t *testing.T) { repoCache: cachePath, } b := io.Discard - if err := o.run(b); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(filepath.Join(cachePath, "test-index.yaml")); err != nil { - t.Fatalf("error finding created index file in custom cache: %v", err) - } + require.NoError(t, o.run(b)) + _, err := os.Stat(filepath.Join(cachePath, "test-index.yaml")) + require.NoErrorf(t, err, "error finding created index file in custom cache") } func TestUpdateCharts(t *testing.T) { @@ -140,20 +134,14 @@ func TestUpdateCharts(t *testing.T) { Name: "charts", URL: ts.URL(), }, getter.All(settings)) - if err != nil { - t.Error(err) - } + require.NoError(t, err) b := bytes.NewBuffer(nil) updateCharts([]*repo.ChartRepository{r}, b) got := b.String() - if strings.Contains(got, "Unable to get an update") { - t.Errorf("Failed to get a repo: %q", got) - } - if !strings.Contains(got, "Update Complete.") { - t.Error("Update was not successful") - } + assert.NotContains(t, got, "Unable to get an update", "Failed to get a repo: %q", got) + assert.Contains(t, got, "Update Complete.", "Update was not successful") } func TestRepoUpdateFileCompletion(t *testing.T) { @@ -176,37 +164,22 @@ func TestUpdateChartsFailWithError(t *testing.T) { Name: "charts", URL: invalidURL, }, getter.All(settings)) - if err != nil { - t.Error(err) - } + require.NoError(t, err) r2, err := repo.NewChartRepository(&repo.Entry{ Name: "charts", URL: invalidURL, }, getter.All(settings)) - if err != nil { - t.Error(err) - } + require.NoError(t, err) b := bytes.NewBuffer(nil) err = updateCharts([]*repo.ChartRepository{r1, r2}, b) - if err == nil { - t.Error("Repo update should return error because update of repository fails and 'fail-on-repo-update-fail' flag set") - return - } + require.Error(t, err, "Repo update should return error because update of repository fails and 'fail-on-repo-update-fail' flag set") var expectedErr = "failed to update the following repositories" var receivedErr = err.Error() - if !strings.Contains(receivedErr, expectedErr) { - t.Errorf("Expected error (%s) but got (%s) instead", expectedErr, receivedErr) - } - if !strings.Contains(receivedErr, invalidURL) { - t.Errorf("Expected invalid URL (%s) in error message but got (%s) instead", invalidURL, receivedErr) - } + require.ErrorContains(t, err, expectedErr, "Expected error (%s) but got (%s) instead", expectedErr, receivedErr) + require.ErrorContains(t, err, invalidURL, "Expected invalid URL (%s) in error message but got (%s) instead", invalidURL, receivedErr) got := b.String() - if !strings.Contains(got, "Unable to get an update") { - t.Errorf("Repo should have failed update but instead got: %q", got) - } - if strings.Contains(got, "Update Complete.") { - t.Error("Update was not successful and should return error message because 'fail-on-repo-update-fail' flag set") - } + assert.Contains(t, got, "Unable to get an update", "Repo should have failed update but instead got: %q", got) + assert.NotContains(t, got, "Update Complete.", "Update was not successful and should return error message because 'fail-on-repo-update-fail' flag set") } diff --git a/pkg/kube/resource_test.go b/pkg/kube/resource_test.go index 283b44945..ca41a06ed 100644 --- a/pkg/kube/resource_test.go +++ b/pkg/kube/resource_test.go @@ -19,6 +19,7 @@ package kube import ( "testing" + "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/cli-runtime/pkg/resource" @@ -37,27 +38,17 @@ func TestResourceList(t *testing.T) { r1 = []*resource.Info{info("foo"), info("bar")} r2 = []*resource.Info{info("bar")} - if r1.Get(info("bar")).Mapping.Resource.Resource != "pod" { - t.Error("expected get pod") - } + assert.Equal(t, "pod", r1.Get(info("bar")).Mapping.Resource.Resource, "expected get pod") diff := r1.Difference(r2) - if len(diff) != 1 { - t.Error("expected 1 result") - } + assert.Len(t, diff, 1, "expected 1 result") - if !diff.Contains(info("foo")) { - t.Error("expected diff to return foo") - } + assert.True(t, diff.Contains(info("foo")), "expected diff to return foo") inter := r1.Intersect(r2) - if len(inter) != 1 { - t.Error("expected 1 result") - } + assert.Len(t, inter, 1, "expected 1 result") - if !inter.Contains(info("bar")) { - t.Error("expected intersect to return bar") - } + assert.True(t, inter.Contains(info("bar")), "expected intersect to return bar") } func TestIsMatchingInfo(t *testing.T) { @@ -66,35 +57,23 @@ func TestIsMatchingInfo(t *testing.T) { gvkDiffGroup := schema.GroupVersionKind{Group: "diff", Version: "version1", Kind: "pod"} resourceInfoDiffGroup := resource.Info{Name: "name1", Namespace: "namespace1", Mapping: &meta.RESTMapping{GroupVersionKind: gvkDiffGroup}} - if isMatchingInfo(&resourceInfo, &resourceInfoDiffGroup) { - t.Error("expected resources not equal") - } + assert.False(t, isMatchingInfo(&resourceInfo, &resourceInfoDiffGroup), "expected resources not equal") gvkDiffVersion := schema.GroupVersionKind{Group: "group1", Version: "diff", Kind: "pod"} resourceInfoDiffVersion := resource.Info{Name: "name1", Namespace: "namespace1", Mapping: &meta.RESTMapping{GroupVersionKind: gvkDiffVersion}} - if !isMatchingInfo(&resourceInfo, &resourceInfoDiffVersion) { - t.Error("expected resources with different versions but same group and kind to be equal") - } + assert.True(t, isMatchingInfo(&resourceInfo, &resourceInfoDiffVersion), "expected resources with different versions but same group and kind to be equal") gvkDiffKind := schema.GroupVersionKind{Group: "group1", Version: "version1", Kind: "deployment"} resourceInfoDiffKind := resource.Info{Name: "name1", Namespace: "namespace1", Mapping: &meta.RESTMapping{GroupVersionKind: gvkDiffKind}} - if isMatchingInfo(&resourceInfo, &resourceInfoDiffKind) { - t.Error("expected resources not equal") - } + assert.False(t, isMatchingInfo(&resourceInfo, &resourceInfoDiffKind), "expected resources not equal") resourceInfoDiffName := resource.Info{Name: "diff", Namespace: "namespace1", Mapping: &meta.RESTMapping{GroupVersionKind: gvk}} - if isMatchingInfo(&resourceInfo, &resourceInfoDiffName) { - t.Error("expected resources not equal") - } + assert.False(t, isMatchingInfo(&resourceInfo, &resourceInfoDiffName), "expected resources not equal") resourceInfoDiffNamespace := resource.Info{Name: "name1", Namespace: "diff", Mapping: &meta.RESTMapping{GroupVersionKind: gvk}} - if isMatchingInfo(&resourceInfo, &resourceInfoDiffNamespace) { - t.Error("expected resources not equal") - } + assert.False(t, isMatchingInfo(&resourceInfo, &resourceInfoDiffNamespace), "expected resources not equal") gvkEqual := schema.GroupVersionKind{Group: "group1", Version: "version1", Kind: "pod"} resourceInfoEqual := resource.Info{Name: "name1", Namespace: "namespace1", Mapping: &meta.RESTMapping{GroupVersionKind: gvkEqual}} - if !isMatchingInfo(&resourceInfo, &resourceInfoEqual) { - t.Error("expected resources to be equal") - } + assert.True(t, isMatchingInfo(&resourceInfo, &resourceInfoEqual), "expected resources to be equal") }