Skip to content

Commit d1ec57e

Browse files
spikecurtispull[bot]
authored andcommitted
fix: wait for prompt on rich param CLI test (#15406)
Fixes a race in TestUpdateValidateRichParameters where the parameter is sent prior to the prompt. Causes errors like: https://github.com/coder/coder/actions/runs/11681622439/job/32527173007 ``` ptytest.go:132: 2024-11-05 10:02:18.819: cmd: "bool_parameter" ptytest.go:167: 2024-11-05 10:02:18.819: cmd: matched "bool_parameter" = "bool_parameter" update_test.go:440: 2024-11-05 10:02:18.819: cmd: stdin: "cat\r\n" ptytest.go:132: 2024-11-05 10:02:18.819: cmd: "> Enter a value (default: \"\"): can't validate build parameter \"bool_parameter\": boolean value can be either \"true\" or \"false\"" ptytest.go:167: 2024-11-05 10:02:18.819: cmd: matched "boolean value can be either" = "\n> Enter a value (default: \"\"): can't validate build parameter \"bool_parameter\": boolean value can be either" update_test.go:440: 2024-11-05 10:02:18.819: cmd: stdin: "\r\n" ptytest.go:167: 2024-11-05 10:02:18.819: cmd: matched "Enter a value" = " \"true\" or \"false\"\n> Enter a value" update_test.go:440: 2024-11-05 10:02:18.819: cmd: stdin: "false\r\n" ptytest.go:132: 2024-11-05 10:02:18.821: cmd: "> Enter a value (default: \"\"): can't validate build parameter \"bool_parameter\": boolean value can be either \"true\" or \"false\"" ```
1 parent dfbcda8 commit d1ec57e

File tree

2 files changed

+58
-47
lines changed

2 files changed

+58
-47
lines changed

cli/update_test.go

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
323323
err := inv.Run()
324324
require.NoError(t, err)
325325

326+
ctx := testutil.Context(t, testutil.WaitLong)
326327
inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt")
328+
inv = inv.WithContext(ctx)
327329
clitest.SetupConfig(t, member, root)
328330
doneChan := make(chan struct{})
329331
pty := ptytest.New(t).Attach(inv)
@@ -333,18 +335,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
333335
assert.NoError(t, err)
334336
}()
335337

336-
matches := []string{
337-
stringParameterName, "$$",
338-
"does not match", "",
339-
"Enter a value", "abc",
340-
}
341-
for i := 0; i < len(matches); i += 2 {
342-
match := matches[i]
343-
value := matches[i+1]
344-
pty.ExpectMatch(match)
345-
pty.WriteLine(value)
346-
}
347-
<-doneChan
338+
pty.ExpectMatch(stringParameterName)
339+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
340+
pty.WriteLine("$$")
341+
pty.ExpectMatch("does not match")
342+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
343+
pty.WriteLine("")
344+
pty.ExpectMatch("does not match")
345+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
346+
pty.WriteLine("abc")
347+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
348348
})
349349

350350
t.Run("ValidateNumber", func(t *testing.T) {
@@ -369,7 +369,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
369369
err := inv.Run()
370370
require.NoError(t, err)
371371

372+
ctx := testutil.Context(t, testutil.WaitLong)
372373
inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt")
374+
inv.WithContext(ctx)
373375
clitest.SetupConfig(t, member, root)
374376
doneChan := make(chan struct{})
375377
pty := ptytest.New(t).Attach(inv)
@@ -379,21 +381,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
379381
assert.NoError(t, err)
380382
}()
381383

382-
matches := []string{
383-
numberParameterName, "12",
384-
"is more than the maximum", "",
385-
"Enter a value", "8",
386-
}
387-
for i := 0; i < len(matches); i += 2 {
388-
match := matches[i]
389-
value := matches[i+1]
390-
pty.ExpectMatch(match)
391-
392-
if value != "" {
393-
pty.WriteLine(value)
394-
}
395-
}
396-
<-doneChan
384+
pty.ExpectMatch(numberParameterName)
385+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
386+
pty.WriteLine("12")
387+
pty.ExpectMatch("is more than the maximum")
388+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
389+
pty.WriteLine("")
390+
pty.ExpectMatch("is not a number")
391+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
392+
pty.WriteLine("8")
393+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
397394
})
398395

399396
t.Run("ValidateBool", func(t *testing.T) {
@@ -418,7 +415,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
418415
err := inv.Run()
419416
require.NoError(t, err)
420417

418+
ctx := testutil.Context(t, testutil.WaitLong)
421419
inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt")
420+
inv = inv.WithContext(ctx)
422421
clitest.SetupConfig(t, member, root)
423422
doneChan := make(chan struct{})
424423
pty := ptytest.New(t).Attach(inv)
@@ -428,18 +427,16 @@ func TestUpdateValidateRichParameters(t *testing.T) {
428427
assert.NoError(t, err)
429428
}()
430429

431-
matches := []string{
432-
boolParameterName, "cat",
433-
"boolean value can be either", "",
434-
"Enter a value", "false",
435-
}
436-
for i := 0; i < len(matches); i += 2 {
437-
match := matches[i]
438-
value := matches[i+1]
439-
pty.ExpectMatch(match)
440-
pty.WriteLine(value)
441-
}
442-
<-doneChan
430+
pty.ExpectMatch(boolParameterName)
431+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
432+
pty.WriteLine("cat")
433+
pty.ExpectMatch("boolean value can be either \"true\" or \"false\"")
434+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
435+
pty.WriteLine("")
436+
pty.ExpectMatch("boolean value can be either \"true\" or \"false\"")
437+
pty.ExpectMatch("> Enter a value (default: \"\"): ")
438+
pty.WriteLine("false")
439+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
443440
})
444441

