-
Notifications
You must be signed in to change notification settings - Fork 73
/
cmd_test.go
144 lines (131 loc) · 3.28 KB
/
cmd_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package wish
import (
"bytes"
"runtime"
"strings"
"testing"
"time"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/wish/testsession"
)
func TestCommandNoPty(t *testing.T) {
tmp := t.TempDir()
sess := testsession.New(t, &ssh.Server{
Handler: func(s ssh.Session) {
runEcho(s, "hello")
runEnv(s, []string{"HELLO=world"})
runPwd(s, tmp)
},
}, nil)
var stdout bytes.Buffer
var stderr bytes.Buffer
sess.Stdout = &stdout
sess.Stderr = &stderr
if err := sess.Run(""); err != nil {
t.Errorf("expected no error, got %v: %s", err, stderr.String())
}
out := stdout.String()
expectContains(t, out, "hello")
expectContains(t, out, "HELLO=world")
expectContains(t, out, tmp)
}
func TestCommandPty(t *testing.T) {
tmp := t.TempDir()
srv := &ssh.Server{
Handler: func(s ssh.Session) {
runEcho(s, "hello")
runEnv(s, []string{"HELLO=world"})
runPwd(s, tmp)
// for some reason sometimes on macos github action runners,
// it cuts parts of the output.
time.Sleep(100 * time.Millisecond)
},
}
if err := ssh.AllocatePty()(srv); err != nil {
t.Fatalf("expected no error, got %v", err)
}
sess := testsession.New(t, srv, nil)
if err := sess.RequestPty("xterm", 500, 200, nil); err != nil {
t.Fatalf("expected no error, got %v", err)
}
var stdout bytes.Buffer
var stderr bytes.Buffer
sess.Stdout = &stdout
sess.Stderr = &stderr
if err := sess.Run(""); err != nil {
t.Errorf("expected no error, got %v: %s", err, stderr.String())
}
out := stdout.String()
expectContains(t, out, "hello")
expectContains(t, out, "HELLO=world")
expectContains(t, out, tmp)
}
func TestCommandPtyError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip()
}
srv := &ssh.Server{
Handler: func(s ssh.Session) {
if err := Command(s, "nopenopenope").Run(); err != nil {
Fatal(s, err)
}
},
}
if err := ssh.AllocatePty()(srv); err != nil {
t.Fatalf("expected no error, got %v", err)
}
sess := testsession.New(t, srv, nil)
if err := sess.RequestPty("xterm", 500, 200, nil); err != nil {
t.Fatalf("expected no error, got %v", err)
}
var stderr bytes.Buffer
sess.Stderr = &stderr
if err := sess.Run(""); err == nil {
t.Errorf("expected an error, got nil")
}
expect := `exec: "nopenopenope"`
if s := stderr.String(); !strings.Contains(s, expect) {
t.Errorf("expected output to contain %q, got %q", expect, s)
}
}
func runEcho(s ssh.Session, str string) {
cmd := Command(s, "echo", str)
if runtime.GOOS == "windows" {
cmd = Command(s, "cmd", "/C", "echo", str)
}
// these should do nothing...
cmd.SetStderr(nil)
cmd.SetStdin(nil)
cmd.SetStdout(nil)
if err := cmd.Run(); err != nil {
Fatal(s, err)
}
}
func runEnv(s ssh.Session, env []string) {
cmd := Command(s, "env")
if runtime.GOOS == "windows" {
cmd = Command(s, "cmd", "/C", "set")
}
cmd.SetEnv(env)
if err := cmd.Run(); err != nil {
Fatal(s, err)
}
if len(cmd.Environ()) == 0 {
Fatal(s, "cmd.Environ() should not be empty")
}
}
func runPwd(s ssh.Session, dir string) {
cmd := Command(s, "pwd")
if runtime.GOOS == "windows" {
cmd = Command(s, "cmd", "/C", "cd")
}
cmd.SetDir(dir)
if err := cmd.Run(); err != nil {
Fatal(s, err)
}
}
func expectContains(tb testing.TB, s, substr string) {
if !strings.Contains(s, substr) {
tb.Errorf("expected output %q to contain %q", s, substr)
}
}