Skip to content

Commit f5618a3

Browse files
rogpeppemyitcv
authored andcommitted
testscript: allow quoting of regexp metacharacters in env vars (#31)
1 parent 9b2bd7c commit f5618a3

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

testscript/doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ quote indicates a literal single quote, as in:
8787
A line beginning with # is a comment and conventionally explains what is
8888
being done or tested at the start of a new phase in the script.
8989
90+
A special form of environment variable syntax can be used to quote
91+
regexp metacharacters inside environment variables. The "@R" suffix
92+
is special, and indicates that the variable should be quoted.
93+
94+
${VAR@R}
95+
9096
The command prefix ! indicates that the command on the rest of the line
9197
(typically go or a matching predicate) must fail, not succeed. Only certain
9298
commands support this prefix. They are indicated below by [!] in the synopsis.

testscript/scripts/regexpquote.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
env XXX='hello)'
2+
grep ^${XXX@R}$ file.txt
3+
4+
-- file.txt --
5+
hello)

testscript/testscript.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,12 @@ func (ts *TestScript) Exec(command string, args ...string) error {
477477

478478
// expand applies environment variable expansion to the string s.
479479
func (ts *TestScript) expand(s string) string {
480-
return os.Expand(s, func(key string) string { return ts.envMap[key] })
480+
return os.Expand(s, func(key string) string {
481+
if key1 := strings.TrimSuffix(key, "@R"); len(key1) != len(key) {
482+
return regexp.QuoteMeta(ts.envMap[key1])
483+
}
484+
return ts.envMap[key]
485+
})
481486
}
482487

483488
// fatalf aborts the test with the given failure message.

0 commit comments

Comments
 (0)