Skip to content

feat(testutil): add GetRandomNameHyphenated #17342

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(testutil): add GetRandomNameHyphenated
  • Loading branch information
johnstcn committed Apr 10, 2025
commit c9b2364e81ae86e03c742b97f4f9c8c8fd68666c
2 changes: 1 addition & 1 deletion cli/templatepush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ func TestTemplatePush(t *testing.T) {
"test_name": tt.name,
}))

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

inv, root := clitest.New(t, "templates", "push", templateName, "-d", tempDir, "--yes")
clitest.SetupConfig(t, templateAdmin, root)
Expand Down
2 changes: 1 addition & 1 deletion coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
require.NoError(t, err)

// Create a template version from the archive
tvName := strings.ReplaceAll(testutil.GetRandomName(t), "_", "-")
tvName := testutil.GetRandomNameHyphenated(t)
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{
Name: tvName,
StorageMethod: codersdk.ProvisionerStorageMethodFile,
Expand Down
10 changes: 10 additions & 0 deletions testutil/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testutil

import (
"strconv"
"strings"
"sync/atomic"
"testing"

Expand All @@ -25,6 +26,15 @@ func GetRandomName(t testing.TB) string {
return incSuffix(name, n.Add(1), maxNameLen)
}

// GetRandomnameHyphenated is as GetRandomName but uses a hyphen "-" instead of
// an underscore.
func GetRandomNameHyphenated(t testing.TB) string {
t.Helper()
name := namesgenerator.GetRandomName(0)
name = strings.ReplaceAll(name, "_", "-")
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 {
Expand Down
Loading