Fixed job name normalisation for bundle generate (#1601)

## Changes
Fixes #1537 

## Tests
Added unit test
This commit is contained in:
Andrew Nester 2024-07-17 14:33:49 +02:00 committed by GitHub
parent e1474a38f9
commit 6d710a411a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 3 deletions

View File

@ -22,7 +22,7 @@ func TestGetShortUserName(t *testing.T) {
}, },
{ {
email: "test$.user@example.com", email: "test$.user@example.com",
expected: "test__user", expected: "test_user",
}, },
{ {
email: `jöhn.dœ@domain.com`, // Using non-ASCII characters. email: `jöhn.dœ@domain.com`, // Using non-ASCII characters.
@ -38,7 +38,7 @@ func TestGetShortUserName(t *testing.T) {
}, },
{ {
email: `"_quoted"@domain.com`, // Quoted strings can be part of the local-part. email: `"_quoted"@domain.com`, // Quoted strings can be part of the local-part.
expected: "__quoted_", expected: "quoted",
}, },
{ {
email: `name-o'mally@website.org`, // Single quote in the local-part. email: `name-o'mally@website.org`, // Single quote in the local-part.

View File

@ -1,6 +1,7 @@
package textutil package textutil
import ( import (
"regexp"
"strings" "strings"
"unicode" "unicode"
) )
@ -9,7 +10,14 @@ import (
// including spaces and dots, which are not supported in e.g. experiment names or YAML keys. // including spaces and dots, which are not supported in e.g. experiment names or YAML keys.
func NormalizeString(name string) string { func NormalizeString(name string) string {
name = strings.ToLower(name) name = strings.ToLower(name)
return strings.Map(replaceNonAlphanumeric, name) s := strings.Map(replaceNonAlphanumeric, name)
// replacing multiple underscores with a single one
re := regexp.MustCompile(`_+`)
s = re.ReplaceAllString(s, "_")
// removing leading and trailing underscores
return strings.Trim(s, "_")
} }
func replaceNonAlphanumeric(r rune) rune { func replaceNonAlphanumeric(r rune) rune {

View File

@ -46,6 +46,10 @@ func TestNormalizeString(t *testing.T) {
{ {
input: "TestTestTest", input: "TestTestTest",
expected: "testtesttest", expected: "testtesttest",
},
{
input: ".test//test..test",
expected: "test_test_test",
}} }}
for _, c := range cases { for _, c := range cases {