Skip to content

Commit 09f7e1a

Browse files
committed
Modernize the code for better readabillity.
1 parent 81ec88d commit 09f7e1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+317
-318
lines changed

cmd/helm-unittest/helm_unittest_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,6 @@ func TestValidateUnittestChartTestsPathFlag(t *testing.T) {
334334
}
335335

336336
// Using %T
337-
func typeofObject(variable interface{}) string {
337+
func typeofObject(variable any) string {
338338
return fmt.Sprintf("%T", variable)
339339
}

internal/common/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package common
22

33
// K8sManifest type for rendered manifest unmarshalled to
4-
type K8sManifest map[string]interface{}
4+
type K8sManifest map[string]any
55

66
// RAW the key value for making content parsable as K8sManifest
77
const RAW string = "raw"

internal/common/utilities.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func YamlNewEncoder(w io.Writer) *yamlv3.Encoder {
3333
}
3434

3535
// TrustedMarshalYAML marshal yaml without error returned, if an error happens it panics
36-
func TrustedMarshalYAML(d interface{}) string {
36+
func TrustedMarshalYAML(d any) string {
3737
byteBuffer := new(bytes.Buffer)
3838
yamlEncoder := yamlv3.NewEncoder(byteBuffer)
3939
yamlEncoder.SetIndent(YAMLINDENTION)
@@ -45,7 +45,7 @@ func TrustedMarshalYAML(d interface{}) string {
4545
}
4646

4747
// TrustedUnmarshalYAML unmarshal yaml without error returned, if an error happens it panics
48-
func TrustedUnmarshalYAML(d string) map[string]interface{} {
48+
func TrustedUnmarshalYAML(d string) map[string]any {
4949
parsedYaml := K8sManifest{}
5050
yamlDecoder := yamlv3.NewDecoder(strings.NewReader(d))
5151
if err := yamlDecoder.Decode(&parsedYaml); err != nil {
@@ -58,11 +58,11 @@ func YamlToJson(in string) ([]byte, error) {
5858
return yaml.YAMLToJSON([]byte(in))
5959
}
6060

61-
func YmlUnmarshal(in string, out interface{}) error {
61+
func YmlUnmarshal(in string, out any) error {
6262
return yamlv3.Unmarshal([]byte(in), out)
6363
}
6464

65-
func YmlMarshall(in interface{}) (string, error) {
65+
func YmlMarshall(in any) (string, error) {
6666
out, err := yaml.Marshal(in)
6767
if err != nil {
6868
return "", err
@@ -76,7 +76,7 @@ func YmlUnmarshalTestHelper(input string, out any, t *testing.T) {
7676
assert.NoError(t, err)
7777
}
7878

79-
func YmlMarshallTestHelper(in interface{}, t *testing.T) string {
79+
func YmlMarshallTestHelper(in any, t *testing.T) string {
8080
t.Helper()
8181
out, err := yaml.Marshal(in)
8282
assert.NoError(t, err)

pkg/unittest/assertion.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ func (a *Assertion) noFileErrMessage(template string) string {
248248
}
249249

250250
// UnmarshalYAML implements yaml.Unmarshaler, constructing the validator according to the assert type.
251-
func (a *Assertion) UnmarshalYAML(unmarshal func(interface{}) error) error {
252-
assertDef := make(map[string]interface{})
251+
func (a *Assertion) UnmarshalYAML(unmarshal func(any) error) error {
252+
assertDef := make(map[string]any)
253253
if err := unmarshal(&assertDef); err != nil {
254254
return err
255255
}
@@ -271,7 +271,7 @@ func (a *Assertion) UnmarshalYAML(unmarshal func(interface{}) error) error {
271271
}
272272

273273
// parseBasicFields parses basic fields like documentIndex, not, and template.
274-
func (a *Assertion) parseBasicFields(assertDef map[string]interface{}) {
274+
func (a *Assertion) parseBasicFields(assertDef map[string]any) {
275275
if documentIndex, ok := assertDef["documentIndex"].(int); ok {
276276
a.DocumentIndex = documentIndex
277277
} else {
@@ -288,8 +288,8 @@ func (a *Assertion) parseBasicFields(assertDef map[string]interface{}) {
288288
}
289289

290290
// parseDocumentSelector parses the documentSelector field if present.
291-
func (a *Assertion) parseDocumentSelector(assertDef map[string]interface{}) error {
292-
if documentSelector, ok := assertDef["documentSelector"].(map[string]interface{}); ok {
291+
func (a *Assertion) parseDocumentSelector(assertDef map[string]any) error {
292+
if documentSelector, ok := assertDef["documentSelector"].(map[string]any); ok {
293293
s, err := valueutils.NewDocumentSelector(documentSelector)
294294
if err != nil {
295295
return err
@@ -300,7 +300,7 @@ func (a *Assertion) parseDocumentSelector(assertDef map[string]interface{}) erro
300300
}
301301

302302
// validateAssertionType validates the assertion type and ensures at least one is defined.
303-
func (a *Assertion) validateAssertionType(assertDef map[string]interface{}) error {
303+
func (a *Assertion) validateAssertionType(assertDef map[string]any) error {
304304
for key := range assertDef {
305305
if key != "template" && key != "documentIndex" && key != "not" {
306306
return fmt.Errorf("Assertion type `%s` is invalid", key)
@@ -309,7 +309,7 @@ func (a *Assertion) validateAssertionType(assertDef map[string]interface{}) erro
309309
return fmt.Errorf("no assertion type defined")
310310
}
311311

312-
func (a *Assertion) constructValidator(assertDef map[string]interface{}) error {
312+
func (a *Assertion) constructValidator(assertDef map[string]any) error {
313313
for assertName, correspondDef := range assertTypeMapping {
314314
if params, ok := assertDef[assertName]; ok {
315315
if a.validator != nil {

pkg/unittest/assertion_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestAssertionUnmarshalFromYAML(t *testing.T) {
8484
`
8585

8686
a := assert.New(t)
87-
assertionsAsMap := make([]map[string]interface{}, 33)
87+
assertionsAsMap := make([]map[string]any, 33)
8888
common.YmlUnmarshalTestHelper(assertionsYAML, &assertionsAsMap, t)
8989

9090
assertions := make([]Assertion, 33)
@@ -225,7 +225,7 @@ func TestReverseAssertionTheSameAsOriginalOneWithNotTrue(t *testing.T) {
225225

226226
type fakeSnapshotComparer bool
227227

228-
func (c fakeSnapshotComparer) CompareToSnapshot(content interface{}, optFns ...func(options *snapshot.CacheOptions) error) *snapshot.CompareResult {
228+
func (c fakeSnapshotComparer) CompareToSnapshot(content any, optFns ...func(options *snapshot.CacheOptions) error) *snapshot.CompareResult {
229229
return &snapshot.CompareResult{
230230
Passed: bool(c),
231231
}

pkg/unittest/deep_copy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ func getTemplateFileNamePattern(fileName string) string {
5353
return pattern
5454
}
5555

56-
func copySet(setValues map[string]interface{}) map[string]interface{} {
56+
func copySet(setValues map[string]any) map[string]any {
5757
copiedSet, err := copystructure.Copy(setValues)
5858
if err != nil {
5959
panic(err)
6060
}
6161

62-
copiedSetValues := copiedSet.(map[string]interface{})
62+
copiedSetValues := copiedSet.(map[string]any)
6363
// if we have an empty map, make sure it is initialized
6464
if copiedSetValues == nil {
65-
copiedSetValues = make(map[string]interface{})
65+
copiedSetValues = make(map[string]any)
6666
}
6767

6868
return copiedSetValues

pkg/unittest/formatter/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func formatDurationMilliSeconds(d time.Duration) string {
4545
return fmt.Sprintf("%d", d.Milliseconds())
4646
}
4747

48-
func writeContentToFile(noXMLHeader bool, content interface{}, w io.Writer) error {
48+
func writeContentToFile(noXMLHeader bool, content any, w io.Writer) error {
4949

5050
// to xml
5151
bytes, err := xml.MarshalIndent(content, "", "\t")

pkg/unittest/formatter/junit_report_xml_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func assertJUnitTestSuite(assert *assert.Assertions, expected, actual []JUnitTes
5252
actualLength := len(actual)
5353
assert.Equal(len(expected), actualLength)
5454

55-
for i := 0; i < actualLength; i++ {
55+
for i := range actualLength {
5656
assert.Equal(expected[i].Tests, actual[i].Tests)
5757
assert.Equal(expected[i].Errors, actual[i].Errors)
5858
assert.Equal(expected[i].Failures, actual[i].Failures)
@@ -73,7 +73,7 @@ func assertJUnitTestCase(assert *assert.Assertions, expected, actual []JUnitTest
7373
actualLength := len(actual)
7474
assert.Equal(len(expected), actualLength)
7575

76-
for i := 0; i < actualLength; i++ {
76+
for i := range actualLength {
7777
assert.Equal(expected[i].Classname, actual[i].Classname)
7878
assert.Equal(expected[i].Name, actual[i].Name)
7979

@@ -105,7 +105,7 @@ func assertJUnitProperty(assert *assert.Assertions, expected, actual []JUnitProp
105105
actualLength := len(actual)
106106
assert.Equal(len(expected), actualLength)
107107

108-
for i := 0; i < actualLength; i++ {
108+
for i := range actualLength {
109109
assert.Equal(expected[i].Name, actual[i].Name)
110110
assert.Equal(expected[i].Value, actual[i].Value)
111111
}

pkg/unittest/formatter/nunit_report_xml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func validateNUnitTestSuite(assert *assert.Assertions, expected, actual []NUnitT
4848
actualLength := len(actual)
4949
assert.Equal(len(expected), actualLength)
5050

51-
for i := 0; i < actualLength; i++ {
51+
for i := range actualLength {
5252
assert.Equal(expected[i].Name, actual[i].Name)
5353
assert.Equal(expected[i].Description, actual[i].Description)
5454
assert.Equal(expected[i].Success, actual[i].Success)
@@ -73,7 +73,7 @@ func validatNUnitTestCase(assert *assert.Assertions, expected, actual []NUnitTes
7373
actualLength := len(actual)
7474
assert.Equal(len(expected), actualLength)
7575

76-
for i := 0; i < actualLength; i++ {
76+
for i := range actualLength {
7777
assert.Equal(expected[i].Name, actual[i].Name)
7878
assert.Equal(expected[i].Description, actual[i].Description)
7979
assert.Equal(expected[i].Success, actual[i].Success)

pkg/unittest/formatter/sonar_report_xml_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func createSonarTestCase(name string, duration string, isError bool, isFailed bo
3131
func validateSonarFiles(assert *assert.Assertions, expected, actual []SonarFile) {
3232
assert.Equal(len(expected), len(actual))
3333

34-
for i := 0; i < len(actual); i++ {
34+
for i := range actual {
3535
assert.Equal(expected[i].Path, actual[i].Path)
3636
validateSonarTestCases(assert, expected[i].TestCases, actual[i].TestCases)
3737
}
@@ -40,7 +40,7 @@ func validateSonarFiles(assert *assert.Assertions, expected, actual []SonarFile)
4040
func validateSonarTestCases(assert *assert.Assertions, expected, actual []SonarTestCase) {
4141
assert.Equal(len(expected), len(actual))
4242

43-
for i := 0; i < len(actual); i++ {
43+
for i := range actual {
4444
assert.Equal(expected[i].Name, actual[i].Name)
4545
assert.Equal(expected[i].Duration, actual[i].Duration)
4646

0 commit comments

Comments
 (0)