Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions testscript/testdata/cmd_stdout_stderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Print to stdout via a TestMain cmd
fprintargs stdout hello world from fprintargs
stdout 'hello world from fprintargs'

# Verify that we can write to stdout via a Params.Cmds cmd
echoStdout hello world from echoStdout
stdout 'hello world from echoStdout'

# Print to stderr via a TestMain cmd
fprintargs stderr the answer is 42 from fprintargs
stderr 'the answer is 42 from fprintargs'

# Verify that we can write to stderr via a Params.Cmds cmd
echoStderr the answer is 42 from echoStderr
stderr 'the answer is 42 from echoStderr'
12 changes: 12 additions & 0 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,18 @@ func (ts *TestScript) Check(err error) {
}
}

// SetStdout updates the last stdout. This is typically used by commands
// declared via Params.Cmds.
func (ts *TestScript) SetStdout(s string) {
ts.stdout = s
}

// SetStderr updates the last stderr. This is typically used by commands
// declared via Params.Cmds.
func (ts *TestScript) SetStderr(s string) {
ts.stderr = s
}

// Logf appends the given formatted message to the test log transcript.
func (ts *TestScript) Logf(format string, args ...interface{}) {
format = strings.TrimSuffix(format, "\n")
Expand Down
6 changes: 6 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ func TestScripts(t *testing.T) {
ts.Fatalf("testscript unexpectedly failed with errors: %q", &t.log)
}
},
"echoStdout": func(ts *TestScript, neg bool, args []string) {
ts.SetStdout(strings.Join(args, " "))
},
"echoStderr": func(ts *TestScript, neg bool, args []string) {
ts.SetStderr(strings.Join(args, " "))
},
},
Setup: func(env *Env) error {
infos, err := ioutil.ReadDir(env.WorkDir)
Expand Down