Skip to content

Commit 6bf774c

Browse files
authored
env: fix errors on valid interpolation expressions (#307)
The parser was too strict here and would reject valid constructs that used an unescaped `$` that was not part of a variable expression (and not ambiguous). Now, only errors for unmatched braced expressions (e.g. `${FOO`) are returned, but other valid cases are ignored and the `$` will be treated literally, e.g. `a $ string` -> `a $ string`, which is the same as in POSIX. Signed-off-by: Milas Bowman <[email protected]>
1 parent ccdcc95 commit 6bf774c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var substitutionNamed = "[_a-z][_a-z0-9]*"
3131
var substitutionBraced = "[_a-z][_a-z0-9]*(?::?[-+?](.*}|[^}]*))?"
3232

3333
var patternString = fmt.Sprintf(
34-
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?P<braced>%s)}|(?P<invalid>))",
34+
"%s(?i:(?P<escaped>%s)|(?P<named>%s)|{(?:(?P<braced>%s)}|(?P<invalid>)))",
3535
delimiter, delimiter, substitutionNamed, substitutionBraced,
3636
)
3737

template/template_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,24 @@ func TestSubstituteNoMatch(t *testing.T) {
4747
assert.Equal(t, "foo", result)
4848
}
4949

50+
func TestUnescaped(t *testing.T) {
51+
templates := []string{
52+
"a $ string",
53+
"^REGEX$",
54+
"$}",
55+
"$",
56+
}
57+
58+
for _, expected := range templates {
59+
actual, err := Substitute(expected, defaultMapping)
60+
assert.NilError(t, err)
61+
assert.Equal(t, expected, actual)
62+
}
63+
}
64+
5065
func TestInvalid(t *testing.T) {
5166
invalidTemplates := []string{
5267
"${",
53-
"$}",
5468
"${}",
5569
"${ }",
5670
"${ foo}",

0 commit comments

Comments
 (0)