-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
227 lines (214 loc) · 3.74 KB
/
main_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2018, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information
package main
import (
"fmt"
"io"
"testing"
"mvdan.cc/sh/v3/interp"
)
// Each test has an even number of strings, which form input-output pairs for
// the interactive shell. The input string is fed to the interactive shell, and
// bytes are read from its output until the expected output string is matched or
// an error is encountered.
//
// In other words, each first string is what the user types, and each following
// string is what the shell will print back. Note that the first "$ " output is
// implicit.
var interactiveTests = []struct {
pairs []string
wantErr string
}{
{},
{
pairs: []string{
"\n",
"$ ",
"\n",
"$ ",
},
},
{
pairs: []string{
"echo foo\n",
"foo\n",
},
},
{
pairs: []string{
"echo foo\n",
"foo\n$ ",
"echo bar\n",
"bar\n",
},
},
{
pairs: []string{
"if true\n",
"> ",
"then echo bar; fi\n",
"bar\n",
},
},
{
pairs: []string{
"echo 'foo\n",
"> ",
"bar'\n",
"foo\nbar\n",
},
},
{
pairs: []string{
"echo foo; echo bar\n",
"foo\nbar\n",
},
},
{
pairs: []string{
"echo foo; echo 'bar\n",
"> ",
"baz'\n",
"foo\nbar\nbaz\n",
},
},
{
pairs: []string{
"(\n",
"> ",
"echo foo)\n",
"foo\n",
},
},
{
pairs: []string{
"[[\n",
"> ",
"true ]]\n",
"$ ",
},
},
{
pairs: []string{
"echo foo ||\n",
"> ",
"echo bar\n",
"foo\n",
},
},
{
pairs: []string{
"echo foo |\n",
"> ",
"read var; echo $var\n",
"foo\n",
},
},
{
pairs: []string{
"echo foo",
"",
" bar\n",
"foo bar\n",
},
},
{
pairs: []string{
"echo\\\n",
"> ",
" foo\n",
"foo\n",
},
},
{
pairs: []string{
"echo foo\\\n",
"> ",
"bar\n",
"foobar\n",
},
},
{
pairs: []string{
"echo 你好\n",
"你好\n$ ",
},
},
{
pairs: []string{
"echo foo; exit 0; echo bar\n",
"foo\n",
"echo baz\n",
"",
},
},
{
pairs: []string{
"echo foo; exit 1; echo bar\n",
"foo\n",
"echo baz\n",
"",
},
wantErr: "exit status 1",
},
{
pairs: []string{
"(\n",
"> ",
},
wantErr: "1:1: reached EOF without matching ( with )",
},
}
func TestInteractive(t *testing.T) {
t.Parallel()
for i, tc := range interactiveTests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
inReader, inWriter := io.Pipe()
outReader, outWriter := io.Pipe()
runner, _ := interp.New(interp.StdIO(inReader, outWriter, outWriter))
errc := make(chan error)
go func() {
errc <- runInteractive(runner, inReader, outWriter, outWriter)
}()
if err := readString(outReader, "$ "); err != nil {
t.Fatal(err)
}
line := 1
for len(tc.pairs) > 0 {
if _, err := io.WriteString(inWriter, tc.pairs[0]); err != nil {
t.Fatal(err)
}
if err := readString(outReader, tc.pairs[1]); err != nil {
t.Fatal(err)
}
line++
tc.pairs = tc.pairs[2:]
}
// Close the input pipe, so that the parser can stop.
inWriter.Close()
// Once the input pipe is closed, close the output pipe
// so that any remaining prompt writes get discarded.
outReader.Close()
err := <-errc
if err != nil && tc.wantErr == "" {
t.Fatalf("unexpected error: %v", err)
} else if tc.wantErr != "" && fmt.Sprint(err) != tc.wantErr {
t.Fatalf("want error %q, got: %v", tc.wantErr, err)
}
})
}
}
// readString will keep reading from a reader until all bytes from the supplied
// string are read.
func readString(r io.Reader, want string) error {
p := make([]byte, len(want))
_, err := io.ReadFull(r, p)
if err != nil {
return err
}
got := string(p)
if got != want {
return fmt.Errorf("ReadString: read %q, wanted %q", got, want)
}
return nil
}