Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Improved error for invalid -var "foo = bar" #30985

Merged
merged 1 commit into from May 30, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/command/meta_vars.go
Expand Up @@ -102,6 +102,14 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue
}
name := raw[:eq]
rawVal := raw[eq+1:]
if strings.HasSuffix(name, " ") {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid -var option",
fmt.Sprintf("Variable name %q is invalid due to trailing space. Did you mean -var=\"%s=%s\"?", name, strings.TrimSuffix(name, " "), strings.TrimPrefix(rawVal, " ")),
))
continue
}
ret[name] = unparsedVariableValueString{
str: rawVal,
name: name,
Expand Down
45 changes: 45 additions & 0 deletions internal/command/plan_test.go
Expand Up @@ -575,6 +575,51 @@ func TestPlan_vars(t *testing.T) {
}
}

func TestPlan_varsInvalid(t *testing.T) {
testCases := []struct {
args []string
wantErr string
}{
{
[]string{"-var", "foo"},
`The given -var option "foo" is not correctly specified.`,
},
{
[]string{"-var", "foo = bar"},
`Variable name "foo " is invalid due to trailing space.`,
},
}

// Create a temporary working directory that is empty
td := t.TempDir()
testCopyDir(t, testFixturePath("plan-vars"), td)
defer testChdir(t, td)()

for _, tc := range testCases {
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
p := planVarsFixtureProvider()
view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}

code := c.Run(tc.args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stdout())
}

got := output.Stderr()
if !strings.Contains(got, tc.wantErr) {
t.Fatalf("bad error output, want %q, got:\n%s", tc.wantErr, got)
}
})
}
}

func TestPlan_varsUnset(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
Expand Down