mirror of
https://github.com/helm/chart-testing.git
synced 2026-02-05 09:45:14 +01:00
Trim leading hyphens from names (#143)
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
This commit is contained in:
@@ -198,8 +198,8 @@ func (c *Chart) CreateInstallParams(buildID string) (release string, namespace s
|
||||
namespace = fmt.Sprintf("%s-%s", namespace, buildID)
|
||||
}
|
||||
randomSuffix := util.RandomString(10)
|
||||
release = util.TruncateLeft(fmt.Sprintf("%s-%s", release, randomSuffix), maxNameLength)
|
||||
namespace = util.TruncateLeft(fmt.Sprintf("%s-%s", namespace, randomSuffix), maxNameLength)
|
||||
release = util.SanitizeName(fmt.Sprintf("%s-%s", release, randomSuffix), maxNameLength)
|
||||
namespace = util.SanitizeName(fmt.Sprintf("%s-%s", namespace, randomSuffix), maxNameLength)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -216,12 +217,15 @@ func PrintDelimiterLine(delimiterChar string) {
|
||||
fmt.Println(strings.Join(delim, ""))
|
||||
}
|
||||
|
||||
func TruncateLeft(s string, maxLength int) string {
|
||||
excess := len(s) - maxLength
|
||||
func SanitizeName(s string, maxLength int) string {
|
||||
reg := regexp.MustCompile("^[^a-zA-Z0-9]+")
|
||||
processed := reg.ReplaceAllString(s, "")
|
||||
|
||||
excess := len(processed) - maxLength
|
||||
if excess > 0 {
|
||||
return s[excess:]
|
||||
return processed[excess:]
|
||||
}
|
||||
return s
|
||||
return processed
|
||||
}
|
||||
|
||||
func GetRandomPort() (int, error) {
|
||||
|
||||
@@ -68,22 +68,23 @@ func TestCompareVersions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateLeft(t *testing.T) {
|
||||
func TestSanitizeName(t *testing.T) {
|
||||
var testDataSlice = []struct {
|
||||
input string
|
||||
maxLength int
|
||||
expected string
|
||||
}{
|
||||
{"way_shorter_than_max_length", 63, "way_shorter_than_max_length"},
|
||||
{"max_length", len("max_length"), "max_length"},
|
||||
{"way_longer_than_max_length", 10, "max_length"},
|
||||
{"one_shorter_than_max_length", len("one_shorter_than_max_length") + 1, "one_shorter_than_max_length"},
|
||||
{"_one_longer_than_max_length", len("_one_longer_than_max_length") - 1, "one_longer_than_max_length"},
|
||||
{"way-shorter-than-max-length", 63, "way-shorter-than-max-length"},
|
||||
{"max-length", len("max-length"), "max-length"},
|
||||
{"way-longer-than-max-length", 10, "max-length"},
|
||||
{"one-shorter-than-max-length", len("one-shorter-than-max-length") + 1, "one-shorter-than-max-length"},
|
||||
{"oone-longer-than-max-length", len("oone-longer-than-max-length") - 1, "one-longer-than-max-length"},
|
||||
{"-starts-with-invalid-char", 63, "starts-with-invalid-char"},
|
||||
}
|
||||
|
||||
for index, testData := range testDataSlice {
|
||||
t.Run(string(index), func(t *testing.T) {
|
||||
actual := TruncateLeft(testData.input, testData.maxLength)
|
||||
actual := SanitizeName(testData.input, testData.maxLength)
|
||||
fmt.Printf("actual: %s,%d, input: %s,%d\n", actual, len(actual), testData.input, testData.maxLength)
|
||||
assert.Equal(t, testData.expected, actual)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user