445442
t.Run("RequiredParameterAdded", func(t *testing.T) {
@@ -485,7 +482,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
485482
require.NoError(t, err)
486483

487484
// Update the workspace
485+
ctx := testutil.Context(t, testutil.WaitLong)
488486
inv, root = clitest.New(t, "update", "my-workspace")
487+
inv.WithContext(ctx)
489488
clitest.SetupConfig(t, member, root)
490489
doneChan := make(chan struct{})
491490
pty := ptytest.New(t).Attach(inv)
@@ -508,7 +507,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
508507
pty.WriteLine(value)
509508
}
510509
}
511-
<-doneChan
510+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
512511
})
513512

514513
t.Run("OptionalParameterAdded", func(t *testing.T) {
@@ -555,7 +554,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
555554
require.NoError(t, err)
556555

557556
// Update the workspace
557+
ctx := testutil.Context(t, testutil.WaitLong)
558558
inv, root = clitest.New(t, "update", "my-workspace")
559+
inv.WithContext(ctx)
559560
clitest.SetupConfig(t, member, root)
560561
doneChan := make(chan struct{})
561562
pty := ptytest.New(t).Attach(inv)
@@ -566,7 +567,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
566567
}()
567568

568569
pty.ExpectMatch("Planning workspace...")
569-
<-doneChan
570+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
570571
})
571572

572573
t.Run("ParameterOptionChanged", func(t *testing.T) {
@@ -612,7 +613,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
612613
require.NoError(t, err)
613614

614615
// Update the workspace
616+
ctx := testutil.Context(t, testutil.WaitLong)
615617
inv, root = clitest.New(t, "update", "my-workspace")
618+
inv.WithContext(ctx)
616619
clitest.SetupConfig(t, member, root)
617620
doneChan := make(chan struct{})
618621
pty := ptytest.New(t).Attach(inv)
@@ -636,7 +639,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
636639
}
637640
}
638641

639-
<-doneChan
642+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
640643
})
641644

642645
t.Run("ParameterOptionDisappeared", func(t *testing.T) {
@@ -683,7 +686,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
683686
require.NoError(t, err)
684687

685688
// Update the workspace
689+
ctx := testutil.Context(t, testutil.WaitLong)
686690
inv, root = clitest.New(t, "update", "my-workspace")
691+
inv.WithContext(ctx)
687692
clitest.SetupConfig(t, member, root)
688693
doneChan := make(chan struct{})
689694
pty := ptytest.New(t).Attach(inv)
@@ -707,7 +712,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
707712
}
708713
}
709714

710-
<-doneChan
715+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
711716
})
712717

713718
t.Run("ParameterOptionFailsMonotonicValidation", func(t *testing.T) {
@@ -739,7 +744,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
739744
require.NoError(t, err)
740745

741746
// Update the workspace
747+
ctx := testutil.Context(t, testutil.WaitLong)
742748
inv, root = clitest.New(t, "update", "my-workspace", "--always-prompt=true")
749+
inv.WithContext(ctx)
743750
clitest.SetupConfig(t, member, root)
744751

745752
doneChan := make(chan struct{})
@@ -762,7 +769,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
762769
pty.ExpectMatch(match)
763770
}
764771

765-
<-doneChan
772+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
766773
})
767774

768775
t.Run("ImmutableRequiredParameterExists_MutableRequiredParameterAdded", func(t *testing.T) {
@@ -804,7 +811,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
804811
require.NoError(t, err)
805812

806813
// Update the workspace
814+
ctx := testutil.Context(t, testutil.WaitLong)
807815
inv, root = clitest.New(t, "update", "my-workspace")
816+
inv.WithContext(ctx)
808817
clitest.SetupConfig(t, member, root)
809818
doneChan := make(chan struct{})
810819
pty := ptytest.New(t).Attach(inv)
@@ -828,7 +837,7 @@ func TestUpdateValidateRichParameters(t *testing.T) {
828837
}
829838
}
830839

831-
<-doneChan
840+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
832841
})
833842

834843
t.Run("MutableRequiredParameterExists_ImmutableRequiredParameterAdded", func(t *testing.T) {
@@ -874,7 +883,9 @@ func TestUpdateValidateRichParameters(t *testing.T) {
874883
require.NoError(t, err)
875884

876885
// Update the workspace
886+
ctx := testutil.Context(t, testutil.WaitLong)
877887
inv, root = clitest.New(t, "update", "my-workspace")
888+
inv.WithContext(ctx)
878889
clitest.SetupConfig(t, member, root)
879890
doneChan := make(chan struct{})
880891
pty := ptytest.New(t).Attach(inv)
@@ -898,6 +909,6 @@ func TestUpdateValidateRichParameters(t *testing.T) {
898909
}
899910
}
900911

901-
<-doneChan
912+
_ = testutil.RequireRecvCtx(ctx, t, doneChan)
902913
})
903914
}

pty/ptytest/ptytest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (e *outExpecter) expectMatcherFunc(ctx context.Context, str string, fn func
198198
e.fatalf("read error", "%v (wanted %q; got %q)", err, str, buffer.String())
199199
return ""
200200
}
201-
e.logf("matched %q = %q", str, stripansi.Strip(buffer.String()))
201+
e.logf("matched %q = %q", str, buffer.String())
202202
return buffer.String()
203203
}
204204

0 commit comments

Comments
 (0)