From 125ce856d8aeb55c5c1e54ad7c37c9032079f89f Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 26 Jul 2023 19:38:08 -0400 Subject: [PATCH] feat: test ui using golden files Still wip --- testscript/script_test.go | 56 ++++++++ testscript/testdata/ui-home.txtar | 205 ++++++++++++++++++++++++++++++ 2 files changed, 261 insertions(+) create mode 100644 testscript/testdata/ui-home.txtar diff --git a/testscript/script_test.go b/testscript/script_test.go index 8c9e3ceae..274006cd4 100644 --- a/testscript/script_test.go +++ b/testscript/script_test.go @@ -5,6 +5,7 @@ import ( "context" "flag" "fmt" + "io" "net" "os" "path/filepath" @@ -13,6 +14,7 @@ import ( "testing" "time" + tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/keygen" "github.com/charmbracelet/log" "github.com/charmbracelet/soft-serve/server" @@ -52,6 +54,8 @@ func TestScript(t *testing.T) { Dir: "./testdata/", UpdateScripts: *update, Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ + "ui": cmdUI(admin1.Signer()), + "uui": cmdUI(user1.Signer()), "soft": cmdSoft(admin1.Signer()), "usoft": cmdSoft(user1.Signer()), "git": cmdGit(key), @@ -198,6 +202,58 @@ func cmdSoft(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []st } } +func cmdUI(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []string) { + return func(ts *testscript.TestScript, neg bool, args []string) { + if len(args) < 1 { + ts.Fatalf("usage: ui ") + return + } + + cli, err := ssh.Dial( + "tcp", + net.JoinHostPort("localhost", ts.Getenv("SSH_PORT")), + &ssh.ClientConfig{ + User: "admin", + Auth: []ssh.AuthMethod{ssh.PublicKeys(key)}, + HostKeyCallback: ssh.InsecureIgnoreHostKey(), + }, + ) + ts.Check(err) + defer cli.Close() + + sess, err := cli.NewSession() + ts.Check(err) + defer sess.Close() + + // XXX: this is a hack to make the UI tests work + // cmp command always complains about an extra newline + // in the output + defer ts.Stdout().Write([]byte("\n")) + + sess.Stdout = ts.Stdout() + sess.Stderr = ts.Stderr() + + stdin, err := sess.StdinPipe() + ts.Check(err) + + go func() { + defer stdin.Close() + in := ts.ReadFile(args[0]) + _, err := io.Copy(stdin, strings.NewReader(in)) + ts.Check(err) + }() + + err = sess.RequestPty("xterm-256color", 40, 80, ssh.TerminalModes{}) + ts.Check(err) + + check(ts, sess.Run(""), neg) + } +} + +var keyToRune = map[string]rune{ + "esc": rune(tea.KeyEscape), +} + // P.S. Windows sucks! func cmdDos2Unix(ts *testscript.TestScript, neg bool, args []string) { if neg { diff --git a/testscript/testdata/ui-home.txtar b/testscript/testdata/ui-home.txtar new file mode 100644 index 000000000..1b5ebb92a --- /dev/null +++ b/testscript/testdata/ui-home.txtar @@ -0,0 +1,205 @@ +# vi: set ft=conf + +# convert crlf to lf on windows +[windows] dos2unix home-in.txt home.txt about-in.txt about.txt + +# test repositories tab +ui home-in.txt +cmp stdout home.txt + +# test about tab +ui about-in.txt +cmp stdout about.txt + +# add a new repo +soft repo create .soft-serve -n 'Config' -d '"Test Soft Serve"' +soft repo description .soft-serve +stdout 'Test Soft Serve' +soft repo project-name .soft-serve +stdout 'Config' + +# clone repo +git clone ssh://localhost:$SSH_PORT/.soft-serve config + +# create readme file +mkfile ./config/README.md '# Hello World\nTest Soft Serve' +git -C config add -A +git -C config commit -m 'Initial commit' +git -C config push origin HEAD + +# test repositories tab +ui home-in.txt +cmpenv stdout home2.txt + +# test about tab +ui about-in.txt +cmp stdout about2.txt + +-- home-in.txt -- + q +-- home.txt -- +[?25l[?1049h[?25l[?1002h +  Test Soft Serve  + + • Repositories About + + No items found. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ↑↓ navigate • tab section • enter select • c copy command • q quit … + [?25h[?1002l[?1003l[?1049l[?25h +-- home2.txt -- +[?25l[?1049h[?25l[?1002h +  Test Soft Serve  + + • Repositories About + + ┃ Config   Updated now + ┃ Test Soft Serve + ┃ git clone ssh://localhost:$SSH_PORT/.soft-serve.git + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ↑↓ navigate • tab section • enter select • / filter • c copy command … + [?25h[?1002l[?1003l[?1049l[?25h +-- about-in.txt -- + q +-- about.txt -- +[?25l[?1049h[?25l[?1002h +  Test Soft Serve  + + Repositories • About + + No readme found. +  + Create a `.soft-serve` repository and add a `README.md` file to display re + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ☰ 100% + + ↑↓ navigate • tab section • q quit • ? toggle help + [?25h[?1002l[?1003l[?1049l[?25h +-- about2.txt -- +[?25l[?1049h[?25l[?1002h +  Test Soft Serve  + + Repositories • About + +  # Hello World\nTest Soft Serve + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ☰ 100% + + ↑↓ navigate • tab section • q quit • ? toggle help + [?25h[?1002l[?1003l[?1049l[?25h