Skip to content

Commit

Permalink
terraform test: Fix crash when referencing variables within the file …
Browse files Browse the repository at this point in the history
…level variable block
  • Loading branch information
liamcervante committed Jan 16, 2024
1 parent 8da1c00 commit c953a28
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
19 changes: 16 additions & 3 deletions internal/backend/local/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ func (runner *TestFileRunner) GetVariables(config *configs.Config, run *modulete
values := make(terraform.InputValues)

// First, let's look at the global variables.
for name, value := range runner.globalVariables {
for name, variable := range runner.globalVariables {
if !relevantVariables[name] {
// Then this run block doesn't need this value.
continue
Expand All @@ -1039,9 +1039,22 @@ func (runner *TestFileRunner) GetVariables(config *configs.Config, run *modulete
parsingMode = cfg.ParsingMode
}

var valueDiags tfdiags.Diagnostics
values[name], valueDiags = value.ParseVariableValue(parsingMode)
value, valueDiags := variable.ParseVariableValue(parsingMode)
diags = diags.Append(valueDiags)
if diags.HasErrors() {
// We still add a value for this variable even though we couldn't
// parse it as we don't want to compound errors later. For example,
// the system would report this variable didn't have a value which
// would confuse the user because it does have a value, it's just
// not a valid value. We have added the diagnostics so the user
// will be informed about the error, and the test won't run. We'll
// just report only the relevant errors.
values[name] = &terraform.InputValue{
Value: cty.NilVal,
}
continue
}
values[name] = value
}

// Second, we'll check the run level variables.
Expand Down
7 changes: 6 additions & 1 deletion internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ func TestTest_Runs(t *testing.T) {
expectedOut: "1 passed, 0 failed.",
code: 0,
},
"global_var_refs": {
expectedOut: "2 failed, 1 skipped.",
expectedErr: "Variables may not be used here.",
code: 1,
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -281,7 +286,7 @@ func TestTest_Runs(t *testing.T) {
}

if !strings.Contains(output.Stdout(), tc.expectedOut) {
t.Errorf("output didn't contain expected string:\n\n%s", output.All())
t.Errorf("output didn't contain expected string:\n\n%s", output.Stdout())
}

if !strings.Contains(output.Stderr(), tc.expectedErr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

variables {
input = var.env_var_input
}

run "execute" {}
7 changes: 7 additions & 0 deletions internal/command/testdata/test/global_var_refs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "input" {
type = string
}

output "value" {
value = var.input
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

variables {
input = var.setup.value
}

run "setup" {
variables {
input = "hello"
}
}

run "execute" {
assert {
condition = output.value == "hello"
error_message = "bad output value"
}
}

0 comments on commit c953a28

Please sign in to comment.