From 1359850715c3e1a2d61d962db26b9565fbbdd93e Mon Sep 17 00:00:00 2001 From: Joe Previte Date: Wed, 7 Sep 2022 13:01:18 -0700 Subject: [PATCH] feat(cli): validate name length on template create (#3823) * feat(cli): add template create validation test This adds a test to validate that `template create` prints an error message if called with a template name exceeding the 32-char limit. * fixup * fixup test * feat(cli): add name validation to templatecreate This adds a validation step to ensure the template name is less than 32 characters. * fixup!: use utf8.RuneCountInString * fixup!: remove pty from test --- cli/templatecreate.go | 5 +++++ cli/templatecreate_test.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/cli/templatecreate.go b/cli/templatecreate.go index 3f8c7fd2e6..9c959f88f7 100644 --- a/cli/templatecreate.go +++ b/cli/templatecreate.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" "time" + "unicode/utf8" "github.com/briandowns/spinner" "github.com/spf13/cobra" @@ -49,6 +50,10 @@ func templateCreate() *cobra.Command { templateName = args[0] } + if utf8.RuneCountInString(templateName) > 31 { + return xerrors.Errorf("Template name must be less than 32 characters") + } + _, err = client.TemplateByName(cmd.Context(), organization.ID, templateName) if err == nil { return xerrors.Errorf("A template already exists named %q!", templateName) diff --git a/cli/templatecreate_test.go b/cli/templatecreate_test.go index 2a7bf39ac4..7bc332c1dd 100644 --- a/cli/templatecreate_test.go +++ b/cli/templatecreate_test.go @@ -241,6 +241,21 @@ func TestTemplateCreate(t *testing.T) { err = create() require.NoError(t, err, "Template must be recreated without error") }) + + t.Run("WithParameterExceedingCharLimit", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerD: true}) + coderdtest.CreateFirstUser(t, client) + cmd, root := clitest.New(t, "templates", "create", "1234567890123456789012345678901234567891", "--test.provisioner", string(database.ProvisionerTypeEcho)) + clitest.SetupConfig(t, client, root) + + execDone := make(chan error) + go func() { + execDone <- cmd.Execute() + }() + + require.EqualError(t, <-execDone, "Template name must be less than 32 characters") + }) } func createTestParseResponse() []*proto.Parse_Response {