From 1c8d1e375fe1823cdb7adfc47fc3014ddc59730e Mon Sep 17 00:00:00 2001 From: Rostyslav Polishchuk Date: Sat, 19 Apr 2025 00:04:11 +0000 Subject: [PATCH] fix: chart icon presence test The `TestValidateChartIconPresence` test fails when run after `TestValidateChartIconURL` as they both are using a global variable `badChart.Icon` ``` : go test -v -test.shuffle 1 -run '^(TestValidateChartIconPresence|TestValidateChartIconURL)$' ./pkg/lint/rules/ -test.shuffle 1 === RUN TestValidateChartIconURL --- PASS: TestValidateChartIconURL (0.00s) === RUN TestValidateChartIconPresence chartfile_test.go:171: validateChartIconPresence to return a linter error, got no error --- FAIL: TestValidateChartIconPresence (0.00s) FAIL FAIL helm.sh/helm/v4/pkg/lint/rules 0.051s FAIL : go test -v -test.shuffle 2 -run '^(TestValidateChartIconPresence|TestValidateChartIconURL)$' ./pkg/lint/rules/ -test.shuffle 2 === RUN TestValidateChartIconPresence --- PASS: TestValidateChartIconPresence (0.00s) === RUN TestValidateChartIconURL --- PASS: TestValidateChartIconURL (0.00s) PASS ok helm.sh/helm/v4/pkg/lint/rules 0.050s ``` This commit: 1. Remove dependency on global variable 2. Explicitly set the state of the test object. Signed-off-by: Rostyslav Polishchuk --- pkg/lint/rules/chartfile_test.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/lint/rules/chartfile_test.go b/pkg/lint/rules/chartfile_test.go index 061d90e33..e97cdadd5 100644 --- a/pkg/lint/rules/chartfile_test.go +++ b/pkg/lint/rules/chartfile_test.go @@ -166,10 +166,30 @@ func TestValidateChartSources(t *testing.T) { } func TestValidateChartIconPresence(t *testing.T) { - err := validateChartIconPresence(badChart) - if err == nil { - t.Errorf("validateChartIconPresence to return a linter error, got no error") - } + t.Run("Icon absent", func(t *testing.T) { + testChart := &chart.Metadata{ + Icon: "", + } + + err := validateChartIconPresence(testChart) + + if err == nil { + t.Errorf("validateChartIconPresence to return a linter error, got no error") + } else if !strings.Contains(err.Error(), "icon is recommended") { + t.Errorf("expected %q, got %q", "icon is recommended", err.Error()) + } + }) + t.Run("Icon present", func(t *testing.T) { + testChart := &chart.Metadata{ + Icon: "http://example.org/icon.png", + } + + err := validateChartIconPresence(testChart) + + if err != nil { + t.Errorf("Unexpected error: %q", err.Error()) + } + }) } func TestValidateChartIconURL(t *testing.T) {