Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/testscript: allow -e flag to pass env value #122

Merged
merged 1 commit into from
Jan 19, 2021
Merged
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
2 changes: 1 addition & 1 deletion cmd/testscript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ The testscript command runs github.com/rogpeppe/go-internal/testscript scripts
in a fresh temporary work directory tree.

Usage:
testscript [-v] [-e VAR]... [-u] files...
testscript [-v] [-e VAR[=value]]... [-u] files...

The testscript command is designed to make it easy to create self-contained
reproductions of command sequences.
Expand Down
2 changes: 1 addition & 1 deletion cmd/testscript/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The testscript command runs github.com/rogpeppe/go-internal/testscript scripts
in a fresh temporary work directory tree.

Usage:
testscript [-v] [-e VAR]... [-u] files...
testscript [-v] [-e VAR[=value]]... [-u] files...

The testscript command is designed to make it easy to create self-contained
reproductions of command sequences.
Expand Down
16 changes: 12 additions & 4 deletions cmd/testscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,19 @@ func run(runDir, filename string, update bool, verbose bool, envVars []string) e
if len(envVars) > 0 {
addSetup(func(env *testscript.Env) error {
for _, v := range envVars {
if v == "WORK" {
// cannot override WORK
continue
varName := v
if i := strings.Index(v, "="); i >= 0 {
varName = v[:i]
} else {
v = fmt.Sprintf("%s=%s", v, os.Getenv(v))
myitcv marked this conversation as resolved.
Show resolved Hide resolved
}
env.Vars = append(env.Vars, v+"="+os.Getenv(v))
switch varName {
case "":
return fmt.Errorf("invalid variable name %q", varName)
case "WORK":
myitcv marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("cannot override WORK variable")
}
env.Vars = append(env.Vars, v)
}
return nil
})
Expand Down
27 changes: 27 additions & 0 deletions cmd/testscript/testdata/env_values.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Test that passing env values, e.g. ENV1=val, works

unquote test.txt

env BLAH1=
env BLAH2=junk

# Normal operation
testscript -e BLAH1=rubbish -e BLAH2 test.txt

# It is an error to specify WORK. Note the error message
# appears on stdout because it is written to the log output
# of testscript, which has no concept of stderr.
! testscript -e BLAH1=rubbish -e BLAH2 -e WORK test.txt
stdout 'cannot override WORK variable'

-- test.txt --
>exec printf $BLAH1'\n'
>cmp stdout stdout1.txt
>
>exec printf $BLAH2'\n'
>cmp stdout stdout2.txt
>
>-- stdout1.txt --
>rubbish
>-- stdout2.txt --
>junk