Skip to content

Commit 4bb227c

Browse files
authored
testscript: add ReadFile method (#82)
This allows custom commands to operate on the output of previous exec commands similar to the built in cp and cmp commands.
1 parent 223dee3 commit 4bb227c

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

testscript/cmd.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,11 @@ func (ts *TestScript) cmdCmpenv(neg bool, args []string) {
119119

120120
func (ts *TestScript) doCmdCmp(args []string, env bool) {
121121
name1, name2 := args[0], args[1]
122-
var text1, text2 string
123-
if name1 == "stdout" {
124-
text1 = ts.stdout
125-
} else if name1 == "stderr" {
126-
text1 = ts.stderr
127-
} else {
128-
data, err := ioutil.ReadFile(ts.MkAbs(name1))
129-
ts.Check(err)
130-
text1 = string(data)
131-
}
122+
text1 := ts.ReadFile(name1)
132123

133124
data, err := ioutil.ReadFile(ts.MkAbs(name2))
134125
ts.Check(err)
135-
text2 = string(data)
136-
126+
text2 := string(data)
137127
if env {
138128
text2 = ts.expand(text2)
139129
}

testscript/testdata/readfile.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
echo stdout stdout
2+
testreadfile stdout
3+
4+
echo stderr stderr
5+
testreadfile stderr
6+
7+
testreadfile x/somefile
8+
9+
-- x/somefile --
10+
x/somefile

testscript/testscript.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,27 @@ func (ts *TestScript) MkAbs(file string) string {
631631
return filepath.Join(ts.cd, file)
632632
}
633633

634+
// ReadFile returns the contents of the file with the
635+
// given name, intepreted relative to the test script's
636+
// current directory. It interprets "stdout" and "stderr" to
637+
// mean the standard output or standard error from
638+
// the most recent exec or wait command respectively.
639+
//
640+
// If the file cannot be read, the script fails.
641+
func (ts *TestScript) ReadFile(file string) string {
642+
switch file {
643+
case "stdout":
644+
return ts.stdout
645+
case "stderr":
646+
return ts.stderr
647+
default:
648+
file = ts.MkAbs(file)
649+
data, err := ioutil.ReadFile(file)
650+
ts.Check(err)
651+
return string(data)
652+
}
653+
}
654+
634655
// Setenv sets the value of the environment variable named by the key.
635656
func (ts *TestScript) Setenv(key, value string) {
636657
ts.env = append(ts.env, key+"="+value)

testscript/testscript_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"reflect"
1515
"regexp"
1616
"strconv"
17+
"strings"
1718
"testing"
1819
"time"
1920
)
@@ -23,6 +24,17 @@ func printArgs() int {
2324
return 0
2425
}
2526

27+
func echo() int {
28+
s := strings.Join(os.Args[2:], " ")
29+
switch os.Args[1] {
30+
case "stdout":
31+
fmt.Println(s)
32+
case "stderr":
33+
fmt.Fprintln(os.Stderr, s)
34+
}
35+
return 0
36+
}
37+
2638
func exitWithStatus() int {
2739
n, _ := strconv.Atoi(os.Args[1])
2840
return n
@@ -46,6 +58,7 @@ func signalCatcher() int {
4658
func TestMain(m *testing.M) {
4759
os.Exit(RunMain(m, map[string]func() int{
4860
"printargs": printArgs,
61+
"echo": echo,
4962
"status": exitWithStatus,
5063
"signalcatcher": signalCatcher,
5164
}))
@@ -100,6 +113,16 @@ func TestScripts(t *testing.T) {
100113
ts.Fatalf("test-values did not see expected value")
101114
}
102115
},
116+
"testreadfile": func(ts *TestScript, neg bool, args []string) {
117+
if len(args) != 1 {
118+
ts.Fatalf("testreadfile <filename>")
119+
}
120+
got := ts.ReadFile(args[0])
121+
want := args[0] + "\n"
122+
if got != want {
123+
ts.Fatalf("reading %q; got %q want %q", args[0], got, want)
124+
}
125+
},
103126
},
104127
Setup: func(env *Env) error {
105128
infos, err := ioutil.ReadDir(env.WorkDir)

0 commit comments

Comments
 (0)