Skip to content

Commit ef6c167

Browse files
authored
Merge pull request #393 from ndeloof/required_interpolate
required variable should not trigger a generic InvalidTemplateError
2 parents 84df208 + d6baef7 commit ef6c167

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

dotenv/godotenv_var_expansion_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ func TestErrorIfEmptyOrUnset(t *testing.T) {
9292
"Error empty or unset: UNSET_VAR",
9393
"RESULT=${UNSET_VAR:?Test error}",
9494
"RESULT=${UNSET_VAR:?Test error}",
95-
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"},
95+
&template.MissingRequiredError{Variable: "UNSET_VAR", Reason: "Test error"},
9696
},
9797
{
9898
"Error empty or unset: EMPTY_VAR",
9999
"RESULT=${EMPTY_VAR:?Test error}",
100100
"RESULT=${EMPTY_VAR:?Test error}",
101-
&template.InvalidTemplateError{Template: "required variable EMPTY_VAR is missing a value: Test error"},
101+
&template.MissingRequiredError{Variable: "EMPTY_VAR", Reason: "Test error"},
102102
},
103103
{
104104
"Error empty or unset: TEST_VAR",
@@ -128,7 +128,7 @@ func TestErrorIfUnset(t *testing.T) {
128128
"Error on unset: UNSET_VAR",
129129
"RESULT=${UNSET_VAR?Test error}",
130130
"RESULT=${UNSET_VAR?Test error}",
131-
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"},
131+
&template.MissingRequiredError{Variable: "UNSET_VAR", Reason: "Test error"},
132132
},
133133
{
134134
"Error on unset: EMPTY_VAR",

interpolation/interpolation_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
134134
}
135135

136136
getFullErrorMsg := func(msg string) string {
137-
return fmt.Sprintf("invalid interpolation format for myservice.environment.TESTVAR.\n"+
138-
"You may need to escape any $ with another $.\nrequired variable FOO is missing a value: %s", msg)
137+
return fmt.Sprintf("error while interpolating myservice.environment.TESTVAR: required variable FOO is missing a value: %s", msg)
139138
}
140139

141140
for _, testcase := range testcases {

loader/loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,6 @@ func TestDeviceWriteBps(t *testing.T) {
23512351

23522352
func TestInvalidProjectNameType(t *testing.T) {
23532353
p, err := loadYAML(`name: 123`)
2354-
assert.Error(t, err, "name must be a string")
2354+
assert.Error(t, err, "validating filename0.yml: name must be a string")
23552355
assert.Assert(t, is.Nil(p))
23562356
}

template/template.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ func (e InvalidTemplateError) Error() string {
4747
return fmt.Sprintf("Invalid template: %#v", e.Template)
4848
}
4949

50+
// MissingRequiredError is returned when a variable template is missing
51+
type MissingRequiredError struct {
52+
Variable string
53+
Reason string
54+
}
55+
56+
func (e MissingRequiredError) Error() string {
57+
if e.Reason != "" {
58+
return fmt.Sprintf("required variable %s is missing a value: %s", e.Variable, e.Reason)
59+
}
60+
return fmt.Sprintf("required variable %s is missing a value", e.Variable)
61+
}
62+
5063
// Mapping is a user-supplied function which maps from variable names to values.
5164
// Returns the value as a string and a bool indicating whether
5265
// the value is present, to distinguish between an empty string
@@ -351,8 +364,9 @@ func withRequired(substitution string, mapping Mapping, sep string, valid func(s
351364
}
352365
value, ok := mapping(name)
353366
if !ok || !valid(value) {
354-
return "", true, &InvalidTemplateError{
355-
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
367+
return "", true, &MissingRequiredError{
368+
Reason: errorMessage,
369+
Variable: name,
356370
}
357371
}
358372
return value, true, nil

template/template_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func TestMandatoryVariableErrors(t *testing.T) {
302302
for _, tc := range testCases {
303303
_, err := Substitute(tc.template, defaultMapping)
304304
assert.ErrorContains(t, err, tc.expectedError)
305-
assert.ErrorType(t, err, reflect.TypeOf(&InvalidTemplateError{}))
305+
assert.ErrorType(t, err, reflect.TypeOf(&MissingRequiredError{}))
306306
}
307307
}
308308

@@ -324,7 +324,7 @@ func TestMandatoryVariableErrorsWithNestedExpansion(t *testing.T) {
324324
for _, tc := range testCases {
325325
_, err := Substitute(tc.template, defaultMapping)
326326
assert.ErrorContains(t, err, tc.expectedError)
327-
assert.ErrorType(t, err, reflect.TypeOf(&InvalidTemplateError{}))
327+
assert.ErrorType(t, err, reflect.TypeOf(&MissingRequiredError{}))
328328
}
329329
}
330330

@@ -388,8 +388,9 @@ func TestPrecedence(t *testing.T) {
388388
{
389389
template: "${UNSET_VAR?bar-baz}", // Unexistent variable
390390
expected: "",
391-
err: &InvalidTemplateError{
392-
Template: "required variable UNSET_VAR is missing a value: bar-baz",
391+
err: &MissingRequiredError{
392+
Variable: "UNSET_VAR",
393+
Reason: "bar-baz",
393394
},
394395
},
395396
{

0 commit comments

Comments
 (0)