Skip to content
Open
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
14 changes: 8 additions & 6 deletions cli/compose/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su
func substituteWith(template string, mapping Mapping, pattern regexper, subsFuncs ...SubstituteFunc) (string, error) {
var err error
result := pattern.ReplaceAllStringFunc(template, func(substring string) string {
if err != nil {
// A previous substitution failed; don't continue, so that the
// error is not overwritten by later (successful) substitutions.
return ""
}
matches := pattern.FindStringSubmatch(substring)
groups := matchGroups(matches, defaultPattern)
if escaped := groups["escaped"]; escaped != "" {
Expand All @@ -89,12 +94,9 @@ func substituteWith(template string, mapping Mapping, pattern regexper, subsFunc
}

for _, f := range subsFuncs {
var (
value string
applied bool
)
value, applied, err = f(substitution, mapping)
if err != nil {
value, applied, subErr := f(substitution, mapping)
if subErr != nil {
err = subErr
return ""
}
if !applied {
Expand Down
18 changes: 18 additions & 0 deletions cli/compose/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,24 @@ func TestMandatoryVariableErrors(t *testing.T) {
}
}

func TestMandatoryVariableErrorsWithOtherVariables(t *testing.T) {
testCases := []string{
"${UNSET_VAR:?Mandatory Variable Unset} ${FOO}",
"${UNSET_VAR:?Mandatory Variable Unset} $FOO",
"${UNSET_VAR:?Mandatory Variable Unset} ${missing:-def}",
"${UNSET_VAR?Mandatory Variable Unset}/${FOO?err}",
"${FOO} ${UNSET_VAR:?Mandatory Variable Unset} ${FOO}",
}

for _, template := range testCases {
t.Run(template, func(t *testing.T) {
_, err := Substitute(template, defaultMapping)
assert.Check(t, is.ErrorContains(err, "required variable UNSET_VAR is missing a value: Mandatory Variable Unset"))
assert.Check(t, is.ErrorType(err, &InvalidTemplateError{}))
})
}
}

func TestDefaultsForMandatoryVariables(t *testing.T) {
testCases := []struct {
template string
Expand Down