diff --git a/exp/teatest/v2/app_test.go b/exp/teatest/v2/app_test.go index 1ac0500c..d071ff55 100644 --- a/exp/teatest/v2/app_test.go +++ b/exp/teatest/v2/app_test.go @@ -35,7 +35,7 @@ func TestApp(t *testing.T) { t.Fatal(err) } - out := tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second)) + out := teatest.TrimEmptyLines(tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second))) if !regexp.MustCompile(`This program will exit in \d+ seconds`).MatchString(out) { t.Fatalf("output does not match the given regular expression: %q", out) } diff --git a/exp/teatest/v2/send_test.go b/exp/teatest/v2/send_test.go index 31f42ba1..4667f465 100644 --- a/exp/teatest/v2/send_test.go +++ b/exp/teatest/v2/send_test.go @@ -39,8 +39,8 @@ func TestAppSendToOtherProgram(t *testing.T) { tm1.Type("q") tm2.Type("q") - out1 := tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second)) - out2 := tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second)) + out1 := teatest.TrimEmptyLines(tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second))) + out2 := teatest.TrimEmptyLines(tm2.FinalOutput(t, teatest.WithFinalTimeout(time.Second))) if string(out1) != string(out2) { t.Errorf("output of both models should be the same, got:\n%v\nand:\n%v\n", string(out1), string(out2)) diff --git a/exp/teatest/v2/teatest.go b/exp/teatest/v2/teatest.go index 90023f9b..f390fabe 100644 --- a/exp/teatest/v2/teatest.go +++ b/exp/teatest/v2/teatest.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/signal" + "strings" "sync" "syscall" "testing" @@ -154,19 +155,23 @@ func NewTestModel(tb testing.TB, m tea.Model, options ...TestOption) *TestModel return tm } -func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) { +func mergeOpts(opts []FinalOpt) FinalOpts { + r := FinalOpts{} + for _, opt := range opts { + opt(&r) + } + return r +} + +func (tm *TestModel) waitDone(tb testing.TB, opts FinalOpts) { tm.done.Do(func() { - fopts := FinalOpts{} - for _, opt := range opts { - opt(&fopts) - } - if fopts.timeout > 0 { + if opts.timeout > 0 { select { - case <-time.After(fopts.timeout): - if fopts.onTimeout == nil { - tb.Fatalf("timeout after %s", fopts.timeout) + case <-time.After(opts.timeout): + if opts.onTimeout == nil { + tb.Fatalf("timeout after %s", opts.timeout) } - fopts.onTimeout(tb) + opts.onTimeout(tb) case <-tm.doneCh: } } else { @@ -179,6 +184,7 @@ func (tm *TestModel) waitDone(tb testing.TB, opts []FinalOpt) { type FinalOpts struct { timeout time.Duration onTimeout func(tb testing.TB) + trim bool } // FinalOpt changes FinalOpts. @@ -203,14 +209,14 @@ func WithFinalTimeout(d time.Duration) FinalOpt { // This method only returns once the program has finished running or when it // times out. func (tm *TestModel) WaitFinished(tb testing.TB, opts ...FinalOpt) { - tm.waitDone(tb, opts) + tm.waitDone(tb, mergeOpts(opts)) } // FinalModel returns the resulting model, resulting from program.Run(). // This method only returns once the program has finished running or when it // times out. func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model { - tm.waitDone(tb, opts) + tm.waitDone(tb, mergeOpts(opts)) select { case m := <-tm.modelCh: tm.model = m @@ -224,7 +230,8 @@ func (tm *TestModel) FinalModel(tb testing.TB, opts ...FinalOpt) tea.Model { // This method only returns once the program has finished running or when it // times out. func (tm *TestModel) FinalOutput(tb testing.TB, opts ...FinalOpt) string { - tm.waitDone(tb, opts) + opt := mergeOpts(opts) + tm.waitDone(tb, opt) return tm.Output() } @@ -269,3 +276,15 @@ func RequireEqualOutput(tb testing.TB, out string) { tb.Helper() golden.RequireEqualEscape(tb, []byte(out), true) } + +// TrimEmptyLines removes trailing empty lines from the given output. +func TrimEmptyLines(out string) string { + // trim empty trailing lines from the output + lines := strings.Split(out, "\n") + for i := len(lines) - 1; i >= 0; i-- { + if strings.TrimSpace(lines[i]) != "" { + return strings.Join(lines[:i], "\n") + } + } + return out +} diff --git a/exp/teatest/v2/testdata/TestApp.golden b/exp/teatest/v2/testdata/TestApp.golden index 12b60376..a728107f 100644 --- a/exp/teatest/v2/testdata/TestApp.golden +++ b/exp/teatest/v2/testdata/TestApp.golden @@ -1,3 +1 @@ -[?25l[?2004h[?2027h[?2027$p Hi. This program will exit in 10 seconds. To quit sooner press any key -Hi. This program will exit in 9 seconds. To quit sooner press any key. - [?2004l[?25h \ No newline at end of file +Hi. This program will exit in 10 seconds. To quit sooner press any key \ No newline at end of file diff --git a/exp/teatest/v2/testdata/TestAppSendToOtherProgram.golden b/exp/teatest/v2/testdata/TestAppSendToOtherProgram.golden index 0e196c51..6ba5f64c 100644 --- a/exp/teatest/v2/testdata/TestAppSendToOtherProgram.golden +++ b/exp/teatest/v2/testdata/TestAppSendToOtherProgram.golden @@ -1,7 +1,6 @@ -[?25l[?2004h[?2027h[?2027$p All pings: +All pings: from m1 from m1 from m2 from m2 -from m2 -from m2 [?2004l[?25h \ No newline at end of file +from m2 \ No newline at end of file