Skip to content
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

feat(teatest/v2): use virtual terminal #250

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions exp/teatest/v2/app_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package teatest_test

import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -35,9 +35,45 @@ func TestApp(t *testing.T) {
t.Fatal(err)
}

out := readBts(t, tm.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
if !regexp.MustCompile(`This program will exit in \d+ seconds`).Match(out) {
t.Fatalf("output does not match the given regular expression: %s", string(out))
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)
}
teatest.RequireEqualOutput(t, out)

if tm.FinalModel(t).(model) != 9 {
t.Errorf("expected model to be 10, was %d", m)
}
}

func TestAppAltScreen(t *testing.T) {
t.Skip("needs changes in /vt")
m := model(10)
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(70, 30),
teatest.WithProgramOptions(tea.WithAltScreen()),
)
t.Cleanup(func() {
if err := tm.Quit(); err != nil {
t.Fatal(err)
}
})

time.Sleep(time.Second + time.Millisecond*200)
tm.Type("I'm typing things, but it'll be ignored by my program")
tm.Send("ignored msg")
tm.Send(tea.KeyPressMsg{
Code: tea.KeyEnter,
})

if err := tm.Quit(); err != nil {
t.Fatal(err)
}

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)
}
teatest.RequireEqualOutput(t, out)

Expand All @@ -56,12 +92,12 @@ func TestAppInteractive(t *testing.T) {
time.Sleep(time.Second + time.Millisecond*200)
tm.Send("ignored msg")

if bts := readBts(t, tm.Output()); !bytes.Contains(bts, []byte("This program will exit in 9 seconds")) {
t.Fatalf("output does not match: expected %q", string(bts))
if s := tm.Output(); !strings.Contains(s, "This program will exit in 9 seconds") {
t.Fatalf("output does not match: expected %q", string(s))
}

teatest.WaitFor(t, tm.Output(), func(out []byte) bool {
return bytes.Contains(out, []byte("This program will exit in 7 seconds"))
teatest.WaitForOutput(t, tm, func(s string) bool {
return strings.Contains(s, "This program will exit in 7 seconds")
}, teatest.WithDuration(5*time.Second), teatest.WithCheckInterval(time.Millisecond*10))

tm.Send(tea.KeyPressMsg{
Expand Down
19 changes: 10 additions & 9 deletions exp/teatest/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ module github.com/charmbracelet/x/exp/teatest/v2
go 1.19

require (
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241113134142-c71ad13e23d6
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a
github.com/charmbracelet/x/vt v0.0.0-20241113201228-12f9eedf8aa6
)

require (
github.com/aymanbagabas/go-udiff v0.2.0 // indirect
github.com/charmbracelet/colorprofile v0.1.6 // indirect
github.com/charmbracelet/x/ansi v0.4.3 // indirect
github.com/charmbracelet/x/cellbuf v0.0.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 // indirect
github.com/charmbracelet/colorprofile v0.1.7 // indirect
github.com/charmbracelet/x/ansi v0.4.6-0.20241113201228-12f9eedf8aa6 // indirect
github.com/charmbracelet/x/cellbuf v0.0.6-0.20241113201228-12f9eedf8aa6 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32 // indirect
github.com/charmbracelet/x/windows v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
)
38 changes: 20 additions & 18 deletions exp/teatest/v2/go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8=
github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2 h1:NkQFWhCii9NtL7Q0L/4mNKtZFgrDpfPSVZAzTwEJdGg=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2/go.mod h1:24niqT9RbtXhWg8zLRU/v/xTixlo1+DUsHQZ3+kez5Y=
github.com/charmbracelet/colorprofile v0.1.6 h1:nMMqCns0c0DfCwNGdagBh6SxutFqkltSxxKk5S9kt+Y=
github.com/charmbracelet/colorprofile v0.1.6/go.mod h1:3EMXDxwRDJl0c17eJ1jX99MhtlP9OxE/9Qw0C5lvyUg=
github.com/charmbracelet/x/ansi v0.4.3 h1:wcdDrW0ejaaZGJxCyxVNzzmctqV+oARIudaFGQvsRkA=
github.com/charmbracelet/x/ansi v0.4.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/cellbuf v0.0.3 h1:HapUUjlo0pZ7iGijrTer1f4X8Uvq17l0zR+80Oh+iJg=
github.com/charmbracelet/x/cellbuf v0.0.3/go.mod h1:SF8R3AqchNzYKKJCFT7co8wt1HgQDfAitQ+SBoxWLNc=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241113134142-c71ad13e23d6 h1:kRj022q2jfr69oRZNnhev/Em44M7/TjV7jvWpyQ9PMo=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20241113134142-c71ad13e23d6/go.mod h1:Az92EQe8w9w+TgIPiTjbZtVohnlxwHiVDNJMPUTSg2o=
github.com/charmbracelet/colorprofile v0.1.7 h1:q7PtMQrRBBnLNE2EbtbNUtouu979EivKcDGGaimhyO8=
github.com/charmbracelet/colorprofile v0.1.7/go.mod h1:d3UYToTrNmsD2p9/lbiya16H1WahndM0miDlJWXWf4U=
github.com/charmbracelet/x/ansi v0.4.6-0.20241113201228-12f9eedf8aa6 h1:4OG3+4CUSzOK0vdg6kkXQksLoh1d3zOnBfHKTOnnnBg=
github.com/charmbracelet/x/ansi v0.4.6-0.20241113201228-12f9eedf8aa6/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/cellbuf v0.0.6-0.20241113201228-12f9eedf8aa6 h1:7YvO1RgEvUkX0gx5++X/lG+DoU8MPYjjPYJKb6b7tFk=
github.com/charmbracelet/x/cellbuf v0.0.6-0.20241113201228-12f9eedf8aa6/go.mod h1:+a10wKGeO6TKYBbvxCfQEEgn5knWzoZtLf00oiXgvBg=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/charmbracelet/x/vt v0.0.0-20241113201228-12f9eedf8aa6 h1:tIcT/TeXoDcmyMKE0x0hh2wE0SCpev6Q2mEACNwXRKI=
github.com/charmbracelet/x/vt v0.0.0-20241113201228-12f9eedf8aa6/go.mod h1:+CYC0tzYqYMtIryA0lcGQgCUaAiRLaS7Rxi9R+PFii8=
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32 h1:14czE6R5CgOlvONsJYa2B1uTyLvXzGXpBqw2AyZeTh4=
github.com/charmbracelet/x/wcwidth v0.0.0-20241113152101-0af7d04e9f32/go.mod h1:hyua5CY63kyl7IfyIxv1SjVEqoKze/XmDkEglItuVjA=
github.com/charmbracelet/x/windows v0.2.0 h1:ilXA1GJjTNkgOm94CLPeSz7rar54jtFatdmoiONPuEw=
github.com/charmbracelet/x/windows v0.2.0/go.mod h1:ZibNFR49ZFqCXgP76sYanisxRyC+EYrBE7TTknD8s1s=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand All @@ -25,9 +27,9 @@ github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
4 changes: 2 additions & 2 deletions exp/teatest/v2/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func TestAppSendToOtherProgram(t *testing.T) {
tm1.Type("q")
tm2.Type("q")

out1 := readBts(t, tm1.FinalOutput(t, teatest.WithFinalTimeout(time.Second)))
out2 := readBts(t, 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))
Expand Down
Loading
Loading