| 1 |
// Copyright (c) 2018, Daniel Martí <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/go-quicktest/qt" |
| 16 |
"github.com/rogpeppe/go-internal/testscript" |
| 17 |
|
| 18 |
"mvdan.cc/sh/v3/interp" |
| 19 |
) |
| 20 |
|
| 21 |
func TestMain(m *testing.M) { |
| 22 |
if runtime.GOOS == "js" { |
| 23 |
// Every test here needs subprocesses or OS pipes, |
| 24 |
// neither of which js/wasm has. |
| 25 |
return |
| 26 |
} |
| 27 |
testscript.Main(m, map[string]func(){ |
| 28 |
"gosh": main, |
| 29 |
}) |
| 30 |
} |
| 31 |
|
| 32 |
var update = flag.Bool("u", false, "update testscript output files") |
| 33 |
|
| 34 |
func TestScript(t *testing.T) { |
| 35 |
t.Parallel() |
| 36 |
testscript.Run(t, testscript.Params{ |
| 37 |
Dir: filepath.Join("testdata", "script"), |
| 38 |
UpdateScripts: *update, |
| 39 |
RequireExplicitExec: true, |
| 40 |
}) |
| 41 |
} |
| 42 |
|
| 43 |
// Each test has an even number of strings, which form input-output pairs for |
| 44 |
// the interactive shell. The input string is fed to the interactive shell, and |
| 45 |
// bytes are read from its output until the expected output string is matched or |
| 46 |
// an error is encountered. |
| 47 |
// |
| 48 |
// In other words, each first string is what the user types, and each following |
| 49 |
// string is what the shell will print back. Note that the first "$ " output is |
| 50 |
// implicit. |
| 51 |
|
| 52 |
var interactiveTests = []struct { |
| 53 |
pairs []string |
| 54 |
wantErr string |
| 55 |
}{ |
| 56 |
{}, |
| 57 |
{ |
| 58 |
pairs: []string{ |
| 59 |
"\n", |
| 60 |
"$ ", |
| 61 |
"\n", |
| 62 |
"$ ", |
| 63 |
}, |
| 64 |
}, |
| 65 |
{ |
| 66 |
pairs: []string{ |
| 67 |
"echo foo\n", |
| 68 |
"foo\n", |
| 69 |
}, |
| 70 |
}, |
| 71 |
{ |
| 72 |
pairs: []string{ |
| 73 |
"echo foo\n", |
| 74 |
"foo\n$ ", |
| 75 |
"echo bar\n", |
| 76 |
"bar\n", |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
pairs: []string{ |
| 81 |
"if true\n", |
| 82 |
"> ", |
| 83 |
"then echo bar; fi\n", |
| 84 |
"bar\n", |
| 85 |
}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
pairs: []string{ |
| 89 |
"echo 'foo\n", |
| 90 |
"> ", |
| 91 |
"bar'\n", |
| 92 |
"foo\nbar\n", |
| 93 |
}, |
| 94 |
}, |
| 95 |
{ |
| 96 |
pairs: []string{ |
| 97 |
"echo foo; echo bar\n", |
| 98 |
"foo\nbar\n", |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
pairs: []string{ |
| 103 |
"echo foo; echo 'bar\n", |
| 104 |
"> ", |
| 105 |
"baz'\n", |
| 106 |
"foo\nbar\nbaz\n", |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
pairs: []string{ |
| 111 |
"(\n", |
| 112 |
"> ", |
| 113 |
"echo foo)\n", |
| 114 |
"foo\n", |
| 115 |
}, |
| 116 |
}, |
| 117 |
{ |
| 118 |
pairs: []string{ |
| 119 |
"[[\n", |
| 120 |
"> ", |
| 121 |
"true ]]\n", |
| 122 |
"$ ", |
| 123 |
}, |
| 124 |
}, |
| 125 |
{ |
| 126 |
pairs: []string{ |
| 127 |
"echo foo ||\n", |
| 128 |
"> ", |
| 129 |
"echo bar\n", |
| 130 |
"foo\n", |
| 131 |
}, |
| 132 |
}, |
| 133 |
{ |
| 134 |
pairs: []string{ |
| 135 |
"echo foo |\n", |
| 136 |
"> ", |
| 137 |
"read var; echo $var\n", |
| 138 |
"foo\n", |
| 139 |
}, |
| 140 |
}, |
| 141 |
{ |
| 142 |
pairs: []string{ |
| 143 |
"echo foo", |
| 144 |
"", |
| 145 |
" bar\n", |
| 146 |
"foo bar\n", |
| 147 |
}, |
| 148 |
}, |
| 149 |
{ |
| 150 |
pairs: []string{ |
| 151 |
"echo\\\n", |
| 152 |
"> ", |
| 153 |
" foo\n", |
| 154 |
"foo\n", |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
pairs: []string{ |
| 159 |
"echo foo\\\n", |
| 160 |
"> ", |
| 161 |
"bar\n", |
| 162 |
"foobar\n", |
| 163 |
}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
pairs: []string{ |
| 167 |
"echo 你好\n", |
| 168 |
"你好\n$ ", |
| 169 |
}, |
| 170 |
}, |
| 171 |
{ |
| 172 |
pairs: []string{ |
| 173 |
"echo *; :\n", |
| 174 |
"main.go main_test.go testdata\n$ ", |
| 175 |
"echo *\n", |
| 176 |
"main.go main_test.go testdata\n$ ", |
| 177 |
"shopt -s globstar; echo **\n", |
| 178 |
// Globs expand with the OS path separator. |
| 179 |
filepath.FromSlash("main.go main_test.go testdata testdata/script testdata/script/flags.txtar") + "\n$ ", |
| 180 |
}, |
| 181 |
}, |
| 182 |
{ |
| 183 |
pairs: []string{ |
| 184 |
"echo foo; exit 0; echo bar\n", |
| 185 |
"foo\n", |
| 186 |
"echo baz\n", |
| 187 |
"", |
| 188 |
}, |
| 189 |
}, |
| 190 |
{ |
| 191 |
pairs: []string{ |
| 192 |
"echo foo; exit 1; echo bar\n", |
| 193 |
"foo\n", |
| 194 |
"echo baz\n", |
| 195 |
"", |
| 196 |
}, |
| 197 |
wantErr: "exit status 1", |
| 198 |
}, |
| 199 |
{ |
| 200 |
pairs: []string{ |
| 201 |
"(x\n", |
| 202 |
"> ", |
| 203 |
}, |
| 204 |
wantErr: "1:1: reached EOF without matching `(` with `)`", |
| 205 |
}, |
| 206 |
{ |
| 207 |
pairs: []string{ |
| 208 |
"gosh_alias arg || true\n", |
| 209 |
"\"gosh_alias\": executable file not found in $PATH\n$ ", |
| 210 |
"alias gosh_alias=echo\n", |
| 211 |
"$ ", |
| 212 |
"gosh_alias arg || true\n", |
| 213 |
"arg\n$ ", |
| 214 |
"unalias gosh_alias\n", |
| 215 |
"$ ", |
| 216 |
"gosh_alias arg || true\n", |
| 217 |
"\"gosh_alias\": executable file not found in $PATH\n$ ", |
| 218 |
}, |
| 219 |
}, |
| 220 |
} |
| 221 |
|
| 222 |
func TestInteractive(t *testing.T) { |
| 223 |
t.Parallel() |
| 224 |
for _, tc := range interactiveTests { |
| 225 |
t.Run("", func(t *testing.T) { |
| 226 |
inReader, inWriter, err := os.Pipe() |
| 227 |
qt.Assert(t, qt.IsNil(err)) |
| 228 |
outReader, outWriter, err := os.Pipe() |
| 229 |
qt.Assert(t, qt.IsNil(err)) |
| 230 |
runner, _ := interp.New(interp.Interactive(true), interp.StdIO(inReader, outWriter, outWriter)) |
| 231 |
errc := make(chan error, 1) |
| 232 |
go func() { |
| 233 |
errc <- runInteractive(runner, inReader, outWriter, outWriter) |
| 234 |
// Discard the rest of the input. |
| 235 |
io.Copy(io.Discard, inReader) |
| 236 |
inReader.Close() |
| 237 |
outWriter.Close() |
| 238 |
}() |
| 239 |
|
| 240 |
if err := readString(outReader, "$ "); err != nil { |
| 241 |
t.Fatal(err) |
| 242 |
} |
| 243 |
|
| 244 |
line := 1 |
| 245 |
for len(tc.pairs) > 0 { |
| 246 |
t.Logf("write %q", tc.pairs[0]) |
| 247 |
if _, err := io.WriteString(inWriter, tc.pairs[0]); err != nil { |
| 248 |
t.Fatal(err) |
| 249 |
} |
| 250 |
t.Logf("read %q", tc.pairs[1]) |
| 251 |
if err := readString(outReader, tc.pairs[1]); err != nil { |
| 252 |
t.Fatal(err) |
| 253 |
} |
| 254 |
|
| 255 |
line++ |
| 256 |
tc.pairs = tc.pairs[2:] |
| 257 |
} |
| 258 |
|
| 259 |
// Close the input pipe, so that the parser can stop. |
| 260 |
inWriter.Close() |
| 261 |
|
| 262 |
// Once the input pipe is closed, close the output pipe |
| 263 |
// so that any remaining prompt writes get discarded. |
| 264 |
outReader.Close() |
| 265 |
|
| 266 |
err = <-errc |
| 267 |
if err != nil && tc.wantErr == "" { |
| 268 |
t.Fatalf("unexpected error: %v", err) |
| 269 |
} else if tc.wantErr != "" && fmt.Sprint(err) != tc.wantErr { |
| 270 |
t.Fatalf("want error %q, got: %v", tc.wantErr, err) |
| 271 |
} |
| 272 |
}) |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func TestInteractiveExit(t *testing.T) { |
| 277 |
inReader, inWriter, err := os.Pipe() |
| 278 |
qt.Assert(t, qt.IsNil(err)) |
| 279 |
defer inReader.Close() |
| 280 |
go func() { |
| 281 |
io.WriteString(inWriter, "exit\n") |
| 282 |
inWriter.Close() |
| 283 |
}() |
| 284 |
w := io.Discard |
| 285 |
runner, _ := interp.New(interp.Interactive(true), interp.StdIO(inReader, w, w)) |
| 286 |
if err := runInteractive(runner, inReader, w, w); err != nil { |
| 287 |
t.Fatal("expected a nil error") |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// readString will keep reading from a reader until all bytes from the supplied |
| 292 |
// string are read. |
| 293 |
func readString(r io.Reader, want string) error { |
| 294 |
p := make([]byte, len(want)) |
| 295 |
_, err := io.ReadFull(r, p) |
| 296 |
if err != nil { |
| 297 |
return err |
| 298 |
} |
| 299 |
got := string(p) |
| 300 |
if got != want { |
| 301 |
return fmt.Errorf("ReadString: read %q, wanted %q", got, want) |
| 302 |
} |
| 303 |
return nil |
| 304 |
} |