Skip to content

Commit

Permalink
feat(tests): run against ssh ui
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 7, 2023
1 parent 1a834e4 commit 7e2f1d6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 122 deletions.
7 changes: 7 additions & 0 deletions cmd/soft/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"runtime/debug"
"strconv"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/cmd/soft/admin"
Expand All @@ -13,9 +14,11 @@ import (
"github.com/charmbracelet/soft-serve/cmd/soft/serve"
"github.com/charmbracelet/soft-serve/pkg/config"
logr "github.com/charmbracelet/soft-serve/pkg/log"
"github.com/charmbracelet/soft-serve/pkg/ui/common"
"github.com/charmbracelet/soft-serve/pkg/version"
mcobra "github.com/muesli/mango-cobra"
"github.com/muesli/roff"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
"go.uber.org/automaxprocs/maxprocs"
)
Expand Down Expand Up @@ -63,6 +66,10 @@ var (
)

func init() {
if noColor, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_NO_COLOR")); noColor {
common.DefaultColorProfile = termenv.Ascii
}

rootCmd.AddCommand(
manCmd,
serve.Command,
Expand Down
37 changes: 18 additions & 19 deletions testscript/script_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package testscript

import (
"bufio"
"bytes"
"context"
"encoding/json"
Expand All @@ -16,11 +15,11 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/keygen"
"github.com/charmbracelet/soft-serve/pkg/config"
"github.com/charmbracelet/soft-serve/pkg/db"
Expand Down Expand Up @@ -124,6 +123,9 @@ func TestScript(t *testing.T) {
// This is used to set up test specific configuration and http endpoints
e.Setenv("SOFT_SERVE_TESTRUN", "1")

// This will disable the default lipgloss renderer colors
e.Setenv("SOFT_SERVE_NO_COLOR", "1")

// Soft Serve debug environment variables
for _, env := range []string{
"SOFT_SERVE_DEBUG",
Expand Down Expand Up @@ -206,24 +208,24 @@ 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>")
ts.Fatalf("usage: ui <quoted string input>")
return
}

cli, err := ssh.Dial(
"tcp",
net.JoinHostPort("localhost", ts.Getenv("SSH_PORT")),
&ssh.ClientConfig{
User: "admin",
User: "git",
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
},
)
ts.Check(err)
check(ts, err, neg)
defer cli.Close()

sess, err := cli.NewSession()
ts.Check(err)
check(ts, err, neg)
defer sess.Close()

// XXX: this is a hack to make the UI tests work
Expand All @@ -235,35 +237,32 @@ func cmdUI(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []stri
sess.Stderr = ts.Stderr()

stdin, err := sess.StdinPipe()
ts.Check(err)
check(ts, err, neg)

in, err := strconv.Unquote(args[0])
check(ts, err, neg)
reader := strings.NewReader(in)
go func() {
defer stdin.Close()
in := ts.ReadFile(args[0])
reader := bufio.NewReader(strings.NewReader(in))
for {
r, _, err := reader.ReadRune()
if err == io.EOF {
break
}
ts.Check(err)
stdin.Write([]byte{byte(r)}) // nolint: errcheck
check(ts, err, neg)
stdin.Write([]byte(string(r))) // nolint: errcheck

// Wait for the UI to process the input
<-time.After(100 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
}
}()

err = sess.RequestPty("xterm-256color", 40, 80, ssh.TerminalModes{})
ts.Check(err)

err = sess.RequestPty("dumb", 40, 80, ssh.TerminalModes{})
check(ts, err, neg)
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
2 changes: 1 addition & 1 deletion testscript/testdata/repo-blob.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ stderr 'revision does not exist'
-- blob1.txt --
# Hello\n\nwelcome
-- blob2.txt --
1 │ package main\nconst foo = 2\n
1 │ package main\nconst foo = 2\n
-- blob3.txt --
1 │ //#include <stdio.h>
133 changes: 31 additions & 102 deletions testscript/testdata/ui-home.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
# convert crlf to lf on windows
[windows] dos2unix home-in.txt home.txt about-in.txt about.txt

# start soft serve
exec soft serve &
# wait for server to start
waitforserver

# test repositories tab
ui home-in.txt
ui '" q"'
cmp stdout home.txt

# test about tab
ui about-in.txt
ui '"\t q"'
cmp stdout about.txt

# add a new repo
Expand All @@ -28,39 +33,31 @@ git -C config commit -m 'Initial commit'
git -C config push origin HEAD

# test repositories tab
ui home-in.txt
cmpenv stdout home2.txt
ui '" q"'
cp stdout home2.txt
grep 'Config' home2.txt
grep 'Test Soft Serve' home2.txt
grep 'git clone ssh://localhost:.*/.soft-serve' home2.txt

# test about tab
ui about-in.txt
cmp stdout about2.txt
ui '"\t q"'
cp stdout about2.txt
grep '• About' about2.txt
grep 'Hello World' about2.txt
grep 'Test Soft Serve' about2.txt

# stop the server
[windows] stopserver
[windows] ! stderr .

-- home-in.txt --
q
-- home.txt --
[?25l[?1049h[?25l[?1002h
 Test Soft Serve 

• Repositories About

No items found.














Test Soft Serve

• Repositories About


No items found.



Expand All @@ -71,31 +68,6 @@ cmp stdout about2.txt








↑↓ navigate • tab section • enter select • / filter • esc clear filter …
[?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












Expand All @@ -117,60 +89,17 @@ cmp stdout about2.txt



↑↓ navigatetab sectionenter select/ filter • c copy command …
↑↓ navigatetab sectionenter selectc copy command • q quit …
[?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
Test Soft Serve

 # Hello World\nTest Soft Serve
Repositories • About

No readme found.

Create a `.soft-serve` repository and add a `README.md` file to display re



Expand Down Expand Up @@ -199,7 +128,7 @@ cmp stdout about2.txt



☰ 100%
☰ 100%

↑↓ navigatetab sectionq quit? toggle help
↑↓ navigatetab sectionq quit? toggle help
[?25h[?1002l[?1003l[?1049l[?25h

0 comments on commit 7e2f1d6

Please sign in to comment.