Skip to content

Commit

Permalink
env: fix errors on valid interpolation expressions (#307)
Browse files Browse the repository at this point in the history
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 <milas.bowman@docker.com>
  • Loading branch information
milas committed Sep 23, 2022
1 parent ccdcc95 commit 6bf774c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion template/template.go
Expand Up @@ -31,7 +31,7 @@ var substitutionNamed = "[_a-z][_a-z0-9]*"
var substitutionBraced = "[_a-z][_a-z0-9]*(?::?[-+?](.*}|[^}]*))?"

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

Expand Down
16 changes: 15 additions & 1 deletion template/template_test.go
Expand Up @@ -47,10 +47,24 @@ func TestSubstituteNoMatch(t *testing.T) {
assert.Equal(t, "foo", result)
}

func TestUnescaped(t *testing.T) {
templates := []string{
"a $ string",
"^REGEX$",
"$}",
"$",
}

for _, expected := range templates {
actual, err := Substitute(expected, defaultMapping)
assert.NilError(t, err)
assert.Equal(t, expected, actual)
}
}

func TestInvalid(t *testing.T) {
invalidTemplates := []string{
"${",
"$}",
"${}",
"${ }",
"${ foo}",
Expand Down

0 comments on commit 6bf774c

Please sign in to comment.