mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat(testutil): add GetRandomNameHyphenated (#17342)
This started coming up more often for me, so time for a test helper! --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@ -534,7 +534,7 @@ func TestTemplatePush(t *testing.T) {
|
|||||||
"test_name": tt.name,
|
"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")
|
inv, root := clitest.New(t, "templates", "push", templateName, "-d", tempDir, "--yes")
|
||||||
clitest.SetupConfig(t, templateAdmin, root)
|
clitest.SetupConfig(t, templateAdmin, root)
|
||||||
|
@ -617,7 +617,7 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Create a template version from the archive
|
// 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{
|
tv, err := templateAdmin.CreateTemplateVersion(ctx, owner.OrganizationID, codersdk.CreateTemplateVersionRequest{
|
||||||
Name: tvName,
|
Name: tvName,
|
||||||
StorageMethod: codersdk.ProvisionerStorageMethodFile,
|
StorageMethod: codersdk.ProvisionerStorageMethodFile,
|
||||||
|
@ -2,6 +2,7 @@ package testutil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -25,6 +26,14 @@ func GetRandomName(t testing.TB) string {
|
|||||||
return incSuffix(name, n.Add(1), maxNameLen)
|
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)
|
||||||
|
return strings.ReplaceAll(name, "_", "-")
|
||||||
|
}
|
||||||
|
|
||||||
func incSuffix(s string, num int64, maxLen int) string {
|
func incSuffix(s string, num int64, maxLen int) string {
|
||||||
suffix := strconv.FormatInt(num, 10)
|
suffix := strconv.FormatInt(num, 10)
|
||||||
if len(s)+len(suffix) <= maxLen {
|
if len(s)+len(suffix) <= maxLen {
|
||||||
|
Reference in New Issue
Block a user