Skip to content

Commit

Permalink
feat: test ui using golden files
Browse files Browse the repository at this point in the history
Still wip
  • Loading branch information
aymanbagabas committed Jul 26, 2023
1 parent c50ee93 commit 125ce85
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 0 deletions.
56 changes: 56 additions & 0 deletions testscript/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"flag"
"fmt"
"io"
"net"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 <stdin file>")
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 {
Expand Down
205 changes: 205 additions & 0 deletions testscript/testdata/ui-home.txtar
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 125ce85

Please sign in to comment.