Skip to content

Commit 5fcae9a

Browse files
authored
terraform test: fix crash when non-applyable plans are applied (#36582)
1 parent d50fcf9 commit 5fcae9a

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: '`terraform test`: Fix crash when a run block attempts to cleanup after a non-applyable plan.'
3+
time: 2025-02-26T14:09:31.83904+01:00
4+
custom:
5+
Issue: "36582"

internal/backend/local/test.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,13 @@ func (runner *TestFileRunner) cleanup(file *moduletest.File) {
937937
for key, state := range runner.RelevantStates {
938938

939939
empty := true
940-
for _, module := range state.State.Modules {
941-
for _, resource := range module.Resources {
942-
if resource.Addr.Resource.Mode == addrs.ManagedResourceMode {
943-
empty = false
944-
break
940+
if !state.State.Empty() {
941+
for _, module := range state.State.Modules {
942+
for _, resource := range module.Resources {
943+
if resource.Addr.Resource.Mode == addrs.ManagedResourceMode {
944+
empty = false
945+
break
946+
}
945947
}
946948
}
947949
}

internal/command/test_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,11 @@ func TestTest_Runs(t *testing.T) {
295295
expectedErr: []string{"Test assertion failed", "resource renamed without moved block"},
296296
code: 1,
297297
},
298+
"unapplyable-plan": {
299+
expectedOut: []string{"0 passed, 1 failed."},
300+
expectedErr: []string{"Cannot apply non-applyable plan"},
301+
code: 1,
302+
},
298303
}
299304
for name, tc := range tcs {
300305
t.Run(name, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
resource "test_resource" "example" {
3+
value = "bar"
4+
}
5+
6+
output "value" {
7+
value = test_resource.example.value
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
run "test" {
3+
command = apply
4+
plan_options {
5+
mode = refresh-only
6+
}
7+
assert {
8+
condition = test_resource.example.value == "bar"
9+
error_message = "wrong value"
10+
}
11+
}

0 commit comments

Comments
 (0)