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
8 changes: 8 additions & 0 deletions internal/command/meta_vars.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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