fix: improve plugin name validation error messages

Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
pull/31491/head
Benoit Tigeot 1 month ago
parent 51a9bc5157
commit c04e18e452
No known key found for this signature in database
GPG Key ID: 8E6D4FC8AEBDA62C

@ -69,7 +69,10 @@ type MetadataLegacy struct {
func (m *MetadataLegacy) Validate() error {
if !validPluginName.MatchString(m.Name) {
return fmt.Errorf("invalid plugin name")
if m.Name == "" {
return fmt.Errorf("plugin name is empty or missing: ensure plugin.yaml contains 'name:' field (lowercase)")
}
return fmt.Errorf("invalid plugin name %q: plugin names can only contain ASCII characters a-z, A-Z, 0-9, _ and -", m.Name)
}
m.Usage = sanitizeString(m.Usage)

@ -0,0 +1,55 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"strings"
"testing"
)
func TestMetadataLegacyValidate_EmptyName(t *testing.T) {
metadata := MetadataLegacy{
Name: "",
Version: "1.0.0",
}
err := metadata.Validate()
if err == nil {
t.Fatal("expected error for empty plugin name")
}
expectedMsg := "plugin name is empty or missing"
if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("expected error to contain %q, got %q", expectedMsg, err.Error())
}
}
func TestMetadataLegacyValidate_InvalidName(t *testing.T) {
metadata := MetadataLegacy{
Name: "invalid name",
Version: "1.0.0",
}
err := metadata.Validate()
if err == nil {
t.Fatal("expected error for invalid plugin name")
}
expectedMsg := "plugin names can only contain ASCII characters"
if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("expected error to contain %q, got %q", expectedMsg, err.Error())
}
}
Loading…
Cancel
Save