Skip to content

Commit ceeb998

Browse files
authored
test(cli): ensure first option selected with is expected (#9770)
1 parent b0e3daa commit ceeb998

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

cli/update_test.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,15 @@ func TestUpdateValidateRichParameters(t *testing.T) {
578578
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
579579
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
580580

581+
// Create new workspace
581582
inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd"))
582583
clitest.SetupConfig(t, client, root)
583584
err := inv.Run()
584585
require.NoError(t, err)
585586

586587
// Update template
587588
updatedTemplateParameters := []*proto.RichParameter{
589+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
588590
{Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{
589591
{Name: "first_option", Description: "This is first option", Value: "1"},
590592
{Name: "second_option", Description: "This is second option", Value: "2"},
@@ -602,12 +604,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
602604
// Update the workspace
603605
inv, root = clitest.New(t, "update", "my-workspace")
604606
clitest.SetupConfig(t, client, root)
605-
607+
doneChan := make(chan struct{})
606608
pty := ptytest.New(t).Attach(inv)
607-
clitest.Start(t, inv)
609+
go func() {
610+
defer close(doneChan)
611+
err := inv.Run()
612+
assert.NoError(t, err)
613+
}()
608614

609615
matches := []string{
610-
stringParameterName, "second_option",
616+
// `cliui.Select` will automatically pick the first option
611617
"Planning workspace...", "",
612618
}
613619
for i := 0; i < len(matches); i += 2 {
@@ -619,6 +625,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
619625
pty.WriteLine(value)
620626
}
621627
}
628+
629+
<-doneChan
622630
})
623631

624632
t.Run("ParameterOptionDisappeared", func(t *testing.T) {
@@ -639,16 +647,19 @@ func TestUpdateValidateRichParameters(t *testing.T) {
639647
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
640648
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
641649

650+
// Create new workspace
642651
inv, root := clitest.New(t, "create", "my-workspace", "--yes", "--template", template.Name, "--parameter", fmt.Sprintf("%s=%s", stringParameterName, "2nd"))
643652
clitest.SetupConfig(t, client, root)
653+
ptytest.New(t).Attach(inv)
644654
err := inv.Run()
645655
require.NoError(t, err)
646656

647657
// Update template - 2nd option disappeared, 4th option added
648658
updatedTemplateParameters := []*proto.RichParameter{
659+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
649660
{Name: stringParameterName, Type: "string", Mutable: true, Required: true, Options: []*proto.RichParameterOption{
650-
{Name: "First option", Description: "This is first option", Value: "1st"},
651661
{Name: "Third option", Description: "This is third option", Value: "3rd"},
662+
{Name: "First option", Description: "This is first option", Value: "1st"},
652663
{Name: "Fourth option", Description: "This is fourth option", Value: "4th"},
653664
}},
654665
}
@@ -663,11 +674,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
663674
// Update the workspace
664675
inv, root = clitest.New(t, "update", "my-workspace")
665676
clitest.SetupConfig(t, client, root)
677+
doneChan := make(chan struct{})
666678
pty := ptytest.New(t).Attach(inv)
667-
clitest.Start(t, inv)
679+
go func() {
680+
defer close(doneChan)
681+
err := inv.Run()
682+
assert.NoError(t, err)
683+
}()
668684

669685
matches := []string{
670-
stringParameterName, "Third option",
686+
// `cliui.Select` will automatically pick the first option
671687
"Planning workspace...", "",
672688
}
673689
for i := 0; i < len(matches); i += 2 {
@@ -679,6 +695,8 @@ func TestUpdateValidateRichParameters(t *testing.T) {
679695
pty.WriteLine(value)
680696
}
681697
}
698+
699+
<-doneChan
682700
})
683701

684702
t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) {
@@ -742,6 +760,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
742760
pty.WriteLine(value)
743761
}
744762
}
763+
745764
<-doneChan
746765
})
747766

@@ -771,10 +790,11 @@ func TestUpdateValidateRichParameters(t *testing.T) {
771790
// Update template: add required, immutable parameter
772791
updatedTemplateParameters := []*proto.RichParameter{
773792
templateParameters[0],
793+
// The order of rich parameter options must be maintained because `cliui.Select` automatically selects the first option during tests.
774794
{Name: immutableParameterName, Type: "string", Mutable: false, Required: true, Options: []*proto.RichParameterOption{
795+
{Name: "thi", Description: "This is third option for immutable parameter", Value: "III"},
775796
{Name: "fir", Description: "This is first option for immutable parameter", Value: "I"},
776797
{Name: "sec", Description: "This is second option for immutable parameter", Value: "II"},
777-
{Name: "thi", Description: "This is third option for immutable parameter", Value: "III"},
778798
}},
779799
}
780800

@@ -797,7 +817,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
797817
}()
798818

799819
matches := []string{
800-
immutableParameterName, "thi",
820+
// `cliui.Select` will automatically pick the first option
801821
"Planning workspace...", "",
802822
}
803823
for i := 0; i < len(matches); i += 2 {
@@ -809,6 +829,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
809829
pty.WriteLine(value)
810830
}
811831
}
832+
812833
<-doneChan
813834
})
814835
}

0 commit comments

Comments
 (0)