From b703d5b4bb109c6ec679fc3327021eb7b90c3bc1 Mon Sep 17 00:00:00 2001 From: Zach Burgess Date: Tue, 1 Jul 2025 14:29:29 -0700 Subject: [PATCH] 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 }