Skip to content

Commit 7b233a2

Browse files
authored
testscript: allow "stdout" and "stderr" as inputs to script_test "cp" command (#59)
Cherry picked from https://go-review.googlesource.com/c/163519
1 parent 184c5df commit 7b233a2

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

testscript/cmd.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,33 @@ func (ts *TestScript) cmdCp(neg bool, args []string) {
161161
}
162162

163163
for _, arg := range args[:len(args)-1] {
164-
src := ts.MkAbs(arg)
165-
info, err := os.Stat(src)
166-
ts.Check(err)
167-
data, err := ioutil.ReadFile(src)
168-
ts.Check(err)
164+
var (
165+
src string
166+
data []byte
167+
mode os.FileMode
168+
)
169+
switch arg {
170+
case "stdout":
171+
src = arg
172+
data = []byte(ts.stdout)
173+
mode = 0666
174+
case "stderr":
175+
src = arg
176+
data = []byte(ts.stderr)
177+
mode = 0666
178+
default:
179+
src = ts.MkAbs(arg)
180+
info, err := os.Stat(src)
181+
ts.Check(err)
182+
mode = info.Mode() & 0777
183+
data, err = ioutil.ReadFile(src)
184+
ts.Check(err)
185+
}
169186
targ := dst
170187
if dstDir {
171188
targ = filepath.Join(dst, filepath.Base(src))
172189
}
173-
ts.Check(ioutil.WriteFile(targ, data, info.Mode()&0777))
190+
ts.Check(ioutil.WriteFile(targ, data, mode))
174191
}
175192
}
176193

testscript/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ The predefined commands are:
133133
134134
- cp src... dst
135135
Copy the listed files to the target file or existing directory.
136+
src can include "stdout" or "stderr" to use the standard output or standard error
137+
from the most recent exec or go command.
136138
137139
- env [key=value...]
138140
With no arguments, print the environment (useful for debugging).

testscript/testdata/cpstdout.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[!exec:cat] stop
2+
3+
# hello world
4+
exec cat hello.text
5+
cp stdout got
6+
cmp got hello.text
7+
8+
-- hello.text --
9+
hello world
10+

0 commit comments

Comments
 (0)