Skip to content

Commit 13f9d7d

Browse files
authored
cmd/testscript: allow -e flag to pass env value (#122)
The -e flag for testscript currently allows the caller to specify that environment variables are inherited from the current environment. However this falls when the current environment does not have a value set and you need to set a specific value. For example: GOBUILD=$(go env GOBUILD) testscript -e GOBUILD script.txt Instead, allow -e to also specify a value: testscript -e GOBUILD=$(go env GOBUILD) script.txt which feels more fluent/natural.
1 parent 8b0b133 commit 13f9d7d

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

cmd/testscript/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ The testscript command runs github.com/rogpeppe/go-internal/testscript scripts
33
in a fresh temporary work directory tree.
44
55
Usage:
6-
testscript [-v] [-e VAR]... [-u] files...
6+
testscript [-v] [-e VAR[=value]]... [-u] files...
77
88
The testscript command is designed to make it easy to create self-contained
99
reproductions of command sequences.

cmd/testscript/help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The testscript command runs github.com/rogpeppe/go-internal/testscript scripts
1414
in a fresh temporary work directory tree.
1515
1616
Usage:
17-
testscript [-v] [-e VAR]... [-u] files...
17+
testscript [-v] [-e VAR[=value]]... [-u] files...
1818
1919
The testscript command is designed to make it easy to create self-contained
2020
reproductions of command sequences.

cmd/testscript/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,19 @@ func run(runDir, filename string, update bool, verbose bool, envVars []string) e
273273
if len(envVars) > 0 {
274274
addSetup(func(env *testscript.Env) error {
275275
for _, v := range envVars {
276-
if v == "WORK" {
277-
// cannot override WORK
278-
continue
276+
varName := v
277+
if i := strings.Index(v, "="); i >= 0 {
278+
varName = v[:i]
279+
} else {
280+
v = fmt.Sprintf("%s=%s", v, os.Getenv(v))
279281
}
280-
env.Vars = append(env.Vars, v+"="+os.Getenv(v))
282+
switch varName {
283+
case "":
284+
return fmt.Errorf("invalid variable name %q", varName)
285+
case "WORK":
286+
return fmt.Errorf("cannot override WORK variable")
287+
}
288+
env.Vars = append(env.Vars, v)
281289
}
282290
return nil
283291
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Test that passing env values, e.g. ENV1=val, works
2+
3+
unquote test.txt
4+
5+
env BLAH1=
6+
env BLAH2=junk
7+
8+
# Normal operation
9+
testscript -e BLAH1=rubbish -e BLAH2 test.txt
10+
11+
# It is an error to specify WORK. Note the error message
12+
# appears on stdout because it is written to the log output
13+
# of testscript, which has no concept of stderr.
14+
! testscript -e BLAH1=rubbish -e BLAH2 -e WORK test.txt
15+
stdout 'cannot override WORK variable'
16+
17+
-- test.txt --
18+
>exec printf $BLAH1'\n'
19+
>cmp stdout stdout1.txt
20+
>
21+
>exec printf $BLAH2'\n'
22+
>cmp stdout stdout2.txt
23+
>
24+
>-- stdout1.txt --
25+
>rubbish
26+
>-- stdout2.txt --
27+
>junk

0 commit comments

Comments
 (0)