diff --git a/internal/chart/v3/lint/rules/deprecations_test.go b/internal/chart/v3/lint/rules/deprecations_test.go index 920fdc31f..96462bc15 100644 --- a/internal/chart/v3/lint/rules/deprecations_test.go +++ b/internal/chart/v3/lint/rules/deprecations_test.go @@ -32,7 +32,7 @@ func TestValidateNoDeprecations(t *testing.T) { require.Error(t, err, "Expected deprecated extension to be flagged") var depErr deprecatedAPIError require.ErrorAs(t, err, &depErr, "Expected error to be of type deprecatedAPIError") - require.NotEmpty(t, depErr.Message, "Expected error message to be non-blank: %v", err) + require.NotEmpty(t, depErr.Message, "Expected error message to be non-blank") assert.NoError(t, validateNoDeprecations(&k8sYamlStruct{ APIVersion: "v1", Kind: "Pod", diff --git a/internal/chart/v3/lint/rules/values_test.go b/internal/chart/v3/lint/rules/values_test.go index c87607f2d..7f1177729 100644 --- a/internal/chart/v3/lint/rules/values_test.go +++ b/internal/chart/v3/lint/rules/values_test.go @@ -56,8 +56,7 @@ func TestValidateValuesYamlNotDirectory(t *testing.T) { _ = os.Mkdir(nonExistingValuesFilePath, os.ModePerm) defer os.Remove(nonExistingValuesFilePath) - err := validateValuesFileExistence(nonExistingValuesFilePath) - assert.Error(t, err, "validateValuesFileExistence to return a linter error, got no error") + assert.Error(t, validateValuesFileExistence(nonExistingValuesFilePath), "validateValuesFileExistence to return a linter error, got no error") } func TestValidateValuesFileWellFormed(t *testing.T) { @@ -150,7 +149,7 @@ func TestValidateValuesFile(t *testing.T) { require.NoError(t, err, "Failed validation") } else { require.Error(t, err, "expected values file to fail parsing") - assert.ErrorContains(t, err, tt.errorMessage, "Failed with unexpected error") + assert.ErrorContains(t, err, tt.errorMessage) } }) } diff --git a/internal/chart/v3/util/validate_name_test.go b/internal/chart/v3/util/validate_name_test.go index cfc62a0f7..3ea768db2 100644 --- a/internal/chart/v3/util/validate_name_test.go +++ b/internal/chart/v3/util/validate_name_test.go @@ -16,7 +16,11 @@ limitations under the License. package util -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) // TestValidateReleaseName is a regression test for ValidateName // @@ -46,12 +50,11 @@ func TestValidateReleaseName(t *testing.T) { "a1111111111111111111111111111111111111111111111111111111111z": false, } for input, expectPass := range names { - if err := ValidateReleaseName(input); (err == nil) != expectPass { - st := "fail" - if expectPass { - st = "succeed" - } - t.Errorf("Expected %q to %s", input, st) + err := ValidateReleaseName(input) + if expectPass { + assert.NoErrorf(t, err, "Expected release name %q to pass validation", input) + } else { + assert.Errorf(t, err, "Expected release name %q to fail validation, but it passed", input) } } } @@ -80,12 +83,11 @@ func TestValidateMetadataName(t *testing.T) { "a1111111111111111111111111111111111111111111111111111111111z": false, } for input, expectPass := range names { - if err := ValidateMetadataName(input); (err == nil) != expectPass { - st := "fail" - if expectPass { - st = "succeed" - } - t.Errorf("Expected %q to %s", input, st) + err := ValidateMetadataName(input) + if expectPass { + assert.NoError(t, err, "Expected %q to succeed", input) + } else { + assert.Error(t, err, "Expected %q to fail", input) } } } diff --git a/internal/fileutil/fileutil_test.go b/internal/fileutil/fileutil_test.go index 9c997fcd3..85dd51894 100644 --- a/internal/fileutil/fileutil_test.go +++ b/internal/fileutil/fileutil_test.go @@ -37,8 +37,7 @@ func TestAtomicWriteFile(t *testing.T) { reader := bytes.NewReader([]byte(stringContent)) mode := os.FileMode(0o644) - err := AtomicWriteFile(testpath, reader, mode) - require.NoError(t, err) + require.NoError(t, AtomicWriteFile(testpath, reader, mode)) got, err := os.ReadFile(testpath) require.NoError(t, err) @@ -58,8 +57,7 @@ func TestAtomicWriteFile_CreateTempError(t *testing.T) { reader := bytes.NewReader([]byte("test content")) mode := os.FileMode(0o644) - err := AtomicWriteFile(invalidPath, reader, mode) - assert.Error(t, err, "Expected error when CreateTemp fails") + assert.Error(t, AtomicWriteFile(invalidPath, reader, mode), "Expected error when CreateTemp fails") } // TestAtomicWriteFile_EmptyContent tests with empty content @@ -70,8 +68,7 @@ func TestAtomicWriteFile_EmptyContent(t *testing.T) { reader := bytes.NewReader([]byte("")) mode := os.FileMode(0o644) - err := AtomicWriteFile(testpath, reader, mode) - require.NoError(t, err, "AtomicWriteFile error with empty content") + require.NoError(t, AtomicWriteFile(testpath, reader, mode), "AtomicWriteFile error with empty content") got, err := os.ReadFile(testpath) require.NoError(t, err) @@ -89,8 +86,7 @@ func TestAtomicWriteFile_LargeContent(t *testing.T) { reader := bytes.NewReader([]byte(largeContent)) mode := os.FileMode(0o644) - err := AtomicWriteFile(testpath, reader, mode) - require.NoError(t, err, "AtomicWriteFile error with large content") + require.NoError(t, AtomicWriteFile(testpath, reader, mode), "AtomicWriteFile error with large content") got, err := os.ReadFile(testpath) require.NoError(t, err) diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index 83979e9db..1d6eaf852 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -197,9 +197,8 @@ func TestDebugCheckHandler_Handle(t *testing.T) { } record := slog.NewRecord(time.Now(), slog.LevelInfo, "test message", 0) - err := handler.Handle(t.Context(), record) - require.NoError(t, err) + require.NoError(t, handler.Handle(t.Context(), record)) assert.Contains(t, buf.String(), "test message") }) @@ -214,9 +213,8 @@ func TestDebugCheckHandler_Handle(t *testing.T) { type testKey string ctx := context.WithValue(t.Context(), testKey("test"), "value") record := slog.NewRecord(time.Now(), slog.LevelInfo, "context test", 0) - err := handler.Handle(ctx, record) - require.NoError(t, err) + require.NoError(t, handler.Handle(ctx, record)) assert.Contains(t, buf.String(), "context test") }) } diff --git a/internal/plugin/installer/base_test.go b/internal/plugin/installer/base_test.go index b7902e699..8ef7ff588 100644 --- a/internal/plugin/installer/base_test.go +++ b/internal/plugin/installer/base_test.go @@ -15,6 +15,8 @@ package installer import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestPath(t *testing.T) { @@ -38,8 +40,6 @@ func TestPath(t *testing.T) { t.Setenv("HELM_PLUGINS", tt.helmPluginsDir) baseIns := newBase(tt.source) baseInsPath := baseIns.Path() - if baseInsPath != tt.expectPath { - t.Errorf("expected name %s, got %s", tt.expectPath, baseInsPath) - } + assert.Equal(t, tt.expectPath, baseInsPath, "expected name %s, got %s", tt.expectPath, baseInsPath) } } diff --git a/internal/plugin/loader_test.go b/internal/plugin/loader_test.go index 4b98a6f83..e0682aed7 100644 --- a/internal/plugin/loader_test.go +++ b/internal/plugin/loader_test.go @@ -194,13 +194,9 @@ func TestDetectDuplicates(t *testing.T) { mockSubprocessCLIPlugin(t, "foo"), mockSubprocessCLIPlugin(t, "bar"), } - if err := detectDuplicates(plugs); err != nil { - t.Error("no duplicates in the first set") - } + require.NoError(t, detectDuplicates(plugs), "no duplicates in the first set") plugs = append(plugs, mockSubprocessCLIPlugin(t, "foo")) - if err := detectDuplicates(plugs); err == nil { - t.Error("duplicates in the second set") - } + assert.Error(t, detectDuplicates(plugs), "duplicates in the second set") } func TestLoadAllDir_Empty(t *testing.T) { diff --git a/internal/plugin/metadata_test.go b/internal/plugin/metadata_test.go index b4994cf68..9cfdbc83a 100644 --- a/internal/plugin/metadata_test.go +++ b/internal/plugin/metadata_test.go @@ -66,7 +66,7 @@ func TestValidatePluginData(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) { err := item.plug.Metadata().Validate() if item.pass { - require.NoError(t, err, "failed to validate case %d: %s", i, err) + require.NoError(t, err, "failed to validate case %d", i) } else { require.Error(t, err, "expected case %d to fail", i) assert.ErrorContains(t, err, item.errString, "expected case %d error to contain %q", i, item.errString) @@ -97,8 +97,7 @@ func TestMetadataValidateVersion(t *testing.T) { t.Run(name, func(t *testing.T) { m := mockSubprocessCLIPlugin(t, "testplugin") m.metadata.Version = tc.version - err := m.Metadata().Validate() - assert.NoError(t, err) + assert.NoError(t, m.Metadata().Validate()) }) } @@ -106,8 +105,7 @@ func TestMetadataValidateVersion(t *testing.T) { t.Run(name, func(t *testing.T) { m := mockSubprocessCLIPlugin(t, "testplugin") m.metadata.Version = tc.version - err := m.Metadata().Validate() - assert.ErrorContains(t, err, "invalid plugin version") + assert.ErrorContains(t, m.Metadata().Validate(), "invalid plugin version") }) } } @@ -139,7 +137,7 @@ func TestMetadataValidateMultipleErrors(t *testing.T) { } for _, expectedErr := range expectedErrors { - assert.Contains(t, errStr, expectedErr, "expected error to contain %q, but got: %v", expectedErr, errStr) + require.ErrorContains(t, err, expectedErr) } // Verify that the error contains the correct number of error messages diff --git a/internal/plugin/metadata_v1_test.go b/internal/plugin/metadata_v1_test.go index 74c67178e..8a6f28705 100644 --- a/internal/plugin/metadata_v1_test.go +++ b/internal/plugin/metadata_v1_test.go @@ -77,8 +77,7 @@ func TestMetadataV1ValidateVersion(t *testing.T) { t.Run("invalid/"+name, func(t *testing.T) { m := base() m.Version = tc.version - err := m.Validate() - assert.ErrorContains(t, err, tc.errMsg) + assert.ErrorContains(t, m.Validate(), tc.errMsg) }) } } diff --git a/internal/plugin/plugin_test.go b/internal/plugin/plugin_test.go index 2e3f274f2..947792046 100644 --- a/internal/plugin/plugin_test.go +++ b/internal/plugin/plugin_test.go @@ -18,6 +18,8 @@ package plugin import ( "testing" + "github.com/stretchr/testify/assert" + "helm.sh/helm/v4/internal/plugin/schema" ) @@ -34,9 +36,7 @@ func TestValidPluginName(t *testing.T) { for name, pluginName := range validNames { t.Run("valid/"+name, func(t *testing.T) { - if !validPluginName.MatchString(pluginName) { - t.Errorf("expected %q to match validPluginName regex", pluginName) - } + assert.Truef(t, validPluginName.MatchString(pluginName), "expected %q to match validPluginName regex", pluginName) }) } @@ -52,9 +52,7 @@ func TestValidPluginName(t *testing.T) { for name, pluginName := range invalidNames { t.Run("invalid/"+name, func(t *testing.T) { - if validPluginName.MatchString(pluginName) { - t.Errorf("expected %q to not match validPluginName regex", pluginName) - } + assert.Falsef(t, validPluginName.MatchString(pluginName), "expected %q to not match validPluginName regex", pluginName) }) } } diff --git a/internal/release/v2/hook_test.go b/internal/release/v2/hook_test.go index 5a0867398..3e946825e 100644 --- a/internal/release/v2/hook_test.go +++ b/internal/release/v2/hook_test.go @@ -170,12 +170,12 @@ func TestHookExecutionUnmarshalJSON(t *testing.T) { err := json.Unmarshal([]byte(tt.input), &exec) if tt.wantErr { assert.Error(t, err) - return + } else { + require.NoError(t, err) + assert.Equal(t, tt.expected.StartedAt.Unix(), exec.StartedAt.Unix()) + assert.Equal(t, tt.expected.CompletedAt.Unix(), exec.CompletedAt.Unix()) + assert.Equal(t, tt.expected.Phase, exec.Phase) } - require.NoError(t, err) - assert.Equal(t, tt.expected.StartedAt.Unix(), exec.StartedAt.Unix()) - assert.Equal(t, tt.expected.CompletedAt.Unix(), exec.CompletedAt.Unix()) - assert.Equal(t, tt.expected.Phase, exec.Phase) }) } } @@ -194,8 +194,7 @@ func TestHookExecutionRoundTrip(t *testing.T) { require.NoError(t, err) var decoded HookExecution - err = json.Unmarshal(data, &decoded) - require.NoError(t, err) + require.NoError(t, json.Unmarshal(data, &decoded)) assert.Equal(t, original.StartedAt.Unix(), decoded.StartedAt.Unix()) assert.Equal(t, original.CompletedAt.Unix(), decoded.CompletedAt.Unix()) @@ -208,12 +207,11 @@ func TestHookExecutionEmptyStringRoundTrip(t *testing.T) { input := `{"started_at":"","completed_at":"","phase":"Succeeded"}` var exec HookExecution - err := json.Unmarshal([]byte(input), &exec) - require.NoError(t, err) + require.NoError(t, json.Unmarshal([]byte(input), &exec)) // Verify time fields are zero values - assert.True(t, exec.StartedAt.IsZero()) - assert.True(t, exec.CompletedAt.IsZero()) + assert.Zero(t, exec.StartedAt) + assert.Zero(t, exec.CompletedAt) assert.Equal(t, HookPhaseSucceeded, exec.Phase) // Marshal back and verify empty time fields are omitted @@ -221,8 +219,7 @@ func TestHookExecutionEmptyStringRoundTrip(t *testing.T) { require.NoError(t, err) var result map[string]any - err = json.Unmarshal(data, &result) - require.NoError(t, err) + require.NoError(t, json.Unmarshal(data, &result)) // Zero time values should be omitted assert.NotContains(t, result, "started_at") diff --git a/internal/sympath/walk_test.go b/internal/sympath/walk_test.go index 76ed39cac..5dd051e4d 100644 --- a/internal/sympath/walk_test.go +++ b/internal/sympath/walk_test.go @@ -132,8 +132,7 @@ func TestWalk(t *testing.T) { return mark(info, err, &errors, true) } // Expect no errors. - err := Walk(tree.name, markFn) - require.NoError(t, err) + require.NoError(t, Walk(tree.name, markFn)) require.Empty(t, errors, "unexpected errors") checkMarks(t, true)