Skip to content

Commit

Permalink
Fix prompt text test occasional failure (#7722)
Browse files Browse the repository at this point in the history
# Description

Fixes an issue in which the `confirm value` test in
`/pkg/cli/prompt/text` sometimes fails due to the timing of the text
rendering in the test model.

The test was checking the output before calling
`TestModel.FinalModel()`, which resulted in an empty result most of the
time, but not all of the time. The other assertions made after calling
`FinalModel()` showed that the code was operating as designed, which
means we can remove the interim check that tests for an empty output.
This change simply removes the code that checks the output before
calling `FinalModel()`.

## Type of change

This pull request fixes a bug in Radius and has an approved issue.

Fixes: #7721

---------

Signed-off-by: Brooke Hamilton <brooke@azuredev.io>
Co-authored-by: Brooke Hamilton <brooke@azuredev.io>
Co-authored-by: Ryan Nowak <nowakra@gmail.com>
  • Loading branch information
3 people authored and sk593 committed Jul 22, 2024
1 parent 4f6670c commit 8c70c37
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,3 @@ You can play around with the various debugger features, like stepping into code.
## Next step

- [Run tests](../first-commit-05-running-tests/index.md)

## Related Links

- [Debugging Go with VS Code](../../debugging-go-with-vscode.md)
101 changes: 42 additions & 59 deletions pkg/cli/prompt/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,68 @@ import (
"github.com/stretchr/testify/require"
)

const defaultText = "test default"
const testPrompt = "test prompt"
const testPlaceholder = "test placeholder"

var (
// Set this to a big value when debugging.
waitTimeout = 5 * time.Second
)

func Test_NewTextModel(t *testing.T) {
options := TextModelOptions{
Default: "test default",
Placeholder: "test placeholder",
Default: defaultText,
Placeholder: testPlaceholder,
Validate: func(input string) error {
return nil
},
}
model := NewTextModel("test prompt", options)
require.NotNil(t, model)
require.NotNil(t, model.textInput)

require.Equal(t, "test prompt", model.prompt)
require.Equal(t, options.Placeholder, model.textInput.Placeholder)
model := NewTextModel(testPrompt, options)

validateNewTextModel(t, &model, &options)
require.Equal(t, textinput.EchoNormal, model.textInput.EchoMode)
require.Nil(t, model.textInput.Validate) // See comments in NewTextModel.
}

func Test_NewTextModel_UpdateEchoMode(t *testing.T) {
options := TextModelOptions{
Default: "test default",
Placeholder: "test placeholder",
Default: defaultText,
Placeholder: testPlaceholder,
Validate: func(input string) error {
return nil
},
EchoMode: textinput.EchoPassword,
}
model := NewTextModel("test prompt", options)

model := NewTextModel(testPrompt, options)

validateNewTextModel(t, &model, &options)
require.Equal(t, textinput.EchoPassword, model.textInput.EchoMode)
}

func validateNewTextModel(t *testing.T, model *Model, options *TextModelOptions) {
require.NotNil(t, model)
require.NotNil(t, model.textInput)

require.Equal(t, "test prompt", model.prompt)
require.Equal(t, testPrompt, model.prompt)
require.Equal(t, options.Placeholder, model.textInput.Placeholder)
require.Equal(t, textinput.EchoPassword, model.textInput.EchoMode)
require.Nil(t, model.textInput.Validate) // See comments in NewTextModel.
}

func Test_E2E(t *testing.T) {

const expectedPrompt = "\r" + testPrompt + "\n" +
"\n" +
"> " + testPlaceholder + "\n" +
"\n" +
"(ctrl+c to quit)"

setup := func(t *testing.T) *teatest.TestModel {
options := TextModelOptions{
Default: "test default",
Placeholder: "test placeholder",
Default: defaultText,
Placeholder: testPlaceholder,
}
model := NewTextModel("test prompt", options)
model := NewTextModel(testPrompt, options)
return teatest.NewTestModel(t, model, teatest.WithInitialTermSize(18, 50))
}

Expand All @@ -100,73 +113,43 @@ func Test_E2E(t *testing.T) {
return waitForContains(t, reader, ">")
}

t.Run("confirm default", func(t *testing.T) {
t.Run("confirm prompt", func(t *testing.T) {
tm := setup(t)

output := waitForInitialRender(t, tm.Output())

require.Equal(t, expectedPrompt, output)
})

expected := "\rtest prompt\n" +
"\n" +
"> test placeholder\n" +
"\n" +
"(ctrl+c to quit)"
require.Equal(t, expected, output)
t.Run("confirm default", func(t *testing.T) {
tm := setup(t)

tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
tm.WaitFinished(t, teatest.WithFinalTimeout(waitTimeout))
bts, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

output = normalizeOutput(bts)
require.Empty(t, strings.TrimSpace(output)) // Output sometimes contains a single space.
require.True(t, tm.FinalModel(t).(Model).valueEntered)
require.False(t, tm.FinalModel(t).(Model).Quitting)
require.Equal(t, "test default", tm.FinalModel(t).(Model).GetValue())
require.Equal(t, defaultText, tm.FinalModel(t).(Model).GetValue())
})

t.Run("confirm value", func(t *testing.T) {
const userInputText = "abcd"
tm := setup(t)
output := waitForInitialRender(t, tm.Output())

expected := "\rtest prompt\n" +
"\n" +
"> test placeholder\n" +
"\n" +
"(ctrl+c to quit)"
require.Equal(t, expected, output)

tm.Type("abcd")
tm.Type(userInputText)
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
tm.WaitFinished(t, teatest.WithFinalTimeout(waitTimeout))
bts, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

output = normalizeOutput(bts)
require.Empty(t, strings.TrimSpace(output)) // Output sometimes contains a single space.
require.True(t, tm.FinalModel(t).(Model).valueEntered)
require.False(t, tm.FinalModel(t).(Model).Quitting)
require.Equal(t, "abcd", tm.FinalModel(t).(Model).GetValue())
require.Equal(t, userInputText, tm.FinalModel(t).(Model).GetValue())
})

t.Run("cancel", func(t *testing.T) {
tm := setup(t)
output := waitForInitialRender(t, tm.Output())

expected := "\rtest prompt\n" +
"\n" +
"> test placeholder\n" +
"\n" +
"(ctrl+c to quit)"
require.Equal(t, expected, output)

tm.Send(tea.KeyMsg{Type: tea.KeyCtrlC})
tm.WaitFinished(t, teatest.WithFinalTimeout(waitTimeout))
bts, err := io.ReadAll(tm.FinalOutput(t))
require.NoError(t, err)

output = normalizeOutput(bts)
require.Empty(t, strings.TrimSpace(output)) // Output sometimes contains a single space.
require.False(t, tm.FinalModel(t).(Model).valueEntered)
require.True(t, tm.FinalModel(t).(Model).Quitting)
require.Equal(t, "test default", tm.FinalModel(t).(Model).GetValue())
require.Equal(t, defaultText, tm.FinalModel(t).(Model).GetValue())
})
}

0 comments on commit 8c70c37

Please sign in to comment.