From f186f96bcfa46b3745cc56cab7e36e3d88884e06 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 19 Sep 2023 10:25:24 +0200 Subject: [PATCH 1/3] test(cli): ensure first option selected with is expected --- cli/update_test.go | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/cli/update_test.go b/cli/update_test.go index 38b042d2813f0..fb539771c2aae 100644 --- a/cli/update_test.go +++ b/cli/update_test.go @@ -578,6 +578,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { coderdtest.AwaitTemplateVersionJob(t, client, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) + // Create new workspace inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd")) clitest.SetupConfig(t, client, root) err := inv.Run() @@ -603,11 +604,16 @@ func TestUpdateValidateRichParameters(t *testing.T) { inv, root = clitest.New(t, "update", "my-workspace") clitest.SetupConfig(t, client, root) + doneChan := make(chan struct{}) pty := ptytest.New(t).Attach(inv) - clitest.Start(t, inv) + go func() { + defer close(doneChan) + err := inv.Run() + assert.NoError(t, err) + }() matches := []string{ - stringParameterName, "second_option", + // UI select will pick "first_option". "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -619,6 +625,8 @@ func TestUpdateValidateRichParameters(t *testing.T) { pty.WriteLine(value) } } + + <-doneChan }) t.Run("ParameterOptionDisappeared", func(t *testing.T) { @@ -639,16 +647,19 @@ func TestUpdateValidateRichParameters(t *testing.T) { coderdtest.AwaitTemplateVersionJob(t, client, version.ID) template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) + // Create new workspace inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd")) clitest.SetupConfig(t, client, root) + + pty := ptytest.New(t).Attach(inv) err := inv.Run() require.NoError(t, err) // Update template - 2nd option disappeared, 4th option added updatedTemplateParameters := []*proto.RichParameter{ {Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{ - {Name: "First option", Description: "This is first option", Value: "1st"}, {Name: "Third option", Description: "This is third option", Value: "3rd"}, + {Name: "First option", Description: "This is first option", Value: "1st"}, {Name: "Fourth option", Description: "This is fourth option", Value: "4th"}, }}, } @@ -663,11 +674,17 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update the workspace inv, root = clitest.New(t, "update", "my-workspace") clitest.SetupConfig(t, client, root) - pty := ptytest.New(t).Attach(inv) - clitest.Start(t, inv) + pty = ptytest.New(t).Attach(inv) + + doneChan := make(chan struct{}) + pty = ptytest.New(t).Attach(inv) + go func() { + defer close(doneChan) + err := inv.Run() + assert.NoError(t, err) + }() matches := []string{ - stringParameterName, "Third option", "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -679,6 +696,8 @@ func TestUpdateValidateRichParameters(t *testing.T) { pty.WriteLine(value) } } + + <-doneChan }) t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) { @@ -742,6 +761,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { pty.WriteLine(value) } } + <-doneChan }) @@ -772,9 +792,9 @@ func TestUpdateValidateRichParameters(t *testing.T) { updatedTemplateParameters := []*proto.RichParameter{ templateParameters[0], {Name: immutableParameterName, Type: "string", Mutable: false, Required: true, Options: []*proto.RichParameterOption{ + {Name: "thi", Description: "This is third option for immutable parameter", Value: "III"}, {Name: "fir", Description: "This is first option for immutable parameter", Value: "I"}, {Name: "sec", Description: "This is second option for immutable parameter", Value: "II"}, - {Name: "thi", Description: "This is third option for immutable parameter", Value: "III"}, }}, } @@ -797,7 +817,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ - immutableParameterName, "thi", + // UI select will pick "thi". "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -809,6 +829,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { pty.WriteLine(value) } } + <-doneChan }) } From 6003205c0e580ae0a9e1c3dc1d356c9d6533e4fe Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 19 Sep 2023 10:44:54 +0200 Subject: [PATCH 2/3] make lint --- cli/update_test.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cli/update_test.go b/cli/update_test.go index fb539771c2aae..60f745f74ee0c 100644 --- a/cli/update_test.go +++ b/cli/update_test.go @@ -603,7 +603,6 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update the workspace inv, root = clitest.New(t, "update", "my-workspace") clitest.SetupConfig(t, client, root) - doneChan := make(chan struct{}) pty := ptytest.New(t).Attach(inv) go func() { @@ -613,7 +612,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ - // UI select will pick "first_option". + // cliui.Select will pick "first_option". "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -650,8 +649,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Create new workspace inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd")) clitest.SetupConfig(t, client, root) - - pty := ptytest.New(t).Attach(inv) + ptytest.New(t).Attach(inv) err := inv.Run() require.NoError(t, err) @@ -674,10 +672,8 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update the workspace inv, root = clitest.New(t, "update", "my-workspace") clitest.SetupConfig(t, client, root) - pty = ptytest.New(t).Attach(inv) - doneChan := make(chan struct{}) - pty = ptytest.New(t).Attach(inv) + pty := ptytest.New(t).Attach(inv) go func() { defer close(doneChan) err := inv.Run() @@ -817,7 +813,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ - // UI select will pick "thi". + // cliui.Select will pick "thi". "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { From c41c8aff544f678932b82e47db31348c0faebec3 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Tue, 19 Sep 2023 13:01:55 +0200 Subject: [PATCH 3/3] comments --- cli/update_test.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/update_test.go b/cli/update_test.go index 60f745f74ee0c..73be536d95c46 100644 --- a/cli/update_test.go +++ b/cli/update_test.go @@ -586,6 +586,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update template updatedTemplateParameters := []*proto.RichParameter{ + // The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests. {Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{ {Name: "first_option", Description: "This is first option", Value: "1"}, {Name: "second_option", Description: "This is second option", Value: "2"}, @@ -612,7 +613,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ - // cliui.Select will pick "first_option". + // `cliui.Select` will automatically pick the first option "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -655,6 +656,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update template - 2nd option disappeared, 4th option added updatedTemplateParameters := []*proto.RichParameter{ + // The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests. {Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{ {Name: "Third option", Description: "This is third option", Value: "3rd"}, {Name: "First option", Description: "This is first option", Value: "1st"}, @@ -681,6 +683,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ + // `cliui.Select` will automatically pick the first option "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 { @@ -787,6 +790,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { // Update template: add required, immutable parameter updatedTemplateParameters := []*proto.RichParameter{ templateParameters[0], + // The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests. {Name: immutableParameterName, Type: "string", Mutable: false, Required: true, Options: []*proto.RichParameterOption{ {Name: "thi", Description: "This is third option for immutable parameter", Value: "III"}, {Name: "fir", Description: "This is first option for immutable parameter", Value: "I"}, @@ -813,7 +817,7 @@ func TestUpdateValidateRichParameters(t *testing.T) { }() matches := []string{ - // cliui.Select will pick "thi". + // `cliui.Select` will automatically pick the first option "Planning workspace...", "", } for i := 0; i < len(matches); i += 2 {