Skip to content

Commit c5cd0ac

Browse files
committed
terraform test: include expected diagnostics in verbose mode
1 parent 0374f04 commit c5cd0ac

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: 'terraform test: expected diagnostics will be included in test output when running in verbose mode"'
3+
time: 2025-07-23T12:29:22.244611+02:00
4+
custom:
5+
Issue: "37362"

internal/command/test_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,37 @@ func TestTest_Runs(t *testing.T) {
130130
expectedOut: []string{"1 passed, 0 failed."},
131131
code: 0,
132132
},
133+
"expect_failures_outputs": {
134+
expectedOut: []string{"1 passed, 0 failed."},
135+
code: 0,
136+
},
137+
"expect_failures_checks_verbose": {
138+
override: "expect_failures_checks",
139+
args: []string{"-verbose"},
140+
expectedOut: []string{"1 passed, 0 failed.", "Warning: Check block assertion failed"},
141+
code: 0,
142+
},
143+
"expect_failures_inputs_verbose": {
144+
override: "expect_failures_inputs",
145+
args: []string{"-verbose"},
146+
expectedOut: []string{"1 passed, 0 failed."},
147+
expectedErr: []string{"Error: Invalid value for variable"},
148+
code: 0,
149+
},
150+
"expect_failures_resources_verbose": {
151+
override: "expect_failures_resources",
152+
args: []string{"-verbose"},
153+
expectedOut: []string{"1 passed, 0 failed."},
154+
expectedErr: []string{"Error: Resource postcondition failed"},
155+
code: 0,
156+
},
157+
"expect_failures_outputs_verbose": {
158+
override: "expect_failures_outputs",
159+
args: []string{"-verbose"},
160+
expectedOut: []string{"1 passed, 0 failed."},
161+
expectedErr: []string{"Error: Module output value precondition failed"},
162+
code: 0,
163+
},
133164
"multiple_files": {
134165
expectedOut: []string{"2 passed, 0 failed"},
135166
code: 0,

internal/moduletest/graph/apply.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (n *NodeTestRun) testApply(ctx *EvalContext, variables terraform.InputValue
6363

6464
// Remove expected diagnostics, and add diagnostics in case anything that should have failed didn't.
6565
// We'll also update the run status based on the presence of errors or missing expected failures.
66-
failOrErr := n.checkForMissingExpectedFailures(run, applyDiags)
66+
failOrErr := n.checkForMissingExpectedFailures(ctx, run, applyDiags)
6767
if failOrErr {
6868
// Even though the apply operation failed, the graph may have done
6969
// partial updates and the returned state should reflect this.
@@ -172,11 +172,19 @@ func (n *NodeTestRun) apply(tfCtx *terraform.Context, plan *plans.Plan, progress
172172

173173
// checkForMissingExpectedFailures checks for missing expected failures in the diagnostics.
174174
// It updates the run status based on the presence of errors or missing expected failures.
175-
func (n *NodeTestRun) checkForMissingExpectedFailures(run *moduletest.Run, diags tfdiags.Diagnostics) (failOrErr bool) {
175+
func (n *NodeTestRun) checkForMissingExpectedFailures(ctx *EvalContext, run *moduletest.Run, diags tfdiags.Diagnostics) (failOrErr bool) {
176176
// Retrieve and append diagnostics that are either unrelated to expected failures
177177
// or report missing expected failures.
178178
unexpectedDiags := run.ValidateExpectedFailures(diags)
179-
run.Diagnostics = run.Diagnostics.Append(unexpectedDiags)
179+
180+
if ctx.Verbose() {
181+
// in verbose mode, we still add all the original diagnostics for
182+
// display even if they are expected.
183+
run.Diagnostics = run.Diagnostics.Append(diags)
184+
} else {
185+
run.Diagnostics = run.Diagnostics.Append(unexpectedDiags)
186+
}
187+
180188
for _, diag := range unexpectedDiags {
181189
// // If any diagnostic indicates a missing expected failure, set the run status to fail.
182190
if ok := moduletest.DiagnosticFromMissingExpectedFailure(diag); ok {

internal/moduletest/graph/plan.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ func (n *NodeTestRun) testPlan(ctx *EvalContext, variables terraform.InputValues
3232
tfCtx, _ := terraform.NewContext(n.opts.ContextOpts)
3333

3434
// execute the terraform plan operation
35-
planScope, plan, planDiags := n.plan(ctx, tfCtx, setVariables, providers, mocks, waiter)
35+
planScope, plan, originalDiags := n.plan(ctx, tfCtx, setVariables, providers, mocks, waiter)
3636
// We exclude the diagnostics that are expected to fail from the plan
3737
// diagnostics, and if an expected failure is not found, we add a new error diagnostic.
38-
planDiags = run.ValidateExpectedFailures(planDiags)
39-
run.Diagnostics = run.Diagnostics.Append(planDiags)
38+
planDiags := run.ValidateExpectedFailures(originalDiags)
39+
40+
if ctx.Verbose() {
41+
// in verbose mode, we still add all the original diagnostics for
42+
// display.
43+
run.Diagnostics = run.Diagnostics.Append(originalDiags)
44+
} else {
45+
run.Diagnostics = run.Diagnostics.Append(planDiags)
46+
}
47+
4048
if planDiags.HasErrors() {
4149
run.Status = moduletest.Error
4250
return

0 commit comments

Comments
 (0)