File tree 2 files changed +70
-1
lines changed
2 files changed +70
-1
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import (
10
10
11
11
var n atomic.Int64
12
12
13
+ const maxNameLen = 32
14
+
13
15
// GetRandomName returns a random name using moby/pkg/namesgenerator.
14
16
// namesgenerator.GetRandomName exposes a retry parameter that appends
15
17
// a pseudo-random number between 1 and 10 to its return value.
@@ -19,5 +21,20 @@ var n atomic.Int64
19
21
// to the return value.
20
22
func GetRandomName (t testing.TB ) string {
21
23
t .Helper ()
22
- return namesgenerator .GetRandomName (0 ) + strconv .FormatInt (n .Add (1 ), 10 )
24
+ name := namesgenerator .GetRandomName (0 )
25
+ return incSuffix (name , n .Add (1 ), maxNameLen )
26
+ }
27
+
28
+ func incSuffix (s string , num int64 , maxLen int ) string {
29
+ suffix := strconv .FormatInt (num , 10 )
30
+ if len (s )+ len (suffix ) <= maxLen {
31
+ return s + suffix
32
+ }
33
+ stripLen := (len (s ) + len (suffix )) - maxLen
34
+ stripIdx := len (s ) - stripLen
35
+ if stripIdx < 0 {
36
+ return ""
37
+ }
38
+ s = s [:stripIdx ]
39
+ return s + suffix
23
40
}
Original file line number Diff line number Diff line change
1
+ package testutil
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/stretchr/testify/assert"
7
+ )
8
+
9
+ func TestIncSuffix (t * testing.T ) {
10
+ t .Parallel ()
11
+
12
+ for _ , tt := range []struct {
13
+ s string
14
+ num int64
15
+ maxLen int
16
+ want string
17
+ }{
18
+ {
19
+ s : "foo" ,
20
+ num : 1 ,
21
+ maxLen : 4 ,
22
+ want : "foo1" ,
23
+ },
24
+ {
25
+ s : "foo" ,
26
+ num : 42 ,
27
+ maxLen : 3 ,
28
+ want : "f42" ,
29
+ },
30
+ {
31
+ s : "foo" ,
32
+ num : 3 ,
33
+ maxLen : 2 ,
34
+ want : "f3" ,
35
+ },
36
+ {
37
+ s : "foo" ,
38
+ num : 4 ,
39
+ maxLen : 1 ,
40
+ want : "4" ,
41
+ },
42
+ {
43
+ s : "foo" ,
44
+ num : 0 ,
45
+ maxLen : 0 ,
46
+ want : "" ,
47
+ },
48
+ } {
49
+ actual := incSuffix (tt .s , tt .num , tt .maxLen )
50
+ assert .Equal (t , tt .want , actual )
51
+ }
52
+ }
You can’t perform that action at this time.
0 commit comments