From 046f3463d900dee49866b5be68d4921d5b2a169d Mon Sep 17 00:00:00 2001 From: ytimocin Date: Tue, 12 Nov 2024 15:33:26 -0800 Subject: [PATCH] Unskipping tea tests and adding protection around possible nil models Signed-off-by: ytimocin --- pkg/cli/cmd/deploy/deploy_test.go | 1 - pkg/cli/cmd/radinit/display_test.go | 42 ++++++++++---------- pkg/cli/prompt/text/text_test.go | 61 ++++++++++++++++++++++------- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/pkg/cli/cmd/deploy/deploy_test.go b/pkg/cli/cmd/deploy/deploy_test.go index 30635aa866..954b5e8a4d 100644 --- a/pkg/cli/cmd/deploy/deploy_test.go +++ b/pkg/cli/cmd/deploy/deploy_test.go @@ -527,7 +527,6 @@ func Test_Run(t *testing.T) { }) t.Run("Deployment with missing parameters", func(t *testing.T) { - //t.Skip() ctrl := gomock.NewController(t) defer ctrl.Finish() diff --git a/pkg/cli/cmd/radinit/display_test.go b/pkg/cli/cmd/radinit/display_test.go index 0a2d25c03a..774f63cac9 100644 --- a/pkg/cli/cmd/radinit/display_test.go +++ b/pkg/cli/cmd/radinit/display_test.go @@ -35,8 +35,6 @@ var ( ) func Test_summaryModel(t *testing.T) { - t.Skip("Test is flaky. See: https://github.com/radius-project/radius/issues/8044") - waitForRender := func(t *testing.T, reader io.Reader) string { normalized := "" teatest.WaitFor(t, reader, func(bts []byte) bool { @@ -47,16 +45,6 @@ func Test_summaryModel(t *testing.T) { return normalized } - waitForEmpty := func(t *testing.T, reader io.Reader) string { - normalized := "" - teatest.WaitFor(t, reader, func(bts []byte) bool { - normalized = stripansi.Strip(strings.ReplaceAll(string(bts), "\r\n", "\n")) - return !strings.Contains(normalized, strings.Trim(summaryFooter, "\n")) - }, teatest.WithDuration(waitTimeout)) - - return normalized - } - resultTest := func(t *testing.T, expected summaryResult, key tea.KeyType) { options := initOptions{} model := &summaryModel{ @@ -66,12 +54,18 @@ func Test_summaryModel(t *testing.T) { waitForRender(t, tm.Output()) - // Press ENTER - tm.Send(tea.KeyMsg{Type: key}) + // Press the given key + tm.Send(tea.KeyMsg{ + Type: key, + }) - // Wait for final render and exit. - tm.WaitFinished(t, teatest.WithFinalTimeout(waitTimeout)) - waitForEmpty(t, tm.FinalOutput(t)) + if err := tm.Quit(); err != nil { + t.Fatal(err) + } + + // FinalModel only returns once the program has finished running or when it times out. + // Please see: https://github.com/charmbracelet/x/blob/20117e9c8cd5ad229645f1bca3422b7e4110c96c/exp/teatest/teatest.go#L220. + // That is why we call tm.Quit() before tm.FinalModel(). model = tm.FinalModel(t).(*summaryModel) require.Equal(t, expected, model.result) } @@ -98,11 +92,17 @@ func Test_summaryModel(t *testing.T) { assert.Equal(t, expected, output) // Press ENTER - tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) + tm.Send(tea.KeyMsg{ + Type: tea.KeyEnter, + }) + + if err := tm.Quit(); err != nil { + t.Fatal(err) + } - // Wait for final render and exit. - tm.WaitFinished(t, teatest.WithFinalTimeout(waitTimeout)) - waitForEmpty(t, tm.FinalOutput(t)) + // FinalModel only returns once the program has finished running or when it times out. + // Please see: https://github.com/charmbracelet/x/blob/20117e9c8cd5ad229645f1bca3422b7e4110c96c/exp/teatest/teatest.go#L220. + // That is why we call tm.Quit() before tm.FinalModel(). model = tm.FinalModel(t).(*summaryModel) assert.Equal(t, summaryResult(resultConfimed), model.result) } diff --git a/pkg/cli/prompt/text/text_test.go b/pkg/cli/prompt/text/text_test.go index 268998e569..7a2df8525a 100644 --- a/pkg/cli/prompt/text/text_test.go +++ b/pkg/cli/prompt/text/text_test.go @@ -78,7 +78,6 @@ func validateNewTextModel(t *testing.T, model *Model, options *TextModelOptions) } func Test_E2E(t *testing.T) { - const expectedPrompt = "\r" + testPrompt + "\n" + "\n" + "> " + testPlaceholder + "\n" + @@ -122,35 +121,67 @@ func Test_E2E(t *testing.T) { }) t.Run("confirm default", func(t *testing.T) { - t.Skip("This test is intermittently failing in linux_amd64: https://github.com/radius-project/radius/issues/7670") tm := setup(t) + tm.Send(tea.KeyMsg{ + Type: tea.KeyEnter, + }) + + if err := tm.Quit(); err != nil { + t.Fatal(err) + } - tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) + // FinalModel only returns once the program has finished running or when it times out. + // Please see: https://github.com/charmbracelet/x/blob/20117e9c8cd5ad229645f1bca3422b7e4110c96c/exp/teatest/teatest.go#L220. + // That is why we call tm.Quit() before tm.FinalModel(). + model, ok := tm.FinalModel(t).(Model) + require.True(t, ok, "Final model should be of type Model") - require.True(t, tm.FinalModel(t).(Model).valueEntered) - require.False(t, tm.FinalModel(t).(Model).Quitting) - require.Equal(t, defaultText, tm.FinalModel(t).(Model).GetValue()) + require.True(t, model.valueEntered) + require.False(t, model.Quitting) + require.Equal(t, defaultText, model.GetValue()) }) t.Run("confirm value", func(t *testing.T) { const userInputText = "abcd" tm := setup(t) - tm.Type(userInputText) - tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) + tm.Send(tea.KeyMsg{ + Type: tea.KeyEnter, + }) - require.True(t, tm.FinalModel(t).(Model).valueEntered) - require.False(t, tm.FinalModel(t).(Model).Quitting) - require.Equal(t, userInputText, tm.FinalModel(t).(Model).GetValue()) + if err := tm.Quit(); err != nil { + t.Fatal(err) + } + + // FinalModel only returns once the program has finished running or when it times out. + // Please see: https://github.com/charmbracelet/x/blob/20117e9c8cd5ad229645f1bca3422b7e4110c96c/exp/teatest/teatest.go#L220. + // That is why we call tm.Quit() before tm.FinalModel(). + model, ok := tm.FinalModel(t).(Model) + require.True(t, ok, "Final model should be of type Model") + + require.True(t, model.valueEntered) + require.False(t, model.Quitting) + require.Equal(t, userInputText, model.GetValue()) }) t.Run("cancel", func(t *testing.T) { tm := setup(t) + tm.Send(tea.KeyMsg{ + Type: tea.KeyCtrlC, + }) + + if err := tm.Quit(); err != nil { + t.Fatal(err) + } - tm.Send(tea.KeyMsg{Type: tea.KeyCtrlC}) + // FinalModel only returns once the program has finished running or when it times out. + // Please see: https://github.com/charmbracelet/x/blob/20117e9c8cd5ad229645f1bca3422b7e4110c96c/exp/teatest/teatest.go#L220. + // That is why we call tm.Quit() before tm.FinalModel(). + model, ok := tm.FinalModel(t).(Model) + require.True(t, ok, "Final model should be of type Model") - require.False(t, tm.FinalModel(t).(Model).valueEntered) - require.True(t, tm.FinalModel(t).(Model).Quitting) - require.Equal(t, defaultText, tm.FinalModel(t).(Model).GetValue()) + require.False(t, model.valueEntered) + require.True(t, model.Quitting) + require.Equal(t, defaultText, model.GetValue()) }) }