Skip to content

fix: wait for prompt on rich param CLI test #15406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
Merged

Conversation

spikecurtis
Copy link
Contributor

@spikecurtis spikecurtis commented Nov 6, 2024

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\""

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @spikecurtis and the rest of your teammates on Graphite Graphite

@spikecurtis spikecurtis marked this pull request as ready for review November 6, 2024 13:08
@spikecurtis spikecurtis requested a review from mtojek November 6, 2024 13:09
@@ -198,7 +198,7 @@ func (e *outExpecter) expectMatcherFunc(ctx context.Context, str string, fn func
e.fatalf("read error", "%v (wanted %q; got %q)", err, str, buffer.String())
return ""
}
e.logf("matched %q = %q", str, stripansi.Strip(buffer.String()))
e.logf("matched %q = %q", str, buffer.String())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be misleading, since we don't strip the the string of ANSI codes when matching.

}
<-doneChan
pty.ExpectMatch(stringParameterName)
pty.ExpectMatch("> Enter a value (default: \"\"): ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the rationale behind proposing this matcher. However, I'm a bit surprised to observe that stdin isn't buffered when input arrives prematurely. Is this the intended behavior, or are you exploring different matcher configurations to address the issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good point. I looked into this more and there is a race in cliui.Prompt that causes input to be lost in cases where input is being sent too early and we fail validation.

coder/internal#203

I'll separately work on a fix for this ^^

I still think this PR is an improvement: in many cases the explicit matches are fewer lines than the looping construct, and are closer to the timing a human would have.

It also prevents these tests from deadlocking on failure via the bare <-doneChan read.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, mitigating the risk of deadlocks is certainly valuable here.

As for the explicit matchers, I also find them preferable from a readability standpoint; they make the intent clear and reduce ambiguity, allowing developers to quickly grasp the expected input without needing to infer it.

@spikecurtis spikecurtis requested a review from mtojek November 7, 2024 05:48
}
<-doneChan
pty.ExpectMatch(stringParameterName)
pty.ExpectMatch("> Enter a value (default: \"\"): ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, mitigating the risk of deadlocks is certainly valuable here.

As for the explicit matchers, I also find them preferable from a readability standpoint; they make the intent clear and reduce ambiguity, allowing developers to quickly grasp the expected input without needing to infer it.

@spikecurtis spikecurtis merged commit cee6b1e into main Nov 7, 2024
27 checks passed
@spikecurtis spikecurtis deleted the spike/cli-rich-param branch November 7, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants