Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e0051a

Browse files
johnstcnCopilot
andauthored
feat(testutil): add GetRandomNameHyphenated (#17342)
This started coming up more often for me, so time for a test helper! --------- Co-authored-by: Copilot <[email protected]>
1 parent 46d4b28 commit 1e0051a

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

cli/templatepush_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func TestTemplatePush(t *testing.T) {
534534
"test_name": tt.name,
535535
}))
536536

537-
templateName := strings.ReplaceAll(testutil.GetRandomName(t), "_", "-")
537+
templateName := testutil.GetRandomNameHyphenated(t)
538538

539539
inv, root := clitest.New(t, "templates", "push", templateName, "-d", tempDir, "--yes")
540540
clitest.SetupConfig(t, templateAdmin, root)

coderd/templateversions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
617617
require.NoError(t, err)
618618

619619
// Create a template version from the archive
620-
tvName := strings.ReplaceAll(testutil.GetRandomName(t), "_", "-")
620+
tvName := testutil.GetRandomNameHyphenated(t)
621621
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{
622622
Name: tvName,
623623
StorageMethod: codersdk.ProvisionerStorageMethodFile,

testutil/names.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testutil
22

33
import (
44
"strconv"
5+
"strings"
56
"sync/atomic"
67
"testing"
78

@@ -25,6 +26,14 @@ func GetRandomName(t testing.TB) string {
2526
return incSuffix(name, n.Add(1), maxNameLen)
2627
}
2728

29+
// GetRandomNameHyphenated is as GetRandomName but uses a hyphen "-" instead of
30+
// an underscore.
31+
func GetRandomNameHyphenated(t testing.TB) string {
32+
t.Helper()
33+
name := namesgenerator.GetRandomName(0)
34+
return strings.ReplaceAll(name, "_", "-")
35+
}
36+
2837
func incSuffix(s string, num int64, maxLen int) string {
2938
suffix := strconv.FormatInt(num, 10)
3039
if len(s)+len(suffix) <= maxLen {

0 commit comments

Comments
 (0)