Merge pull request #32410 from mmorel-35/testifylint-manual-assert-internal-6

chore(internal): refactor: convert tests to testify assert/require part 6
pull/32291/merge
George Jenkins 3 days ago committed by GitHub
commit 715888a5eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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",

@ -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)
}
})
}

@ -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)
}
}
}

@ -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)

@ -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")
})
}

@ -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)
}
}

@ -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) {

@ -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

@ -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)
})
}
}

@ -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)
})
}
}

@ -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")

@ -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)

Loading…
Cancel
Save