Skip to content

Commit

Permalink
testscript: allow quoting of regexp metacharacters in env vars (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogpeppe authored and myitcv committed Nov 30, 2018
1 parent 9b2bd7c commit f5618a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ quote indicates a literal single quote, as in:
A line beginning with # is a comment and conventionally explains what is
being done or tested at the start of a new phase in the script.
A special form of environment variable syntax can be used to quote
regexp metacharacters inside environment variables. The "@R" suffix
is special, and indicates that the variable should be quoted.
${VAR@R}
The command prefix ! indicates that the command on the rest of the line
(typically go or a matching predicate) must fail, not succeed. Only certain
commands support this prefix. They are indicated below by [!] in the synopsis.
Expand Down
5 changes: 5 additions & 0 deletions testscript/scripts/regexpquote.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
env XXX='hello)'
grep ^${XXX@R}$ file.txt

-- file.txt --
hello)
7 changes: 6 additions & 1 deletion testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,12 @@ func (ts *TestScript) Exec(command string, args ...string) error {

// expand applies environment variable expansion to the string s.
func (ts *TestScript) expand(s string) string {
return os.Expand(s, func(key string) string { return ts.envMap[key] })
return os.Expand(s, func(key string) string {
if key1 := strings.TrimSuffix(key, "@R"); len(key1) != len(key) {
return regexp.QuoteMeta(ts.envMap[key1])
}
return ts.envMap[key]
})
}

// fatalf aborts the test with the given failure message.
Expand Down

0 comments on commit f5618a3

Please sign in to comment.