From b703d5b4bb109c6ec679fc3327021eb7b90c3bc1 Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Tue, 1 Jul 2025 14:29:29 -0700 Subject: [PATCH 1/4] Return early when linting if the `templates/` dir does not exist The `vaildateTemplatesDir` function would still return `nil` if the directory doesn't exist, so the early return that was documented never occurs. Signed-off-by: Zach Burgess --- ...t-chart-with-bad-subcharts-with-subcharts.txt | 2 -- .../output/lint-chart-with-bad-subcharts.txt | 1 - .../testdata/output/lint-quiet-with-error.txt | 1 - pkg/lint/lint_test.go | 2 +- pkg/lint/rules/template.go | 16 +++++++++------- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt index 6e2efcecd..2a84d8739 100644 --- a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt +++ b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt @@ -1,6 +1,5 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended -[ERROR] templates/: error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required [ERROR] : unable to load chart error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required @@ -9,7 +8,6 @@ [ERROR] Chart.yaml: apiVersion is required. The value must be either "v1" or "v2" [ERROR] Chart.yaml: version is required [INFO] Chart.yaml: icon is recommended -[ERROR] templates/: validation: chart.metadata.name is required [ERROR] : unable to load chart validation: chart.metadata.name is required diff --git a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt index af533797b..0cba1c52b 100644 --- a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt +++ b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt @@ -1,6 +1,5 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended -[ERROR] templates/: error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required [ERROR] : unable to load chart error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required diff --git a/pkg/cmd/testdata/output/lint-quiet-with-error.txt b/pkg/cmd/testdata/output/lint-quiet-with-error.txt index e3d29a5a3..2711d9397 100644 --- a/pkg/cmd/testdata/output/lint-quiet-with-error.txt +++ b/pkg/cmd/testdata/output/lint-quiet-with-error.txt @@ -1,7 +1,6 @@ ==> Linting testdata/testcharts/chart-bad-requirements [ERROR] Chart.yaml: unable to parse YAML error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator -[ERROR] templates/: cannot load Chart.yaml: error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator [ERROR] : unable to load chart cannot load Chart.yaml: error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 888d3dfe6..6e7f40ef6 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -39,7 +39,7 @@ const invalidChartFileDir = "rules/testdata/invalidchartfile" func TestBadChart(t *testing.T) { m := RunAll(badChartDir, values, namespace).Messages - if len(m) != 8 { + if len(m) != 7 { t.Errorf("Number of errors %v", len(m)) t.Errorf("All didn't fail with expected errors, got %#v", m) } diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 72b81f191..55bc0ec89 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -54,13 +54,13 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string fpath := "templates/" templatesPath := filepath.Join(linter.ChartDir, fpath) - templatesDirExist := linter.RunLinterRule(support.WarningSev, fpath, validateTemplatesDir(templatesPath)) - // Templates directory is optional for now - if !templatesDirExist { + if _, err := os.Stat(templatesPath); errors.Is(err, os.ErrNotExist) { return } + linter.RunLinterRule(support.WarningSev, fpath, validateTemplatesDir(templatesPath)) + // Load chart and parse templates chart, err := loader.Load(linter.ChartDir) @@ -195,10 +195,12 @@ func validateTopIndentLevel(content string) error { // Validation functions func validateTemplatesDir(templatesPath string) error { - if fi, err := os.Stat(templatesPath); err == nil { - if !fi.IsDir() { - return errors.New("not a directory") - } + fi, err := os.Stat(templatesPath) + if err != nil { + return err + } + if !fi.IsDir() { + return errors.New("not a directory") } return nil } From eea2d4577bf08d5cac2bf8f054439db01e0cf97d Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Mon, 7 Jul 2025 20:31:41 -0700 Subject: [PATCH 2/4] Raise an error if the `templates/` dir is not valid and return early. Signed-off-by: Zach Burgess --- pkg/lint/rules/template.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 55bc0ec89..06a5a2994 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -59,7 +59,10 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string return } - linter.RunLinterRule(support.WarningSev, fpath, validateTemplatesDir(templatesPath)) + validTemplatesDir := linter.RunLinterRule(support.ErrorSev, fpath, validateTemplatesDir(templatesPath)) + if !validTemplatesDir { + return + } // Load chart and parse templates chart, err := loader.Load(linter.ChartDir) From 35434947a36d8859e16a81c0d4349266d35f0314 Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Mon, 7 Jul 2025 21:02:11 -0700 Subject: [PATCH 3/4] Return a lint warning if `templates/` does not exist. Signed-off-by: Zach Burgess --- pkg/action/lint_test.go | 6 +++--- .../lint-chart-with-bad-subcharts-with-subcharts.txt | 3 +++ .../testdata/output/lint-chart-with-bad-subcharts.txt | 1 + pkg/cmd/testdata/output/lint-quiet-with-error.txt | 1 + pkg/cmd/testdata/output/lint-quiet-with-warning.txt | 4 ++++ pkg/lint/rules/template.go | 11 ++++++++++- 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/action/lint_test.go b/pkg/action/lint_test.go index a01580b0a..613149a4d 100644 --- a/pkg/action/lint_test.go +++ b/pkg/action/lint_test.go @@ -154,12 +154,12 @@ func TestLint_ChartWithWarnings(t *testing.T) { } }) - t.Run("should pass with no errors when strict", func(t *testing.T) { + t.Run("should fail with one error when strict", func(t *testing.T) { testCharts := []string{chartWithNoTemplatesDir} testLint := NewLint() testLint.Strict = true - if result := testLint.Run(testCharts, values); len(result.Errors) != 0 { - t.Error("expected no errors, but got", len(result.Errors)) + if result := testLint.Run(testCharts, values); len(result.Errors) != 1 { + t.Error("expected one error, but got", len(result.Errors)) } }) } diff --git a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt index 2a84d8739..7b445a69a 100644 --- a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt +++ b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt @@ -1,5 +1,6 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended +[WARNING] templates/: directory does not exist [ERROR] : unable to load chart error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required @@ -8,10 +9,12 @@ [ERROR] Chart.yaml: apiVersion is required. The value must be either "v1" or "v2" [ERROR] Chart.yaml: version is required [INFO] Chart.yaml: icon is recommended +[WARNING] templates/: directory does not exist [ERROR] : unable to load chart validation: chart.metadata.name is required ==> Linting testdata/testcharts/chart-with-bad-subcharts/charts/good-subchart [INFO] Chart.yaml: icon is recommended +[WARNING] templates/: directory does not exist Error: 3 chart(s) linted, 2 chart(s) failed diff --git a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt index 0cba1c52b..5a1c388bb 100644 --- a/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt +++ b/pkg/cmd/testdata/output/lint-chart-with-bad-subcharts.txt @@ -1,5 +1,6 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended +[WARNING] templates/: directory does not exist [ERROR] : unable to load chart error unpacking subchart bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required diff --git a/pkg/cmd/testdata/output/lint-quiet-with-error.txt b/pkg/cmd/testdata/output/lint-quiet-with-error.txt index 2711d9397..0731a07d1 100644 --- a/pkg/cmd/testdata/output/lint-quiet-with-error.txt +++ b/pkg/cmd/testdata/output/lint-quiet-with-error.txt @@ -1,6 +1,7 @@ ==> Linting testdata/testcharts/chart-bad-requirements [ERROR] Chart.yaml: unable to parse YAML error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator +[WARNING] templates/: directory does not exist [ERROR] : unable to load chart cannot load Chart.yaml: error converting YAML to JSON: yaml: line 6: did not find expected '-' indicator diff --git a/pkg/cmd/testdata/output/lint-quiet-with-warning.txt b/pkg/cmd/testdata/output/lint-quiet-with-warning.txt index e69de29bb..ebf6c1989 100644 --- a/pkg/cmd/testdata/output/lint-quiet-with-warning.txt +++ b/pkg/cmd/testdata/output/lint-quiet-with-warning.txt @@ -0,0 +1,4 @@ +==> Linting testdata/testcharts/chart-with-only-crds +[WARNING] templates/: directory does not exist + +1 chart(s) linted, 0 chart(s) failed diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index ef355e193..b36153ec6 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -55,7 +55,8 @@ func TemplatesWithSkipSchemaValidation(linter *support.Linter, values map[string templatesPath := filepath.Join(linter.ChartDir, fpath) // Templates directory is optional for now - if _, err := os.Stat(templatesPath); errors.Is(err, os.ErrNotExist) { + templatesDirExists := linter.RunLinterRule(support.WarningSev, fpath, templatesDirExists(templatesPath)) + if !templatesDirExists { return } @@ -197,6 +198,14 @@ func validateTopIndentLevel(content string) error { } // Validation functions +func templatesDirExists(templatesPath string) error { + _, err := os.Stat(templatesPath) + if errors.Is(err, os.ErrNotExist) { + return errors.New("directory does not exist") + } + return nil +} + func validateTemplatesDir(templatesPath string) error { fi, err := os.Stat(templatesPath) if err != nil { From 1002ec5ae981b5dd4517b8a75bd4d088efab0bbd Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Mon, 7 Jul 2025 21:11:05 -0700 Subject: [PATCH 4/4] Update tests in lint_test.go Signed-off-by: Zach Burgess --- pkg/lint/lint_test.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 6e7f40ef6..63cf017e4 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -39,18 +39,23 @@ const invalidChartFileDir = "rules/testdata/invalidchartfile" func TestBadChart(t *testing.T) { m := RunAll(badChartDir, values, namespace).Messages - if len(m) != 7 { + if len(m) != 8 { t.Errorf("Number of errors %v", len(m)) t.Errorf("All didn't fail with expected errors, got %#v", m) } - // There should be one INFO, and 2 ERROR messages, check for them - var i, e, e2, e3, e4, e5, e6 bool + // There should be one INFO, one WARNING, and 2 ERROR messages, check for them + var i, w, e, e2, e3, e4, e5, e6 bool for _, msg := range m { if msg.Severity == support.InfoSev { if strings.Contains(msg.Err.Error(), "icon is recommended") { i = true } } + if msg.Severity == support.WarningSev { + if strings.Contains(msg.Err.Error(), "does not exist") { + w = true + } + } if msg.Severity == support.ErrorSev { if strings.Contains(msg.Err.Error(), "version '0.0.0.0' is not a valid SemVer") { e = true @@ -76,7 +81,7 @@ func TestBadChart(t *testing.T) { } } } - if !e || !e2 || !e3 || !e4 || !e5 || !i || !e6 { + if !e || !e2 || !e3 || !e4 || !e5 || !i || !e6 || !w { t.Errorf("Didn't find all the expected errors, got %#v", m) } } @@ -93,7 +98,7 @@ func TestInvalidYaml(t *testing.T) { func TestInvalidChartYaml(t *testing.T) { m := RunAll(invalidChartFileDir, values, namespace).Messages - if len(m) != 1 { + if len(m) != 2 { t.Fatalf("All didn't fail with expected errors, got %#v", m) } if !strings.Contains(m[0].Err.Error(), "failed to strictly parse chart metadata file") {