Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49a2880

Browse files
authored
fix(testutil): ensure GetRandomName never returns strings greater tha… (#14153)
1 parent8acc7f2 commit49a2880

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

‎testutil/names.go‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
varn atomic.Int64
1212

13+
constmaxNameLen=32
14+
1315
// GetRandomName returns a random name using moby/pkg/namesgenerator.
1416
// namesgenerator.GetRandomName exposes a retry parameter that appends
1517
// a pseudo-random number between 1 and 10 to its return value.
@@ -19,5 +21,20 @@ var n atomic.Int64
1921
// to the return value.
2022
funcGetRandomName(t testing.TB)string {
2123
t.Helper()
22-
returnnamesgenerator.GetRandomName(0)+strconv.FormatInt(n.Add(1),10)
24+
name:=namesgenerator.GetRandomName(0)
25+
returnincSuffix(name,n.Add(1),maxNameLen)
26+
}
27+
28+
funcincSuffix(sstring,numint64,maxLenint)string {
29+
suffix:=strconv.FormatInt(num,10)
30+
iflen(s)+len(suffix)<=maxLen {
31+
returns+suffix
32+
}
33+
stripLen:= (len(s)+len(suffix))-maxLen
34+
stripIdx:=len(s)-stripLen
35+
ifstripIdx<0 {
36+
return""
37+
}
38+
s=s[:stripIdx]
39+
returns+suffix
2340
}

‎testutil/names_internal_test.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package testutil
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
funcTestIncSuffix(t*testing.T) {
10+
t.Parallel()
11+
12+
for_,tt:=range []struct {
13+
sstring
14+
numint64
15+
maxLenint
16+
wantstring
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+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp