Skip to content

Commit 175baed

Browse files
committed
table-driven tests
Signed-off-by: Thiago Perrotta <[email protected]>
1 parent 067269e commit 175baed

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

pkg/chart/chart_test.go

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -397,50 +397,55 @@ func TestLintYamlValidation(t *testing.T) {
397397
}
398398

399399
func TestLintYamlValidationWithoutValuesYaml(t *testing.T) {
400-
type testData struct {
401-
name string
402-
chartDir string
403-
expected bool
400+
testCases := []struct {
401+
name string
402+
chartDir string
403+
validateYaml bool
404+
callsYamlLint int
405+
callsYamale int
406+
}{
407+
{
408+
name: "no values.yaml with validation",
409+
chartDir: "testdata/no_values_yaml",
410+
validateYaml: true,
411+
callsYamlLint: 1, // Only Chart.yaml
412+
callsYamale: 0,
413+
},
414+
{
415+
name: "no values.yaml without validation",
416+
chartDir: "testdata/no_values_yaml",
417+
validateYaml: false,
418+
callsYamlLint: 0,
419+
callsYamale: 0,
420+
},
421+
{
422+
name: "with values.yaml with validation",
423+
chartDir: "testdata/test_lints",
424+
validateYaml: true,
425+
callsYamlLint: 2, // Chart.yaml and values.yaml
426+
callsYamale: 0,
427+
},
404428
}
405429

406-
runTests := func(validate bool, callsYamlLint int, callsYamale int) {
407-
fakeMockLinter := new(fakeLinter)
408-
409-
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
410-
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
411-
412-
ct.linter = fakeMockLinter
413-
ct.config.ValidateYaml = validate
414-
ct.config.ValidateChartSchema = false
415-
ct.config.ValidateMaintainers = false
416-
417-
var suffix string
418-
if validate {
419-
suffix = "with-validation"
420-
} else {
421-
suffix = "without-validation"
422-
}
430+
for _, testData := range testCases {
431+
t.Run(testData.name, func(t *testing.T) {
432+
fakeMockLinter := new(fakeLinter)
433+
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
434+
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
423435

424-
testCases := []testData{
425-
{fmt.Sprintf("lint-no-values-yaml-%s", suffix), "testdata/no_values_yaml", true},
426-
}
436+
ct.linter = fakeMockLinter
437+
ct.config.ValidateYaml = testData.validateYaml
438+
ct.config.ValidateChartSchema = false
439+
ct.config.ValidateMaintainers = false
427440

428-
for _, testData := range testCases {
429-
t.Run(testData.name, func(t *testing.T) {
430-
chart, err := NewChart(testData.chartDir)
431-
assert.Nil(t, err)
432-
result := ct.LintChart(chart)
433-
assert.Equal(t, testData.expected, result.Error == nil)
434-
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
435-
// Only Chart.yaml should be linted, not values.yaml since it doesn't exist
436-
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
437-
})
438-
}
441+
chart, err := NewChart(testData.chartDir)
442+
assert.Nil(t, err)
443+
result := ct.LintChart(chart)
444+
assert.Nil(t, result.Error)
445+
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", testData.callsYamale)
446+
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", testData.callsYamlLint)
447+
})
439448
}
440-
441-
// When validation is enabled, only Chart.yaml should be linted (1 call), not values.yaml
442-
runTests(true, 1, 0)
443-
runTests(false, 0, 0)
444449
}
445450

446451
func TestLintDependencyExtraArgs(t *testing.T) {

0 commit comments

Comments
 (0)