| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package testscript |
| 6 |
|
| 7 |
import ( |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"path/filepath" |
| 14 |
"regexp" |
| 15 |
"runtime" |
| 16 |
"slices" |
| 17 |
"strconv" |
| 18 |
"strings" |
| 19 |
|
| 20 |
"github.com/rogpeppe/go-internal/diff" |
| 21 |
"github.com/rogpeppe/go-internal/testscript/internal/pty" |
| 22 |
"github.com/rogpeppe/go-internal/txtar" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
var scriptCmds = map[string]func(*TestScript, bool, []string){ |
| 30 |
"cd": (*TestScript).cmdCd, |
| 31 |
"chmod": (*TestScript).cmdChmod, |
| 32 |
"cmp": (*TestScript).cmdCmp, |
| 33 |
"cmpenv": (*TestScript).cmdCmpenv, |
| 34 |
"cp": (*TestScript).cmdCp, |
| 35 |
"env": (*TestScript).cmdEnv, |
| 36 |
"exec": (*TestScript).cmdExec, |
| 37 |
"exists": (*TestScript).cmdExists, |
| 38 |
"grep": (*TestScript).cmdGrep, |
| 39 |
"kill": (*TestScript).cmdKill, |
| 40 |
"mkdir": (*TestScript).cmdMkdir, |
| 41 |
"mv": (*TestScript).cmdMv, |
| 42 |
"rm": (*TestScript).cmdRm, |
| 43 |
"skip": (*TestScript).cmdSkip, |
| 44 |
"stderr": (*TestScript).cmdStderr, |
| 45 |
"stdin": (*TestScript).cmdStdin, |
| 46 |
"stdout": (*TestScript).cmdStdout, |
| 47 |
"ttyin": (*TestScript).cmdTtyin, |
| 48 |
"ttyout": (*TestScript).cmdTtyout, |
| 49 |
"stop": (*TestScript).cmdStop, |
| 50 |
"symlink": (*TestScript).cmdSymlink, |
| 51 |
"unix2dos": (*TestScript).cmdUNIX2DOS, |
| 52 |
"unquote": (*TestScript).cmdUnquote, |
| 53 |
"wait": (*TestScript).cmdWait, |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func (ts *TestScript) cmdCd(neg bool, args []string) { |
| 58 |
if neg { |
| 59 |
ts.Fatalf("unsupported: ! cd") |
| 60 |
} |
| 61 |
if len(args) != 1 { |
| 62 |
ts.Fatalf("usage: cd dir") |
| 63 |
} |
| 64 |
|
| 65 |
dir := args[0] |
| 66 |
err := ts.Chdir(dir) |
| 67 |
if os.IsNotExist(err) { |
| 68 |
ts.Fatalf("directory %s does not exist", dir) |
| 69 |
} |
| 70 |
ts.Check(err) |
| 71 |
ts.Logf("%s\n", ts.cd) |
| 72 |
} |
| 73 |
|
| 74 |
func (ts *TestScript) cmdChmod(neg bool, args []string) { |
| 75 |
if neg { |
| 76 |
ts.Fatalf("unsupported: ! chmod") |
| 77 |
} |
| 78 |
if len(args) != 2 { |
| 79 |
ts.Fatalf("usage: chmod perm paths...") |
| 80 |
} |
| 81 |
perm, err := strconv.ParseUint(args[0], 8, 32) |
| 82 |
if err != nil || perm&uint64(os.ModePerm) != perm { |
| 83 |
ts.Fatalf("invalid mode: %s", args[0]) |
| 84 |
} |
| 85 |
for _, arg := range args[1:] { |
| 86 |
path := arg |
| 87 |
if !filepath.IsAbs(path) { |
| 88 |
path = filepath.Join(ts.cd, arg) |
| 89 |
} |
| 90 |
err := os.Chmod(path, os.FileMode(perm)) |
| 91 |
ts.Check(err) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (ts *TestScript) cmdCmp(neg bool, args []string) { |
| 97 |
if len(args) != 2 { |
| 98 |
ts.Fatalf("usage: cmp file1 file2") |
| 99 |
} |
| 100 |
|
| 101 |
ts.doCmdCmp(neg, args, false) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (ts *TestScript) cmdCmpenv(neg bool, args []string) { |
| 106 |
if len(args) != 2 { |
| 107 |
ts.Fatalf("usage: cmpenv file1 file2") |
| 108 |
} |
| 109 |
ts.doCmdCmp(neg, args, true) |
| 110 |
} |
| 111 |
|
| 112 |
func (ts *TestScript) doCmdCmp(neg bool, args []string, env bool) { |
| 113 |
name1, name2 := args[0], args[1] |
| 114 |
if name1 == name2 { |
| 115 |
|
| 116 |
|
| 117 |
ts.Fatalf("cmp: cannot compare a file against itself") |
| 118 |
} |
| 119 |
text1 := ts.ReadFile(name1) |
| 120 |
|
| 121 |
absName2 := ts.MkAbs(name2) |
| 122 |
data, err := os.ReadFile(absName2) |
| 123 |
ts.Check(err) |
| 124 |
text2 := string(data) |
| 125 |
if env { |
| 126 |
text2 = ts.expand(text2) |
| 127 |
} |
| 128 |
eq := text1 == text2 |
| 129 |
if neg { |
| 130 |
if eq { |
| 131 |
ts.Fatalf("%s and %s do not differ", name1, name2) |
| 132 |
} |
| 133 |
return |
| 134 |
} |
| 135 |
if eq { |
| 136 |
return |
| 137 |
} |
| 138 |
if ts.params.UpdateScripts && !env { |
| 139 |
if scriptFile, ok := ts.scriptFiles[absName2]; ok { |
| 140 |
ts.scriptUpdates[scriptFile] = text1 |
| 141 |
return |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
unifiedDiff := diff.Diff(name1, []byte(text1), name2, []byte(text2)) |
| 148 |
|
| 149 |
ts.Logf("%s", unifiedDiff) |
| 150 |
ts.Fatalf("%s and %s differ", name1, name2) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (ts *TestScript) cmdCp(neg bool, args []string) { |
| 155 |
if neg { |
| 156 |
ts.Fatalf("unsupported: ! cp") |
| 157 |
} |
| 158 |
if len(args) < 2 { |
| 159 |
ts.Fatalf("usage: cp src... dst") |
| 160 |
} |
| 161 |
|
| 162 |
dst := ts.MkAbs(args[len(args)-1]) |
| 163 |
info, err := os.Stat(dst) |
| 164 |
dstDir := err == nil && info.IsDir() |
| 165 |
if len(args) > 2 && !dstDir { |
| 166 |
ts.Fatalf("cp: destination %s is not a directory", dst) |
| 167 |
} |
| 168 |
|
| 169 |
for _, arg := range args[:len(args)-1] { |
| 170 |
var ( |
| 171 |
src string |
| 172 |
data []byte |
| 173 |
mode os.FileMode |
| 174 |
) |
| 175 |
switch arg { |
| 176 |
case "stdout": |
| 177 |
src = arg |
| 178 |
data = []byte(ts.stdout) |
| 179 |
mode = 0o666 |
| 180 |
case "stderr": |
| 181 |
src = arg |
| 182 |
data = []byte(ts.stderr) |
| 183 |
mode = 0o666 |
| 184 |
case "ttyout": |
| 185 |
src = arg |
| 186 |
data = []byte(ts.ttyout) |
| 187 |
mode = 0o666 |
| 188 |
default: |
| 189 |
src = ts.MkAbs(arg) |
| 190 |
info, err := os.Stat(src) |
| 191 |
ts.Check(err) |
| 192 |
mode = info.Mode() & 0o777 |
| 193 |
data, err = os.ReadFile(src) |
| 194 |
ts.Check(err) |
| 195 |
} |
| 196 |
targ := dst |
| 197 |
if dstDir { |
| 198 |
targ = filepath.Join(dst, filepath.Base(src)) |
| 199 |
} |
| 200 |
ts.Check(os.WriteFile(targ, data, mode)) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (ts *TestScript) cmdEnv(neg bool, args []string) { |
| 206 |
if neg { |
| 207 |
ts.Fatalf("unsupported: ! env") |
| 208 |
} |
| 209 |
if len(args) == 0 { |
| 210 |
printed := make(map[string]bool) |
| 211 |
for _, kv := range ts.env { |
| 212 |
k := envvarname(kv[:strings.Index(kv, "=")]) |
| 213 |
if !printed[k] { |
| 214 |
printed[k] = true |
| 215 |
ts.Logf("%s=%s\n", k, ts.envMap[k]) |
| 216 |
} |
| 217 |
} |
| 218 |
return |
| 219 |
} |
| 220 |
for _, env := range args { |
| 221 |
before, after, ok := strings.Cut(env, "=") |
| 222 |
if !ok { |
| 223 |
|
| 224 |
ts.Logf("%s=%s\n", env, ts.Getenv(env)) |
| 225 |
continue |
| 226 |
} |
| 227 |
ts.Setenv(before, after) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
var backgroundSpecifier = regexp.MustCompile(`^&([a-zA-Z_0-9]+&)?$`) |
| 232 |
|
| 233 |
|
| 234 |
func (ts *TestScript) cmdExec(neg bool, args []string) { |
| 235 |
if len(args) < 1 || (len(args) == 1 && args[0] == "&") { |
| 236 |
ts.Fatalf("usage: exec program [args...] [&]") |
| 237 |
} |
| 238 |
|
| 239 |
var err error |
| 240 |
if len(args) > 0 && backgroundSpecifier.MatchString(args[len(args)-1]) { |
| 241 |
bgName := strings.TrimSuffix(strings.TrimPrefix(args[len(args)-1], "&"), "&") |
| 242 |
if ts.findBackground(bgName) != nil { |
| 243 |
ts.Fatalf("duplicate background process name %q", bgName) |
| 244 |
} |
| 245 |
var cmd *exec.Cmd |
| 246 |
cmd, err = ts.execBackground(args[0], args[1:len(args)-1]...) |
| 247 |
if err == nil { |
| 248 |
wait := make(chan struct{}) |
| 249 |
go func() { |
| 250 |
waitOrStop(ts.ctxt, cmd, -1) |
| 251 |
close(wait) |
| 252 |
}() |
| 253 |
ts.background = append(ts.background, backgroundCmd{bgName, cmd, wait, neg}) |
| 254 |
} |
| 255 |
ts.stdout, ts.stderr = "", "" |
| 256 |
} else { |
| 257 |
ts.stdout, ts.stderr, err = ts.exec(args[0], args[1:]...) |
| 258 |
if ts.stdout != "" { |
| 259 |
fmt.Fprintf(&ts.log, "[stdout]\n%s", ts.stdout) |
| 260 |
} |
| 261 |
if ts.stderr != "" { |
| 262 |
fmt.Fprintf(&ts.log, "[stderr]\n%s", ts.stderr) |
| 263 |
} |
| 264 |
if err == nil && neg { |
| 265 |
ts.Fatalf("unexpected command success") |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
if err != nil { |
| 270 |
fmt.Fprintf(&ts.log, "[%v]\n", err) |
| 271 |
if ts.ctxt.Err() != nil { |
| 272 |
ts.Fatalf("test timed out while running command") |
| 273 |
} else if !neg { |
| 274 |
ts.Fatalf("unexpected command failure") |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
func (ts *TestScript) cmdExists(neg bool, args []string) { |
| 281 |
var readonly bool |
| 282 |
if len(args) > 0 && args[0] == "-readonly" { |
| 283 |
readonly = true |
| 284 |
args = args[1:] |
| 285 |
} |
| 286 |
if len(args) == 0 { |
| 287 |
ts.Fatalf("usage: exists [-readonly] file...") |
| 288 |
} |
| 289 |
|
| 290 |
for _, file := range args { |
| 291 |
file = ts.MkAbs(file) |
| 292 |
info, err := os.Stat(file) |
| 293 |
if err == nil && neg { |
| 294 |
what := "file" |
| 295 |
if info.IsDir() { |
| 296 |
what = "directory" |
| 297 |
} |
| 298 |
ts.Fatalf("%s %s unexpectedly exists", what, file) |
| 299 |
} |
| 300 |
if err != nil && !neg { |
| 301 |
ts.Fatalf("%s does not exist", file) |
| 302 |
} |
| 303 |
if err == nil && !neg && readonly && info.Mode()&0o222 != 0 { |
| 304 |
ts.Fatalf("%s exists but is writable", file) |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
func (ts *TestScript) cmdMkdir(neg bool, args []string) { |
| 311 |
if neg { |
| 312 |
ts.Fatalf("unsupported: ! mkdir") |
| 313 |
} |
| 314 |
if len(args) < 1 { |
| 315 |
ts.Fatalf("usage: mkdir dir...") |
| 316 |
} |
| 317 |
for _, arg := range args { |
| 318 |
ts.Check(os.MkdirAll(ts.MkAbs(arg), 0o777)) |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
func (ts *TestScript) cmdMv(neg bool, args []string) { |
| 323 |
if neg { |
| 324 |
ts.Fatalf("unsupported: ! mv") |
| 325 |
} |
| 326 |
if len(args) != 2 { |
| 327 |
ts.Fatalf("usage: mv old new") |
| 328 |
} |
| 329 |
ts.Check(os.Rename(ts.MkAbs(args[0]), ts.MkAbs(args[1]))) |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
func (ts *TestScript) cmdUnquote(neg bool, args []string) { |
| 334 |
if neg { |
| 335 |
ts.Fatalf("unsupported: ! unquote") |
| 336 |
} |
| 337 |
for _, arg := range args { |
| 338 |
file := ts.MkAbs(arg) |
| 339 |
data, err := os.ReadFile(file) |
| 340 |
ts.Check(err) |
| 341 |
data, err = txtar.Unquote(data) |
| 342 |
ts.Check(err) |
| 343 |
err = os.WriteFile(file, data, 0o666) |
| 344 |
ts.Check(err) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
func (ts *TestScript) cmdRm(neg bool, args []string) { |
| 350 |
if neg { |
| 351 |
ts.Fatalf("unsupported: ! rm") |
| 352 |
} |
| 353 |
if len(args) < 1 { |
| 354 |
ts.Fatalf("usage: rm file...") |
| 355 |
} |
| 356 |
for _, arg := range args { |
| 357 |
file := ts.MkAbs(arg) |
| 358 |
removeAll(file) |
| 359 |
ts.Check(os.RemoveAll(file)) |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
func (ts *TestScript) cmdSkip(neg bool, args []string) { |
| 365 |
if len(args) > 1 { |
| 366 |
ts.Fatalf("usage: skip [msg]") |
| 367 |
} |
| 368 |
if neg { |
| 369 |
ts.Fatalf("unsupported: ! skip") |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
for _, bg := range ts.background { |
| 375 |
interruptProcess(bg.cmd.Process) |
| 376 |
} |
| 377 |
ts.cmdWait(false, nil) |
| 378 |
|
| 379 |
if len(args) == 1 { |
| 380 |
ts.t.Skip(args[0]) |
| 381 |
} |
| 382 |
ts.t.Skip() |
| 383 |
} |
| 384 |
|
| 385 |
func (ts *TestScript) cmdStdin(neg bool, args []string) { |
| 386 |
if neg { |
| 387 |
ts.Fatalf("unsupported: ! stdin") |
| 388 |
} |
| 389 |
if len(args) != 1 { |
| 390 |
ts.Fatalf("usage: stdin filename") |
| 391 |
} |
| 392 |
if ts.stdinPty { |
| 393 |
ts.Fatalf("conflicting use of 'stdin' and 'ttyin -stdin'") |
| 394 |
} |
| 395 |
ts.stdin = ts.ReadFile(args[0]) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
func (ts *TestScript) cmdStdout(neg bool, args []string) { |
| 400 |
scriptMatch(ts, neg, args, ts.stdout, "stdout") |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
func (ts *TestScript) cmdStderr(neg bool, args []string) { |
| 405 |
scriptMatch(ts, neg, args, ts.stderr, "stderr") |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
func (ts *TestScript) cmdGrep(neg bool, args []string) { |
| 411 |
scriptMatch(ts, neg, args, "", "grep") |
| 412 |
} |
| 413 |
|
| 414 |
func (ts *TestScript) cmdTtyin(neg bool, args []string) { |
| 415 |
if !pty.Supported { |
| 416 |
ts.Fatalf("unsupported: ttyin on %s", runtime.GOOS) |
| 417 |
} |
| 418 |
if neg { |
| 419 |
ts.Fatalf("unsupported: ! ttyin") |
| 420 |
} |
| 421 |
switch len(args) { |
| 422 |
case 1: |
| 423 |
ts.ttyin = ts.ReadFile(args[0]) |
| 424 |
case 2: |
| 425 |
if args[0] != "-stdin" { |
| 426 |
ts.Fatalf("usage: ttyin [-stdin] filename") |
| 427 |
} |
| 428 |
if ts.stdin != "" { |
| 429 |
ts.Fatalf("conflicting use of 'stdin' and 'ttyin -stdin'") |
| 430 |
} |
| 431 |
ts.stdinPty = true |
| 432 |
ts.ttyin = ts.ReadFile(args[1]) |
| 433 |
default: |
| 434 |
ts.Fatalf("usage: ttyin [-stdin] filename") |
| 435 |
} |
| 436 |
if ts.ttyin == "" { |
| 437 |
ts.Fatalf("tty input file is empty") |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
func (ts *TestScript) cmdTtyout(neg bool, args []string) { |
| 442 |
scriptMatch(ts, neg, args, ts.ttyout, "ttyout") |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
func (ts *TestScript) cmdStop(neg bool, args []string) { |
| 447 |
if neg { |
| 448 |
ts.Fatalf("unsupported: ! stop") |
| 449 |
} |
| 450 |
if len(args) > 1 { |
| 451 |
ts.Fatalf("usage: stop [msg]") |
| 452 |
} |
| 453 |
if len(args) == 1 { |
| 454 |
ts.Logf("stop: %s\n", args[0]) |
| 455 |
} else { |
| 456 |
ts.Logf("stop\n") |
| 457 |
} |
| 458 |
ts.stopped = true |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
func (ts *TestScript) cmdSymlink(neg bool, args []string) { |
| 463 |
if neg { |
| 464 |
ts.Fatalf("unsupported: ! symlink") |
| 465 |
} |
| 466 |
if len(args) != 3 || args[1] != "->" { |
| 467 |
ts.Fatalf("usage: symlink file -> target") |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
ts.Check(os.Symlink(args[2], ts.MkAbs(args[0]))) |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
func (ts *TestScript) cmdUNIX2DOS(neg bool, args []string) { |
| 476 |
if neg { |
| 477 |
ts.Fatalf("unsupported: ! unix2dos") |
| 478 |
} |
| 479 |
if len(args) < 1 { |
| 480 |
ts.Fatalf("usage: unix2dos paths...") |
| 481 |
} |
| 482 |
for _, arg := range args { |
| 483 |
filename := ts.MkAbs(arg) |
| 484 |
data, err := os.ReadFile(filename) |
| 485 |
ts.Check(err) |
| 486 |
dosData, err := unix2DOS(data) |
| 487 |
ts.Check(err) |
| 488 |
if err := os.WriteFile(filename, dosData, 0o666); err != nil { |
| 489 |
ts.Fatalf("%s: %v", filename, err) |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
func (ts *TestScript) cmdKill(neg bool, args []string) { |
| 496 |
signals := map[string]os.Signal{ |
| 497 |
"INT": os.Interrupt, |
| 498 |
"KILL": os.Kill, |
| 499 |
} |
| 500 |
var ( |
| 501 |
name string |
| 502 |
signal os.Signal |
| 503 |
) |
| 504 |
switch len(args) { |
| 505 |
case 0: |
| 506 |
case 1, 2: |
| 507 |
sig, ok := strings.CutPrefix(args[0], "-") |
| 508 |
if ok { |
| 509 |
signal, ok = signals[sig] |
| 510 |
if !ok { |
| 511 |
ts.Fatalf("unknown signal: %s", sig) |
| 512 |
} |
| 513 |
} else { |
| 514 |
name = args[0] |
| 515 |
break |
| 516 |
} |
| 517 |
if len(args) == 2 { |
| 518 |
name = args[1] |
| 519 |
} |
| 520 |
default: |
| 521 |
ts.Fatalf("usage: kill [-SIGNAL] [name]") |
| 522 |
} |
| 523 |
if neg { |
| 524 |
ts.Fatalf("unsupported: ! kill") |
| 525 |
} |
| 526 |
if signal == nil { |
| 527 |
signal = os.Kill |
| 528 |
} |
| 529 |
if name != "" { |
| 530 |
ts.killBackgroundOne(name, signal) |
| 531 |
} else { |
| 532 |
ts.killBackground(signal) |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
func (ts *TestScript) killBackgroundOne(bgName string, signal os.Signal) { |
| 537 |
bg := ts.findBackground(bgName) |
| 538 |
if bg == nil { |
| 539 |
ts.Fatalf("unknown background process %q", bgName) |
| 540 |
} |
| 541 |
err := bg.cmd.Process.Signal(signal) |
| 542 |
if err != nil { |
| 543 |
ts.Fatalf("unexpected error terminating background command %q: %v", bgName, err) |
| 544 |
} |
| 545 |
} |
| •546 |
|
| 547 |
func (ts *TestScript) killBackground(signal os.Signal) { |
| 548 |
for bgName, bg := range ts.background { |
| 549 |
err := bg.cmd.Process.Signal(signal) |
| 550 |
if err != nil { |
| 551 |
ts.Fatalf("unexpected error terminating background command %v: %v", bgName, err) |
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
func (ts *TestScript) cmdWait(neg bool, args []string) { |
| 558 |
if len(args) > 1 { |
| 559 |
ts.Fatalf("usage: wait [name]") |
| 560 |
} |
| 561 |
if neg { |
| 562 |
ts.Fatalf("unsupported: ! wait") |
| 563 |
} |
| 564 |
if len(args) > 0 { |
| 565 |
ts.waitBackgroundOne(args[0]) |
| 566 |
} else { |
| 567 |
ts.waitBackground(true) |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
func (ts *TestScript) waitBackgroundOne(bgName string) { |
| 572 |
bg := ts.findBackground(bgName) |
| 573 |
if bg == nil { |
| 574 |
ts.Fatalf("unknown background process %q", bgName) |
| 575 |
} |
| 576 |
<-bg.wait |
| 577 |
ts.stdout = bg.cmd.Stdout.(*strings.Builder).String() |
| 578 |
ts.stderr = bg.cmd.Stderr.(*strings.Builder).String() |
| 579 |
if ts.stdout != "" { |
| 580 |
fmt.Fprintf(&ts.log, "[stdout]\n%s", ts.stdout) |
| 581 |
} |
| 582 |
if ts.stderr != "" { |
| 583 |
fmt.Fprintf(&ts.log, "[stderr]\n%s", ts.stderr) |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
if bg.cmd.ProcessState.Success() { |
| 588 |
if bg.neg { |
| 589 |
ts.Fatalf("unexpected command success") |
| 590 |
} |
| 591 |
} else { |
| 592 |
if ts.ctxt.Err() != nil { |
| 593 |
ts.Fatalf("test timed out while running command") |
| 594 |
} else if !bg.neg { |
| 595 |
ts.Fatalf("unexpected command failure") |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
for i := range ts.background { |
| 600 |
if bg == &ts.background[i] { |
| 601 |
ts.background = slices.Delete(ts.background, i, i+1) |
| 602 |
break |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
func (ts *TestScript) findBackground(bgName string) *backgroundCmd { |
| 608 |
if bgName == "" { |
| 609 |
return nil |
| 610 |
} |
| 611 |
for i := range ts.background { |
| 612 |
bg := &ts.background[i] |
| 613 |
if bg.name == bgName { |
| 614 |
return bg |
| 615 |
} |
| 616 |
} |
| 617 |
return nil |
| 618 |
} |
| 619 |
|
| 620 |
func (ts *TestScript) waitBackground(checkStatus bool) { |
| 621 |
var stdouts, stderrs []string |
| 622 |
for _, bg := range ts.background { |
| 623 |
<-bg.wait |
| 624 |
|
| 625 |
args := append([]string{filepath.Base(bg.cmd.Args[0])}, bg.cmd.Args[1:]...) |
| 626 |
fmt.Fprintf(&ts.log, "[background] %s: %v\n", strings.Join(args, " "), bg.cmd.ProcessState) |
| 627 |
|
| 628 |
cmdStdout := bg.cmd.Stdout.(*strings.Builder).String() |
| 629 |
if cmdStdout != "" { |
| 630 |
fmt.Fprintf(&ts.log, "[stdout]\n%s", cmdStdout) |
| 631 |
stdouts = append(stdouts, cmdStdout) |
| 632 |
} |
| 633 |
|
| 634 |
cmdStderr := bg.cmd.Stderr.(*strings.Builder).String() |
| 635 |
if cmdStderr != "" { |
| 636 |
fmt.Fprintf(&ts.log, "[stderr]\n%s", cmdStderr) |
| 637 |
stderrs = append(stderrs, cmdStderr) |
| 638 |
} |
| 639 |
|
| 640 |
if !checkStatus { |
| 641 |
continue |
| 642 |
} |
| 643 |
if bg.cmd.ProcessState.Success() { |
| 644 |
if bg.neg { |
| 645 |
ts.Fatalf("unexpected command success") |
| 646 |
} |
| 647 |
} else { |
| 648 |
if ts.ctxt.Err() != nil { |
| 649 |
ts.Fatalf("test timed out while running command") |
| 650 |
} else if !bg.neg { |
| 651 |
ts.Fatalf("unexpected command failure") |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
ts.stdout = strings.Join(stdouts, "") |
| 657 |
ts.stderr = strings.Join(stderrs, "") |
| 658 |
ts.background = nil |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
func scriptMatch(ts *TestScript, neg bool, args []string, text, name string) { |
| 663 |
n := 0 |
| 664 |
if len(args) >= 1 && strings.HasPrefix(args[0], "-count=") { |
| 665 |
if neg { |
| 666 |
ts.Fatalf("cannot use -count= with negated match") |
| 667 |
} |
| 668 |
var err error |
| 669 |
n, err = strconv.Atoi(args[0][len("-count="):]) |
| 670 |
if err != nil { |
| 671 |
ts.Fatalf("bad -count=: %v", err) |
| 672 |
} |
| 673 |
if n < 1 { |
| 674 |
ts.Fatalf("bad -count=: must be at least 1") |
| 675 |
} |
| 676 |
args = args[1:] |
| 677 |
} |
| 678 |
|
| 679 |
extraUsage := "" |
| 680 |
want := 1 |
| 681 |
if name == "grep" { |
| 682 |
extraUsage = " file" |
| 683 |
want = 2 |
| 684 |
} |
| 685 |
if len(args) != want { |
| 686 |
ts.Fatalf("usage: %s [-count=N] 'pattern'%s", name, extraUsage) |
| 687 |
} |
| 688 |
|
| 689 |
pattern := args[0] |
| 690 |
re, err := regexp.Compile(`(?m)` + pattern) |
| 691 |
ts.Check(err) |
| 692 |
|
| 693 |
isGrep := name == "grep" |
| 694 |
if isGrep { |
| 695 |
name = args[1] |
| 696 |
data, err := os.ReadFile(ts.MkAbs(args[1])) |
| 697 |
ts.Check(err) |
| 698 |
text = string(data) |
| 699 |
} |
| 700 |
|
| 701 |
if neg { |
| 702 |
if re.MatchString(text) { |
| 703 |
if isGrep { |
| 704 |
ts.Logf("[%s]\n%s\n", name, text) |
| 705 |
} |
| 706 |
ts.Fatalf("unexpected match for %#q found in %s: %s", pattern, name, re.FindString(text)) |
| 707 |
} |
| 708 |
} else { |
| 709 |
if !re.MatchString(text) { |
| 710 |
if isGrep { |
| 711 |
ts.Logf("[%s]\n%s\n", name, text) |
| 712 |
} |
| 713 |
ts.Fatalf("no match for %#q found in %s", pattern, name) |
| 714 |
} |
| 715 |
if n > 0 { |
| 716 |
count := len(re.FindAllString(text, -1)) |
| 717 |
if count != n { |
| 718 |
if isGrep { |
| 719 |
ts.Logf("[%s]\n%s\n", name, text) |
| 720 |
} |
| 721 |
ts.Fatalf("have %d matches for %#q, want %d", count, pattern, n) |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
func unix2DOS(data []byte) ([]byte, error) { |
| 729 |
sb := &strings.Builder{} |
| 730 |
s := bufio.NewScanner(bytes.NewReader(data)) |
| 731 |
for s.Scan() { |
| 732 |
if _, err := sb.Write(s.Bytes()); err != nil { |
| 733 |
return nil, err |
| 734 |
} |
| 735 |
if _, err := sb.WriteString("\r\n"); err != nil { |
| 736 |
return nil, err |
| 737 |
} |
| 738 |
} |
| 739 |
if err := s.Err(); err != nil { |
| 740 |
return nil, err |
| 741 |
} |
| 742 |
return []byte(sb.String()), nil |
| 743 |
} |