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

fix(testutil): ensure GetRandomName never returns strings greater tha…#14153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 2 commits intomainfromcj/flake-username-len
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletiontestutil/names.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@ import (

var n atomic.Int64

const maxNameLen = 32

// GetRandomName returns a random name using moby/pkg/namesgenerator.
// namesgenerator.GetRandomName exposes a retry parameter that appends
// a pseudo-random number between 1 and 10 to its return value.
Expand All@@ -19,5 +21,20 @@ var n atomic.Int64
// to the return value.
func GetRandomName(t testing.TB) string {
t.Helper()
return namesgenerator.GetRandomName(0) + strconv.FormatInt(n.Add(1), 10)
name := namesgenerator.GetRandomName(0)
return incSuffix(name, n.Add(1), maxNameLen)
}

func incSuffix(s string, num int64, maxLen int) string {
suffix := strconv.FormatInt(num, 10)
if len(s)+len(suffix) <= maxLen {
return s + suffix
}
stripLen := (len(s) + len(suffix)) - maxLen
stripIdx := len(s) - stripLen
if stripIdx < 0 {
return ""
}
s = s[:stripIdx]
return s + suffix
}
52 changes: 52 additions & 0 deletionstestutil/names_internal_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
package testutil

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIncSuffix(t *testing.T) {
t.Parallel()

for _, tt := range []struct {
s string
num int64
maxLen int
want string
}{
{
s: "foo",
num: 1,
maxLen: 4,
want: "foo1",
},
{
s: "foo",
num: 42,
maxLen: 3,
want: "f42",
},
{
s: "foo",
num: 3,
maxLen: 2,
want: "f3",
},
{
s: "foo",
num: 4,
maxLen: 1,
want: "4",
},
{
s: "foo",
num: 0,
maxLen: 0,
want: "",
},
} {
actual := incSuffix(tt.s, tt.num, tt.maxLen)
assert.Equal(t, tt.want, actual)
}
}

[8]ページ先頭

©2009-2025 Movatter.jp