fix: Make invalid name error message more similar and move tests

Follow Scott comment\
https://github.com/helm/helm/pull/31491/files#r2524933784

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31491/head
Benoit Tigeot 11 months ago
parent cf077ceb27
commit 9e1e3d21c5
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -54,7 +54,7 @@ func (m Metadata) Validate() error {
var errs []error var errs []error
if !validPluginName.MatchString(m.Name) { if !validPluginName.MatchString(m.Name) {
errs = append(errs, fmt.Errorf("invalid name")) errs = append(errs, fmt.Errorf("invalid name %q: must contain only a-z, A-Z, 0-9, _ and -", m.Name))
} }
if m.APIVersion == "" { if m.APIVersion == "" {

@ -69,7 +69,7 @@ type MetadataLegacy struct {
func (m *MetadataLegacy) Validate() error { func (m *MetadataLegacy) Validate() error {
if !validPluginName.MatchString(m.Name) { if !validPluginName.MatchString(m.Name) {
return fmt.Errorf("invalid plugin name %q: must contain only a-z, A-Z, 0-9, _ and -", m.Name) return fmt.Errorf("invalid name %q: must contain only a-z, A-Z, 0-9, _ and -", m.Name)
} }
m.Usage = sanitizeString(m.Usage) m.Usage = sanitizeString(m.Usage)

@ -25,23 +25,8 @@ func TestMetadataLegacyValidate_PluginName(t *testing.T) {
pluginName string pluginName string
shouldPass bool shouldPass bool
}{ }{
// Valid names {"valid name", "my-plugin", true},
{"lowercase", "myplugin", true}, {"invalid with space", "my plugin", false},
{"uppercase", "MYPLUGIN", true},
{"mixed case", "MyPlugin", true},
{"with digits", "plugin123", true},
{"with hyphen", "my-plugin", true},
{"with underscore", "my_plugin", true},
{"mixed chars", "my-awesome_plugin_123", true},
// Invalid names
{"empty", "", false},
{"space", "my plugin", false},
{"colon", "Name:", false},
{"period", "my.plugin", false},
{"slash", "my/plugin", false},
{"dollar", "$plugin", false},
{"unicode", "plügîn", false},
} }
for _, tt := range tests { for _, tt := range tests {

@ -66,8 +66,8 @@ func TestValidatePluginData(t *testing.T) {
} else if !item.pass && err == nil { } else if !item.pass && err == nil {
t.Errorf("expected case %d to fail", i) t.Errorf("expected case %d to fail", i)
} }
if !item.pass && err.Error() != item.errString { if !item.pass && !strings.Contains(err.Error(), item.errString) {
t.Errorf("index [%d]: expected the following error: %s, but got: %s", i, item.errString, err.Error()) t.Errorf("index [%d]: expected error to contain: %s, but got: %s", i, item.errString, err.Error())
} }
} }
} }

@ -21,6 +21,44 @@ import (
"helm.sh/helm/v4/internal/plugin/schema" "helm.sh/helm/v4/internal/plugin/schema"
) )
func TestValidPluginName(t *testing.T) {
tests := []struct {
name string
pluginName string
shouldPass bool
}{
// Valid names
{"lowercase", "myplugin", true},
{"uppercase", "MYPLUGIN", true},
{"mixed case", "MyPlugin", true},
{"with digits", "plugin123", true},
{"with hyphen", "my-plugin", true},
{"with underscore", "my_plugin", true},
{"mixed chars", "my-awesome_plugin_123", true},
// Invalid names
{"empty", "", false},
{"space", "my plugin", false},
{"colon", "plugin:", false},
{"period", "my.plugin", false},
{"slash", "my/plugin", false},
{"dollar", "$plugin", false},
{"unicode", "plügîn", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matches := validPluginName.MatchString(tt.pluginName)
if tt.shouldPass && !matches {
t.Errorf("expected %q to match validPluginName regex", tt.pluginName)
}
if !tt.shouldPass && matches {
t.Errorf("expected %q to not match validPluginName regex", tt.pluginName)
}
})
}
}
func mockSubprocessCLIPlugin(t *testing.T, pluginName string) *SubprocessPluginRuntime { func mockSubprocessCLIPlugin(t *testing.T, pluginName string) *SubprocessPluginRuntime {
t.Helper() t.Helper()

Loading…
Cancel
Save