forked from coder/wsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty_test.go
62 lines (51 loc) · 1.25 KB
/
tty_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package wsep
import (
"context"
"io/ioutil"
"strings"
"sync"
"testing"
"time"
"cdr.dev/slog/sloggers/slogtest/assert"
"nhooyr.io/websocket"
)
func TestTTY(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ws, server := mockConn(ctx, t)
defer ws.Close(websocket.StatusInternalError, "")
defer server.Close()
execer := RemoteExecer(ws)
testTTY(ctx, t, execer)
}
func testTTY(ctx context.Context, t *testing.T, e Execer) {
process, err := e.Start(ctx, Command{
Command: "sh",
TTY: true,
Stdin: true,
})
assert.Success(t, "start sh", err)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
stdout, err := ioutil.ReadAll(process.Stdout())
assert.Success(t, "read stdout", err)
t.Logf("bash tty stdout = %s", stdout)
prompt := string(stdout)
assert.True(t, `bash "$" or "#" prompt found`,
strings.HasSuffix(prompt, "$ ") || strings.HasSuffix(prompt, "# "))
}()
wg.Add(1)
go func() {
defer wg.Done()
stderr, err := ioutil.ReadAll(process.Stderr())
assert.Success(t, "read stderr", err)
t.Logf("bash tty stderr = %s", stderr)
assert.True(t, "stderr is empty", len(stderr) == 0)
}()
time.Sleep(3 * time.Second)
process.Close()
wg.Wait()
}