Skip to content

Commit

Permalink
testscript: support stdout/stderr in more builtins
Browse files Browse the repository at this point in the history
Support these special files in the stdin, unquote, and match (grep)
commands. This is particularly useful to "pipe" output like "stdin
stdout", or to use commands that produce txtar output.

The grep one isn't super useful, as one can already do "stdout xxx"
instead of "grep xxx stdout", but this way it's more consistent and we
save some repeated lines. Because the feature is itself a bit redundant,
don't document it, to prevent confusing the user.
  • Loading branch information
mvdan committed Apr 13, 2020
1 parent bc89b17 commit 501fbe2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 4 additions & 9 deletions testscript/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func (ts *TestScript) cmdCp(neg bool, args []string) {
data []byte
mode os.FileMode
)
// Don't use TestScript.ReadFile, as we want the mode too.
switch arg {
case "stdout":
src = arg
Expand Down Expand Up @@ -310,9 +311,7 @@ func (ts *TestScript) cmdUnquote(neg bool, args []string) {
}
for _, arg := range args {
file := ts.MkAbs(arg)
data, err := ioutil.ReadFile(file)
ts.Check(err)
data, err = txtar.Unquote(data)
data, err := txtar.Unquote([]byte(ts.ReadFile(file)))
ts.Check(err)
err = ioutil.WriteFile(file, data, 0666)
ts.Check(err)
Expand Down Expand Up @@ -363,9 +362,7 @@ func (ts *TestScript) cmdStdin(neg bool, args []string) {
if len(args) != 1 {
ts.Fatalf("usage: stdin filename")
}
data, err := ioutil.ReadFile(ts.MkAbs(args[0]))
ts.Check(err)
ts.stdin = string(data)
ts.stdin = ts.ReadFile(args[0])
}

// stdout checks that the last go command standard output matches a regexp.
Expand Down Expand Up @@ -494,9 +491,7 @@ func scriptMatch(ts *TestScript, neg bool, args []string, text, name string) {
isGrep := name == "grep"
if isGrep {
name = args[1] // for error messages
data, err := ioutil.ReadFile(ts.MkAbs(args[1]))
ts.Check(err)
text = string(data)
text = ts.ReadFile(args[1])
}

if neg {
Expand Down
4 changes: 4 additions & 0 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ The predefined commands are:
each line. This enables a file to contain substrings that look like
txtar file markers.
See also https://godoc.org/github.com/rogpeppe/go-internal/txtar#Unquote
File can be "stdout" or "stderr" to use the standard output or standard error
from the most recent exec or wait command.
- rm file...
Remove the listed files or directories.
Expand All @@ -180,6 +182,8 @@ The predefined commands are:
- stdin file
Set the standard input for the next exec command to the contents of the given file.
File can be "stdout" or "stderr" to use the standard output or standard error
from the most recent exec or wait command.
- [!] stderr [-count=N] pattern
Apply the grep command (see above) to the standard error
Expand Down

0 comments on commit 501fbe2

Please sign in to comment.