diff --git a/pkg/naming/triming.go b/pkg/naming/triming.go index 1520254780..4b2dde37e4 100644 --- a/pkg/naming/triming.go +++ b/pkg/naming/triming.go @@ -35,11 +35,11 @@ func init() { func Truncate(format string, max int, values ...interface{}) string { var truncated []interface{} result := fmt.Sprintf(format, values...) - if excess := len(result) - max; excess > 0 { // we try to reduce the first string we find for _, value := range values { if excess == 0 { + truncated = append(truncated, value) continue } @@ -55,7 +55,6 @@ func Truncate(format string, max int, values ...interface{}) string { truncated = append(truncated, value) } - result = fmt.Sprintf(format, truncated...) } diff --git a/pkg/naming/triming_test.go b/pkg/naming/triming_test.go index 90fccd7fdb..e1396d478e 100644 --- a/pkg/naming/triming_test.go +++ b/pkg/naming/triming_test.go @@ -59,6 +59,13 @@ func TestTruncate(t *testing.T) { expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174--collector", cap: "first value gets dropped, second truncated", }, + { + format: "%s-%s-collector", + max: 63, + values: []interface{}{"4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11ea-b174-c85b7644b6b5", "d0c1e62"}, + expected: "4d96-11ea-b174-c85b7644b6b5-5d0c1e62-4d96-11e-d0c1e62-collector", + cap: "first value gets truncated, second added", + }, { format: "%d-%s-collector", max: 63, @@ -67,7 +74,9 @@ func TestTruncate(t *testing.T) { cap: "first value gets passed, second truncated", }, } { - assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...)) + t.Run(tt.cap, func(t *testing.T) { + assert.Equal(t, tt.expected, Truncate(tt.format, tt.max, tt.values...)) + }) } }