Skip to content
Merged
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
6 changes: 3 additions & 3 deletions dotenv/godotenv_var_expansion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ func TestErrorIfEmptyOrUnset(t *testing.T) {
"Error empty or unset: UNSET_VAR",
"RESULT=${UNSET_VAR:?Test error}",
"RESULT=${UNSET_VAR:?Test error}",
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"},
&template.MissingRequiredError{Variable: "UNSET_VAR", Reason: "Test error"},
},
{
"Error empty or unset: EMPTY_VAR",
"RESULT=${EMPTY_VAR:?Test error}",
"RESULT=${EMPTY_VAR:?Test error}",
&template.InvalidTemplateError{Template: "required variable EMPTY_VAR is missing a value: Test error"},
&template.MissingRequiredError{Variable: "EMPTY_VAR", Reason: "Test error"},
},
{
"Error empty or unset: TEST_VAR",
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestErrorIfUnset(t *testing.T) {
"Error on unset: UNSET_VAR",
"RESULT=${UNSET_VAR?Test error}",
"RESULT=${UNSET_VAR?Test error}",
&template.InvalidTemplateError{Template: "required variable UNSET_VAR is missing a value: Test error"},
&template.MissingRequiredError{Variable: "UNSET_VAR", Reason: "Test error"},
},
{
"Error on unset: EMPTY_VAR",
Expand Down
3 changes: 1 addition & 2 deletions interpolation/interpolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ func TestValidUnexistentInterpolation(t *testing.T) {
}

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

for _, testcase := range testcases {
Expand Down
2 changes: 1 addition & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,6 @@ func TestDeviceWriteBps(t *testing.T) {

func TestInvalidProjectNameType(t *testing.T) {
p, err := loadYAML(`name: 123`)
assert.Error(t, err, "name must be a string")
assert.Error(t, err, "validating filename0.yml: name must be a string")
assert.Assert(t, is.Nil(p))
}
18 changes: 16 additions & 2 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func (e InvalidTemplateError) Error() string {
return fmt.Sprintf("Invalid template: %#v", e.Template)
}

// MissingRequiredError is returned when a variable template is missing
type MissingRequiredError struct {
Variable string
Reason string
}

func (e MissingRequiredError) Error() string {
if e.Reason != "" {
return fmt.Sprintf("required variable %s is missing a value: %s", e.Variable, e.Reason)
}
return fmt.Sprintf("required variable %s is missing a value", e.Variable)
}

// Mapping is a user-supplied function which maps from variable names to values.
// Returns the value as a string and a bool indicating whether
// the value is present, to distinguish between an empty string
Expand Down Expand Up @@ -351,8 +364,9 @@ func withRequired(substitution string, mapping Mapping, sep string, valid func(s
}
value, ok := mapping(name)
if !ok || !valid(value) {
return "", true, &InvalidTemplateError{
Template: fmt.Sprintf("required variable %s is missing a value: %s", name, errorMessage),
return "", true, &MissingRequiredError{
Reason: errorMessage,
Variable: name,
}
}
return value, true, nil
Expand Down
9 changes: 5 additions & 4 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestMandatoryVariableErrors(t *testing.T) {
for _, tc := range testCases {
_, err := Substitute(tc.template, defaultMapping)
assert.ErrorContains(t, err, tc.expectedError)
assert.ErrorType(t, err, reflect.TypeOf(&InvalidTemplateError{}))
assert.ErrorType(t, err, reflect.TypeOf(&MissingRequiredError{}))
}
}

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

Expand Down Expand Up @@ -388,8 +388,9 @@ func TestPrecedence(t *testing.T) {
{
template: "${UNSET_VAR?bar-baz}", // Unexistent variable
expected: "",
err: &InvalidTemplateError{
Template: "required variable UNSET_VAR is missing a value: bar-baz",
err: &MissingRequiredError{
Variable: "UNSET_VAR",
Reason: "bar-baz",
},
},
{
Expand Down