|
6 | 6 | "os"
|
7 | 7 | "path/filepath"
|
8 | 8 | "runtime"
|
| 9 | + "strings" |
9 | 10 | "testing"
|
10 | 11 |
|
11 | 12 | "github.com/stretchr/testify/assert"
|
@@ -70,6 +71,90 @@ func TestTemplatePush(t *testing.T) {
|
70 | 71 | require.Equal(t, "example", templateVersions[1].Name)
|
71 | 72 | })
|
72 | 73 |
|
| 74 | + t.Run("Message less than or equal to 72 chars", func(t *testing.T) { |
| 75 | + t.Parallel() |
| 76 | + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) |
| 77 | + user := coderdtest.CreateFirstUser(t, client) |
| 78 | + version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) |
| 79 | + _ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) |
| 80 | + |
| 81 | + template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) |
| 82 | + source := clitest.CreateTemplateVersionSource(t, &echo.Responses{ |
| 83 | + Parse: echo.ParseComplete, |
| 84 | + ProvisionApply: echo.ProvisionComplete, |
| 85 | + }) |
| 86 | + |
| 87 | + wantMessage := strings.Repeat("a", 72) |
| 88 | + |
| 89 | + inv, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--name", "example", "--message", wantMessage, "--yes") |
| 90 | + clitest.SetupConfig(t, client, root) |
| 91 | + pty := ptytest.New(t).Attach(inv) |
| 92 | + |
| 93 | + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + inv = inv.WithContext(ctx) |
| 97 | + w := clitest.StartWithWaiter(t, inv) |
| 98 | + |
| 99 | + pty.ExpectNoMatchBefore(ctx, "Template message is longer than 72 characters", "Updated version at") |
| 100 | + |
| 101 | + w.RequireSuccess() |
| 102 | + |
| 103 | + // Assert that the template version changed. |
| 104 | + templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{ |
| 105 | + TemplateID: template.ID, |
| 106 | + }) |
| 107 | + require.NoError(t, err) |
| 108 | + assert.Len(t, templateVersions, 2) |
| 109 | + assert.NotEqual(t, template.ActiveVersionID, templateVersions[1].ID) |
| 110 | + require.Equal(t, wantMessage, templateVersions[1].Message) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("Message too long, warn but continue", func(t *testing.T) { |
| 114 | + t.Parallel() |
| 115 | + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) |
| 116 | + user := coderdtest.CreateFirstUser(t, client) |
| 117 | + version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) |
| 118 | + _ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) |
| 119 | + |
| 120 | + template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) |
| 121 | + source := clitest.CreateTemplateVersionSource(t, &echo.Responses{ |
| 122 | + Parse: echo.ParseComplete, |
| 123 | + ProvisionApply: echo.ProvisionComplete, |
| 124 | + }) |
| 125 | + |
| 126 | + ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) |
| 127 | + defer cancel() |
| 128 | + |
| 129 | + for i, tt := range []struct { |
| 130 | + wantMessage string |
| 131 | + wantMatch string |
| 132 | + }{ |
| 133 | + {wantMessage: strings.Repeat("a", 73), wantMatch: "Template message is longer than 72 characters"}, |
| 134 | + {wantMessage: "This is my title\n\nAnd this is my body.", wantMatch: "Template message contains newlines"}, |
| 135 | + } { |
| 136 | + inv, root := clitest.New(t, "templates", "push", template.Name, "--directory", source, "--test.provisioner", string(database.ProvisionerTypeEcho), "--message", tt.wantMessage, "--yes") |
| 137 | + clitest.SetupConfig(t, client, root) |
| 138 | + pty := ptytest.New(t).Attach(inv) |
| 139 | + |
| 140 | + inv = inv.WithContext(ctx) |
| 141 | + w := clitest.StartWithWaiter(t, inv) |
| 142 | + |
| 143 | + pty.ExpectMatchContext(ctx, tt.wantMatch) |
| 144 | + |
| 145 | + w.RequireSuccess() |
| 146 | + |
| 147 | + // Assert that the template version changed. |
| 148 | + templateVersions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{ |
| 149 | + TemplateID: template.ID, |
| 150 | + }) |
| 151 | + require.NoError(t, err) |
| 152 | + assert.Len(t, templateVersions, 2+i) |
| 153 | + assert.NotEqual(t, template.ActiveVersionID, templateVersions[1+i].ID) |
| 154 | + require.Equal(t, tt.wantMessage, templateVersions[1+i].Message) |
| 155 | + } |
| 156 | + }) |
| 157 | + |
73 | 158 | t.Run("NoLockfile", func(t *testing.T) {
|
74 | 159 | t.Parallel()
|
75 | 160 | client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
|
0 commit comments