Skip to content

Commit a985ccd

Browse files
committed
feat: test ssh ui output
Still wip wip feat(tests): run against ssh ui
1 parent cd964a6 commit a985ccd

File tree

7 files changed

+131
-6
lines changed

7 files changed

+131
-6
lines changed

cmd/soft/browse/browse.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/charmbracelet/soft-serve/pkg/ui/common"
1414
"github.com/charmbracelet/soft-serve/pkg/ui/components/footer"
1515
"github.com/charmbracelet/soft-serve/pkg/ui/pages/repo"
16-
"github.com/muesli/termenv"
1716
"github.com/spf13/cobra"
1817
)
1918

@@ -40,7 +39,7 @@ var Command = &cobra.Command{
4039

4140
// Bubble Tea uses Termenv default output so we have to use the same
4241
// thing here.
43-
output := termenv.DefaultOutput()
42+
output := lipgloss.DefaultRenderer()
4443
ctx := cmd.Context()
4544
c := common.NewCommon(ctx, output, 0, 0)
4645
c.HideCloneCmd = true

cmd/soft/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"runtime/debug"
8+
"strconv"
89

910
"github.com/charmbracelet/log"
1011
"github.com/charmbracelet/soft-serve/cmd/soft/admin"
@@ -13,9 +14,11 @@ import (
1314
"github.com/charmbracelet/soft-serve/cmd/soft/serve"
1415
"github.com/charmbracelet/soft-serve/pkg/config"
1516
logr "github.com/charmbracelet/soft-serve/pkg/log"
17+
"github.com/charmbracelet/soft-serve/pkg/ui/common"
1618
"github.com/charmbracelet/soft-serve/pkg/version"
1719
mcobra "github.com/muesli/mango-cobra"
1820
"github.com/muesli/roff"
21+
"github.com/muesli/termenv"
1922
"github.com/spf13/cobra"
2023
"go.uber.org/automaxprocs/maxprocs"
2124
)
@@ -63,6 +66,10 @@ var (
6366
)
6467

6568
func init() {
69+
if noColor, _ := strconv.ParseBool(os.Getenv("SOFT_SERVE_NO_COLOR")); noColor {
70+
common.DefaultColorProfile = termenv.Ascii
71+
}
72+
6673
rootCmd.AddCommand(
6774
manCmd,
6875
serve.Command,

pkg/ssh/session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
tea "github.com/charmbracelet/bubbletea"
8+
"github.com/charmbracelet/lipgloss"
89
"github.com/charmbracelet/soft-serve/pkg/access"
910
"github.com/charmbracelet/soft-serve/pkg/backend"
1011
"github.com/charmbracelet/soft-serve/pkg/config"
@@ -54,7 +55,7 @@ func SessionHandler(s ssh.Session) *tea.Program {
5455
}
5556

5657
envs := &sessionEnv{s}
57-
output := termenv.NewOutput(s, termenv.WithColorCache(true), termenv.WithEnvironment(envs))
58+
output := lipgloss.NewRenderer(s, termenv.WithColorCache(true), termenv.WithEnvironment(envs))
5859
c := common.NewCommon(ctx, output, pty.Window.Width, pty.Window.Height)
5960
c.SetValue(common.ConfigKey, cfg)
6061
m := NewUI(c, initialRepo)

pkg/ui/common/common.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/charmbracelet/lipgloss"
78
"github.com/charmbracelet/log"
89
"github.com/charmbracelet/soft-serve/git"
910
"github.com/charmbracelet/soft-serve/pkg/backend"
@@ -38,15 +39,15 @@ type Common struct {
3839
}
3940

4041
// NewCommon returns a new Common struct.
41-
func NewCommon(ctx context.Context, out *termenv.Output, width, height int) Common {
42+
func NewCommon(ctx context.Context, out *lipgloss.Renderer, width, height int) Common {
4243
if ctx == nil {
4344
ctx = context.TODO()
4445
}
4546
return Common{
4647
ctx: ctx,
4748
Width: width,
4849
Height: height,
49-
Output: out,
50+
Output: out.Output(),
5051
Styles: styles.DefaultStyles(),
5152
KeyMap: keymap.DefaultKeyMap(),
5253
Zone: zone.New(),

testscript/script_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"os/exec"
1616
"path/filepath"
1717
"runtime"
18+
"strconv"
1819
"strings"
1920
"testing"
2021
"time"
@@ -92,6 +93,8 @@ func TestScript(t *testing.T) {
9293
"new-webhook": cmdNewWebhook,
9394
"waitforserver": cmdWaitforserver,
9495
"stopserver": cmdStopserver,
96+
"ui": cmdUI(admin1.Signer()),
97+
"uui": cmdUI(user1.Signer()),
9598
},
9699
Setup: func(e *testscript.Env) error {
97100
// Add binPath to PATH
@@ -120,6 +123,9 @@ func TestScript(t *testing.T) {
120123
// This is used to set up test specific configuration and http endpoints
121124
e.Setenv("SOFT_SERVE_TESTRUN", "1")
122125

126+
// This will disable the default lipgloss renderer colors
127+
e.Setenv("SOFT_SERVE_NO_COLOR", "1")
128+
123129
// Soft Serve debug environment variables
124130
for _, env := range []string{
125131
"SOFT_SERVE_DEBUG",
@@ -199,6 +205,64 @@ func cmdSoft(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []st
199205
}
200206
}
201207

208+
func cmdUI(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []string) {
209+
return func(ts *testscript.TestScript, neg bool, args []string) {
210+
if len(args) < 1 {
211+
ts.Fatalf("usage: ui <quoted string input>")
212+
return
213+
}
214+
215+
cli, err := ssh.Dial(
216+
"tcp",
217+
net.JoinHostPort("localhost", ts.Getenv("SSH_PORT")),
218+
&ssh.ClientConfig{
219+
User: "git",
220+
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
221+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
222+
},
223+
)
224+
check(ts, err, neg)
225+
defer cli.Close()
226+
227+
sess, err := cli.NewSession()
228+
check(ts, err, neg)
229+
defer sess.Close()
230+
231+
// XXX: this is a hack to make the UI tests work
232+
// cmp command always complains about an extra newline
233+
// in the output
234+
defer ts.Stdout().Write([]byte("\n"))
235+
236+
sess.Stdout = ts.Stdout()
237+
sess.Stderr = ts.Stderr()
238+
239+
stdin, err := sess.StdinPipe()
240+
check(ts, err, neg)
241+
242+
in, err := strconv.Unquote(args[0])
243+
check(ts, err, neg)
244+
reader := strings.NewReader(in)
245+
go func() {
246+
defer stdin.Close()
247+
for {
248+
r, _, err := reader.ReadRune()
249+
if err == io.EOF {
250+
break
251+
}
252+
check(ts, err, neg)
253+
stdin.Write([]byte(string(r))) // nolint: errcheck
254+
255+
// Wait for the UI to process the input
256+
time.Sleep(100 * time.Millisecond)
257+
}
258+
}()
259+
260+
err = sess.RequestPty("dumb", 40, 80, ssh.TerminalModes{})
261+
check(ts, err, neg)
262+
check(ts, sess.Run(""), neg)
263+
}
264+
}
265+
202266
// P.S. Windows sucks!
203267
func cmdDos2Unix(ts *testscript.TestScript, neg bool, args []string) {
204268
if neg {

testscript/testdata/repo-blob.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ stderr 'revision does not exist'
5454
-- blob1.txt --
5555
# Hello\n\nwelcome
5656
-- blob2.txt --
57-
1 │ package main\nconst foo = 2\n
57+
1 │ package main\nconst foo = 2\n
5858
-- blob3.txt --
5959
1 │ //#include <stdio.h>

testscript/testdata/ui-home.txtar

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# vi: set ft=conf
2+
3+
# start soft serve
4+
exec soft serve &
5+
# wait for server to start
6+
waitforserver
7+
8+
# test repositories tab
9+
ui '" q"'
10+
cp stdout home.txt
11+
grep 'Test Soft Serve' home.txt
12+
grep '• Repositories' home.txt
13+
grep 'No items found' home.txt
14+
15+
# test about tab
16+
ui '"\t q"'
17+
cp stdout about.txt
18+
grep 'Create a `.soft-serve` repository and add a `README.md` file' about.txt
19+
20+
# add a new repo
21+
soft repo create .soft-serve -n 'Config' -d '"Test Soft Serve"'
22+
soft repo description .soft-serve
23+
stdout 'Test Soft Serve'
24+
soft repo project-name .soft-serve
25+
stdout 'Config'
26+
27+
# clone repo
28+
git clone ssh://localhost:$SSH_PORT/.soft-serve config
29+
30+
# create readme file
31+
mkfile ./config/README.md '# Hello World\nTest Soft Serve'
32+
git -C config add -A
33+
git -C config commit -m 'Initial commit'
34+
git -C config push origin HEAD
35+
36+
# test repositories tab
37+
ui '" q"'
38+
cp stdout home2.txt
39+
grep 'Config' home2.txt
40+
grep 'Test Soft Serve' home2.txt
41+
grep 'git clone ssh://localhost:.*/.soft-serve' home2.txt
42+
43+
# test about tab
44+
ui '"\t q"'
45+
cp stdout about2.txt
46+
grep '• About' about2.txt
47+
grep 'Hello World' about2.txt
48+
grep 'Test Soft Serve' about2.txt
49+
50+
# stop the server
51+
[windows] stopserver
52+
[windows] ! stderr .
53+

0 commit comments

Comments
 (0)