From 8975f1ef5cfe95dc186f77f3e464585fd14af9a3 Mon Sep 17 00:00:00 2001 From: Moynur Date: Mon, 18 Mar 2024 23:26:07 +0000 Subject: [PATCH] squash stuff Signed-off-by: Moynur --- pkg/lint/rules/chartfile.go | 48 ++++++++++++++++++++- pkg/lint/rules/chartfile_test.go | 73 ++++++++++++++++++++++++++------ 2 files changed, 108 insertions(+), 13 deletions(-) diff --git a/pkg/lint/rules/chartfile.go b/pkg/lint/rules/chartfile.go index 910602b7d..4e1a1949d 100644 --- a/pkg/lint/rules/chartfile.go +++ b/pkg/lint/rules/chartfile.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "github.com/Masterminds/semver/v3" "github.com/asaskevich/govalidator" @@ -102,14 +103,59 @@ func validateChartYamlFormat(chartFileError error) error { return nil } +const ( + chartNamePattern = "^[a-z0-9-]*$" + chartNameStartPattern = "^[a-z]" +) + +type chartNameValidator func(*chart.Metadata) error + func validateChartName(cf *chart.Metadata) error { + rules := []chartNameValidator{ + isNotEmpty, + nameIsNotPath, + doesNotContainInvalidCharacters, + beginsWithAlphabeticCharacter, + } + for _, rule := range rules { + if err := rule(cf); err != nil { + return err + } + } + + return nil +} + +func isNotEmpty(cf *chart.Metadata) error { if cf.Name == "" { return errors.New("name is required") } + return nil +} + +func nameIsNotPath(cf *chart.Metadata) error { name := filepath.Base(cf.Name) if name != cf.Name { - return fmt.Errorf("chart name %q is invalid", cf.Name) + return fmt.Errorf("chart name '%s' should not include a path", cf.Name) + } + return nil +} + +func doesNotContainInvalidCharacters(cf *chart.Metadata) error { + match, _ := regexp.MatchString(chartNamePattern, cf.Name) + if !match { + return errors.New("name must not contain any upper case letters or any special characters other than '-'") } + + return nil +} + +func beginsWithAlphabeticCharacter(cf *chart.Metadata) error { + match, _ := regexp.MatchString(chartNameStartPattern, cf.Name[0:1]) + if !match { + return errors.New("name must begin with a lowercase alphabetic character (a-z)") + } + return nil } diff --git a/pkg/lint/rules/chartfile_test.go b/pkg/lint/rules/chartfile_test.go index a06d7dc31..f4f6fa392 100644 --- a/pkg/lint/rules/chartfile_test.go +++ b/pkg/lint/rules/chartfile_test.go @@ -17,12 +17,14 @@ limitations under the License. package rules import ( + "fmt" "os" "path/filepath" "strings" "testing" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chartutil" @@ -67,18 +69,6 @@ func TestValidateChartYamlFormat(t *testing.T) { } } -func TestValidateChartName(t *testing.T) { - err := validateChartName(badChart) - if err == nil { - t.Errorf("validateChartName to return a linter error, got no error") - } - - err = validateChartName(badChartName) - if err == nil { - t.Error("expected validateChartName to return a linter error for an invalid name, got no error") - } -} - func TestValidateChartVersion(t *testing.T) { var failTest = []struct { Version string @@ -253,3 +243,62 @@ func TestChartfile(t *testing.T) { } }) } + +func TestValidateChartName(t *testing.T) { + tests := []struct { + name string + cm *chart.Metadata + wantErr assert.ErrorAssertionFunc + }{ + { + name: "no chart name so returns an error", + cm: &chart.Metadata{Name: ""}, + wantErr: assert.Error, + }, + { + name: "chart name begins with a number so returns an error", + cm: &chart.Metadata{Name: "1at-the-beginning"}, + wantErr: assert.Error, + }, + { + name: "valid chart name with dashes so returns no error", + cm: &chart.Metadata{Name: "good-chart-name"}, + wantErr: assert.NoError, + }, + { + name: "valid chart name without dashes so returns no error", + cm: &chart.Metadata{Name: "name"}, + wantErr: assert.NoError, + }, + { + name: "chart name has special characters which isn't a dash so returns error", + cm: &chart.Metadata{Name: "some.odd?chart,name!"}, + wantErr: assert.Error, + }, + { + name: "chart name has underscore special characters which isn't a dash so returns error", + cm: &chart.Metadata{Name: "some_slightly_wrong_chart_name"}, + wantErr: assert.Error, + }, + { + name: "chart name has upper case characters so returns error", + cm: &chart.Metadata{Name: "bad-Chart-name"}, + wantErr: assert.Error, + }, + { + name: "bad chart name", + cm: badChartName, + wantErr: assert.Error, + }, + { + name: "bad chart", + cm: badChart, + wantErr: assert.Error, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.wantErr(t, validateChartName(tt.cm), fmt.Sprintf("validateChartName(%v)", tt.cm.Name)) + }) + } +}