From 9cc7464ad49c60552abeb829f25d677fcb1deace Mon Sep 17 00:00:00 2001 From: Brooke Hamilton Date: Mon, 1 Jul 2024 20:43:02 +0000 Subject: [PATCH 1/3] Remove output check before final rendering of test model Fixes issue #7721 Signed-off-by: Brooke Hamilton --- pkg/cli/prompt/text/text_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/cli/prompt/text/text_test.go b/pkg/cli/prompt/text/text_test.go index f64fc9d337..ba1ea56015 100644 --- a/pkg/cli/prompt/text/text_test.go +++ b/pkg/cli/prompt/text/text_test.go @@ -137,11 +137,7 @@ func Test_E2E(t *testing.T) { tm.Type("abcd") 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()) From 7640376c247d214f7359db54f7d280d76cc4259e Mon Sep 17 00:00:00 2001 From: Brooke Hamilton Date: Tue, 2 Jul 2024 20:54:38 +0000 Subject: [PATCH 2/3] Remove invalid doc link Signed-off-by: Brooke Hamilton --- .../first-commit-04-debugging-cli/index.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/contributing/contributing-code/contributing-code-first-commit/first-commit-04-debugging-cli/index.md b/docs/contributing/contributing-code/contributing-code-first-commit/first-commit-04-debugging-cli/index.md index c002fe429b..8fe8b7edd4 100644 --- a/docs/contributing/contributing-code/contributing-code-first-commit/first-commit-04-debugging-cli/index.md +++ b/docs/contributing/contributing-code/contributing-code-first-commit/first-commit-04-debugging-cli/index.md @@ -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) From ca96751c3f94a1a3a8b945fd98eb97facecf647f Mon Sep 17 00:00:00 2001 From: Brooke Hamilton Date: Wed, 3 Jul 2024 21:35:59 +0000 Subject: [PATCH 3/3] Refactor and update tests Signed-off-by: Brooke Hamilton --- pkg/cli/prompt/text/text_test.go | 97 ++++++++++++++------------------ 1 file changed, 42 insertions(+), 55 deletions(-) diff --git a/pkg/cli/prompt/text/text_test.go b/pkg/cli/prompt/text/text_test.go index ba1ea56015..e2048a8614 100644 --- a/pkg/cli/prompt/text/text_test.go +++ b/pkg/cli/prompt/text/text_test.go @@ -29,6 +29,10 @@ 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 @@ -36,48 +40,57 @@ var ( 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)) } @@ -100,69 +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)) 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()) }) }