terraform test: include expected diagnostics in verbose mode (#37362)

pull/37349/head
Liam Cervante 10 months ago committed by GitHub
parent c177897320
commit 160b6b35b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
kind: ENHANCEMENTS
body: 'terraform test: expected diagnostics will be included in test output when running in verbose mode"'
time: 2025-07-23T12:29:22.244611+02:00
custom:
Issue: "37362"

@ -130,6 +130,37 @@ func TestTest_Runs(t *testing.T) {
expectedOut: []string{"1 passed, 0 failed."},
code: 0,
},
"expect_failures_outputs": {
expectedOut: []string{"1 passed, 0 failed."},
code: 0,
},
"expect_failures_checks_verbose": {
override: "expect_failures_checks",
args: []string{"-verbose"},
expectedOut: []string{"1 passed, 0 failed.", "Warning: Check block assertion failed"},
code: 0,
},
"expect_failures_inputs_verbose": {
override: "expect_failures_inputs",
args: []string{"-verbose"},
expectedOut: []string{"1 passed, 0 failed."},
expectedErr: []string{"Error: Invalid value for variable"},
code: 0,
},
"expect_failures_resources_verbose": {
override: "expect_failures_resources",
args: []string{"-verbose"},
expectedOut: []string{"1 passed, 0 failed."},
expectedErr: []string{"Error: Resource postcondition failed"},
code: 0,
},
"expect_failures_outputs_verbose": {
override: "expect_failures_outputs",
args: []string{"-verbose"},
expectedOut: []string{"1 passed, 0 failed."},
expectedErr: []string{"Error: Module output value precondition failed"},
code: 0,
},
"multiple_files": {
expectedOut: []string{"2 passed, 0 failed"},
code: 0,

@ -63,7 +63,7 @@ func (n *NodeTestRun) testApply(ctx *EvalContext, variables terraform.InputValue
// Remove expected diagnostics, and add diagnostics in case anything that should have failed didn't.
// We'll also update the run status based on the presence of errors or missing expected failures.
failOrErr := n.checkForMissingExpectedFailures(run, applyDiags)
failOrErr := n.checkForMissingExpectedFailures(ctx, run, applyDiags)
if failOrErr {
// Even though the apply operation failed, the graph may have done
// partial updates and the returned state should reflect this.
@ -172,11 +172,19 @@ func (n *NodeTestRun) apply(tfCtx *terraform.Context, plan *plans.Plan, progress
// checkForMissingExpectedFailures checks for missing expected failures in the diagnostics.
// It updates the run status based on the presence of errors or missing expected failures.
func (n *NodeTestRun) checkForMissingExpectedFailures(run *moduletest.Run, diags tfdiags.Diagnostics) (failOrErr bool) {
func (n *NodeTestRun) checkForMissingExpectedFailures(ctx *EvalContext, run *moduletest.Run, diags tfdiags.Diagnostics) (failOrErr bool) {
// Retrieve and append diagnostics that are either unrelated to expected failures
// or report missing expected failures.
unexpectedDiags := run.ValidateExpectedFailures(diags)
run.Diagnostics = run.Diagnostics.Append(unexpectedDiags)
if ctx.Verbose() {
// in verbose mode, we still add all the original diagnostics for
// display even if they are expected.
run.Diagnostics = run.Diagnostics.Append(diags)
} else {
run.Diagnostics = run.Diagnostics.Append(unexpectedDiags)
}
for _, diag := range unexpectedDiags {
// // If any diagnostic indicates a missing expected failure, set the run status to fail.
if ok := moduletest.DiagnosticFromMissingExpectedFailure(diag); ok {

@ -32,11 +32,19 @@ func (n *NodeTestRun) testPlan(ctx *EvalContext, variables terraform.InputValues
tfCtx, _ := terraform.NewContext(n.opts.ContextOpts)
// execute the terraform plan operation
planScope, plan, planDiags := n.plan(ctx, tfCtx, setVariables, providers, mocks, waiter)
planScope, plan, originalDiags := n.plan(ctx, tfCtx, setVariables, providers, mocks, waiter)
// We exclude the diagnostics that are expected to fail from the plan
// diagnostics, and if an expected failure is not found, we add a new error diagnostic.
planDiags = run.ValidateExpectedFailures(planDiags)
run.Diagnostics = run.Diagnostics.Append(planDiags)
planDiags := run.ValidateExpectedFailures(originalDiags)
if ctx.Verbose() {
// in verbose mode, we still add all the original diagnostics for
// display.
run.Diagnostics = run.Diagnostics.Append(originalDiags)
} else {
run.Diagnostics = run.Diagnostics.Append(planDiags)
}
if planDiags.HasErrors() {
run.Status = moduletest.Error
return

Loading…
Cancel
Save