From c5ea8fe00d72b2c66277736c84dd5954e830627b Mon Sep 17 00:00:00 2001 From: wawa0210 Date: Tue, 4 Aug 2020 22:08:10 +0800 Subject: [PATCH] fix subcharts templates not linted if parent chart doesn't have templates directory Signed-off-by: wawa0210 --- .../lint-chart-with-bad-subcharts-with-subcharts.txt | 2 ++ .../testdata/output/lint-chart-with-bad-subcharts.txt | 1 + pkg/lint/lint_test.go | 11 +++++++---- pkg/lint/rules/template.go | 10 ++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cmd/helm/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt b/cmd/helm/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt index e77aa387f..6adf48b2b 100644 --- a/cmd/helm/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt +++ b/cmd/helm/testdata/output/lint-chart-with-bad-subcharts-with-subcharts.txt @@ -1,6 +1,7 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended [WARNING] templates/: directory not found +[ERROR] templates/: error unpacking bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required [ERROR] : unable to load chart error unpacking bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required @@ -10,6 +11,7 @@ [ERROR] Chart.yaml: version is required [INFO] Chart.yaml: icon is recommended [WARNING] templates/: directory not found +[ERROR] templates/: validation: chart.metadata.name is required [ERROR] : unable to load chart validation: chart.metadata.name is required diff --git a/cmd/helm/testdata/output/lint-chart-with-bad-subcharts.txt b/cmd/helm/testdata/output/lint-chart-with-bad-subcharts.txt index 265e555f7..b975aa119 100644 --- a/cmd/helm/testdata/output/lint-chart-with-bad-subcharts.txt +++ b/cmd/helm/testdata/output/lint-chart-with-bad-subcharts.txt @@ -1,6 +1,7 @@ ==> Linting testdata/testcharts/chart-with-bad-subcharts [INFO] Chart.yaml: icon is recommended [WARNING] templates/: directory not found +[ERROR] templates/: error unpacking bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required [ERROR] : unable to load chart error unpacking bad-subchart in chart-with-bad-subcharts: validation: chart.metadata.name is required diff --git a/pkg/lint/lint_test.go b/pkg/lint/lint_test.go index 29ed67026..e0819d3ad 100644 --- a/pkg/lint/lint_test.go +++ b/pkg/lint/lint_test.go @@ -38,12 +38,12 @@ const goodChartDir = "rules/testdata/goodone" func TestBadChart(t *testing.T) { m := All(badChartDir, values, namespace, strict).Messages - if len(m) != 8 { + if len(m) != 9 { 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, 2 WARNINGs and 2 ERROR messages, check for them - var i, w, e, e2, e3, e4, e5, e6 bool + // There should be one INFO, 2 WARNINGs and 6 ERROR messages, check for them + var i, w, w2, 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") { @@ -54,6 +54,9 @@ func TestBadChart(t *testing.T) { if strings.Contains(msg.Err.Error(), "directory not found") { w = true } + if strings.Contains(msg.Err.Error(), "directory not found") { + w2 = true + } } if msg.Severity == support.ErrorSev { if strings.Contains(msg.Err.Error(), "version '0.0.0.0' is not a valid SemVer") { @@ -80,7 +83,7 @@ func TestBadChart(t *testing.T) { } } } - if !e || !e2 || !e3 || !e4 || !e5 || !w || !i || !e6 { + if !e || !e2 || !e3 || !e4 || !e5 || !w || !w2 || !i || !e6 { t.Errorf("Didn't find all the expected errors, got %#v", m) } } diff --git a/pkg/lint/rules/template.go b/pkg/lint/rules/template.go index 3da5b63fa..40f9d060c 100644 --- a/pkg/lint/rules/template.go +++ b/pkg/lint/rules/template.go @@ -50,12 +50,10 @@ func Templates(linter *support.Linter, values map[string]interface{}, namespace path := "templates/" templatesPath := filepath.Join(linter.ChartDir, path) - templatesDirExist := linter.RunLinterRule(support.WarningSev, path, validateTemplatesDir(templatesPath)) - - // Templates directory is optional for now - if !templatesDirExist { - return - } + // Templates directory is optional + // if a warning level alarm occurs, just output the result, + // and then continue to execute the subsequent logic + linter.RunLinterRule(support.WarningSev, path, validateTemplatesDir(templatesPath)) // Load chart and parse templates chart, err := loader.Load(linter.ChartDir)