Skip to content

Commit 4385788

Browse files
authored
Merge pull request #68 from rogpeppe/022-testscript-values
testscript: provide way for Setup to pass values to commands
2 parents bb30e2a + 726e557 commit 4385788

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

testscript/testdata/exec_path_change.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ exec go$exe build
1515
exec go$exe version
1616
stdout 'This is not go'
1717

18+
-- go/go.mod --
19+
module example.com/go
20+
1821
-- go/main.go --
1922
package main
2023

testscript/testdata/values.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test-values

testscript/testscript.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,28 @@ var testWork = flag.Bool("testwork", false, "")
3939

4040
// Env holds the environment to use at the start of a test script invocation.
4141
type Env struct {
42+
// WorkDir holds the path to the root directory of the
43+
// extracted files.
4244
WorkDir string
43-
Vars []string
44-
Cd string
45+
// Vars holds the initial set environment variables that will be passed to the
46+
// testscript commands.
47+
Vars []string
48+
// Cd holds the initial current working directory.
49+
Cd string
50+
// Values holds a map of arbitrary values for use by custom
51+
// testscript commands. This enables Setup to pass arbitrary
52+
// values (not just strings) through to custom commands.
53+
Values map[interface{}]interface{}
4554

4655
ts *TestScript
4756
}
4857

58+
// Value returns a value from Env.Values, or nil if no
59+
// value was set by Setup.
60+
func (ts *TestScript) Value(key interface{}) interface{} {
61+
return ts.values[key]
62+
}
63+
4964
// Defer arranges for f to be called at the end
5065
// of the test. If Defer is called multiple times, the
5166
// defers are executed in reverse order (similar
@@ -171,23 +186,24 @@ type TestScript struct {
171186
params Params
172187
t T
173188
testTempDir string
174-
workdir string // temporary work dir ($WORK)
175-
log bytes.Buffer // test execution log (printed at end of test)
176-
mark int // offset of next log truncation
177-
cd string // current directory during test execution; initially $WORK/gopath/src
178-
name string // short name of test ("foo")
179-
file string // full file name ("testdata/script/foo.txt")
180-
lineno int // line number currently executing
181-
line string // line currently executing
182-
env []string // environment list (for os/exec)
183-
envMap map[string]string // environment mapping (matches env; on Windows keys are lowercase)
184-
stdin string // standard input to next 'go' command; set by 'stdin' command.
185-
stdout string // standard output from last 'go' command; for 'stdout' command
186-
stderr string // standard error from last 'go' command; for 'stderr' command
187-
stopped bool // test wants to stop early
188-
start time.Time // time phase started
189-
background []backgroundCmd // backgrounded 'exec' and 'go' commands
190-
deferred func() // deferred cleanup actions.
189+
workdir string // temporary work dir ($WORK)
190+
log bytes.Buffer // test execution log (printed at end of test)
191+
mark int // offset of next log truncation
192+
cd string // current directory during test execution; initially $WORK/gopath/src
193+
name string // short name of test ("foo")
194+
file string // full file name ("testdata/script/foo.txt")
195+
lineno int // line number currently executing
196+
line string // line currently executing
197+
env []string // environment list (for os/exec)
198+
envMap map[string]string // environment mapping (matches env; on Windows keys are lowercase)
199+
values map[interface{}]interface{} // values for custom commands
200+
stdin string // standard input to next 'go' command; set by 'stdin' command.
201+
stdout string // standard output from last 'go' command; for 'stdout' command
202+
stderr string // standard error from last 'go' command; for 'stderr' command
203+
stopped bool // test wants to stop early
204+
start time.Time // time phase started
205+
background []backgroundCmd // backgrounded 'exec' and 'go' commands
206+
deferred func() // deferred cleanup actions.
191207

192208
ctxt context.Context // per TestScript context
193209
}
@@ -213,6 +229,7 @@ func (ts *TestScript) setup() string {
213229
":=" + string(os.PathListSeparator),
214230
},
215231
WorkDir: ts.workdir,
232+
Values: make(map[interface{}]interface{}),
216233
Cd: ts.workdir,
217234
ts: ts,
218235
}
@@ -242,6 +259,7 @@ func (ts *TestScript) setup() string {
242259
}
243260
ts.cd = env.Cd
244261
ts.env = env.Vars
262+
ts.values = env.Values
245263

246264
ts.envMap = make(map[string]string)
247265
for _, kv := range ts.env {

testscript/testscript_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func TestScripts(t *testing.T) {
9393
ts.Fatalf("setup did not see expected files; got %q want %q", setupFilenames, args)
9494
}
9595
},
96+
"test-values": func(ts *TestScript, neg bool, args []string) {
97+
if ts.Value("somekey") != 1234 {
98+
ts.Fatalf("test-values did not see expected value")
99+
}
100+
},
96101
},
97102
Setup: func(env *Env) error {
98103
infos, err := ioutil.ReadDir(env.WorkDir)
@@ -103,6 +108,7 @@ func TestScripts(t *testing.T) {
103108
for _, info := range infos {
104109
setupFilenames = append(setupFilenames, info.Name())
105110
}
111+
env.Values["somekey"] = 1234
106112
return nil
107113
},
108114
})

0 commit comments

Comments
 (0)