Skip to content

Commit 15ecdb6

Browse files
author
Liam Cervante
authored
Fix no-op outputs causing the plan renderer to skip the 'no changes' message (#32820)
* Fix no-op outputs causing the plan renderer to skip the 'no changes' message * fix imports
1 parent 843beff commit 15ecdb6

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

internal/command/jsonformat/plan.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,11 @@ func (plan Plan) renderHuman(renderer Renderer, mode plans.Mode, opts ...PlanRen
8484
}
8585
}
8686

87-
if len(changes) == 0 && len(diffs.outputs) == 0 {
87+
// Precompute the outputs early, so we can make a decision about whether we
88+
// display the "there are no changes messages".
89+
outputs := renderHumanDiffOutputs(renderer, diffs.outputs)
90+
91+
if len(changes) == 0 && len(outputs) == 0 {
8892
// If we didn't find any changes to report at all then this is a
8993
// "No changes" plan. How we'll present this depends on whether
9094
// the plan is "applyable" and, if so, whether it had refresh changes
@@ -219,10 +223,9 @@ func (plan Plan) renderHuman(renderer Renderer, mode plans.Mode, opts ...PlanRen
219223
counts[plans.Delete]+counts[plans.DeleteThenCreate]+counts[plans.CreateThenDelete])
220224
}
221225

222-
diff := renderHumanDiffOutputs(renderer, diffs.outputs)
223-
if len(diff) > 0 {
226+
if len(outputs) > 0 {
224227
renderer.Streams.Print("\nChanges to Outputs:\n")
225-
renderer.Streams.Printf("%s\n", diff)
228+
renderer.Streams.Printf("%s\n", outputs)
226229

227230
if len(counts) == 0 {
228231
// If we have output changes but not resource changes then we

internal/command/jsonformat/plan_test.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,81 @@
11
package jsonformat
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"testing"
67

7-
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
8-
98
"github.com/google/go-cmp/cmp"
109
"github.com/mitchellh/colorstring"
1110
"github.com/zclconf/go-cty/cty"
1211

1312
"github.com/hashicorp/terraform/internal/addrs"
1413
"github.com/hashicorp/terraform/internal/command/jsonformat/differ"
14+
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
1515
"github.com/hashicorp/terraform/internal/command/jsonplan"
1616
"github.com/hashicorp/terraform/internal/command/jsonprovider"
1717
"github.com/hashicorp/terraform/internal/configs/configschema"
1818
"github.com/hashicorp/terraform/internal/lang/marks"
1919
"github.com/hashicorp/terraform/internal/plans"
2020
"github.com/hashicorp/terraform/internal/providers"
2121
"github.com/hashicorp/terraform/internal/states"
22+
"github.com/hashicorp/terraform/internal/terminal"
2223
"github.com/hashicorp/terraform/internal/terraform"
2324
)
2425

26+
func TestRenderHuman_EmptyPlan(t *testing.T) {
27+
color := &colorstring.Colorize{Colors: colorstring.DefaultColors, Disable: true}
28+
streams, done := terminal.StreamsForTesting(t)
29+
30+
plan := Plan{}
31+
32+
renderer := Renderer{Colorize: color, Streams: streams}
33+
plan.renderHuman(renderer, plans.NormalMode)
34+
35+
want := `
36+
No changes. Your infrastructure matches the configuration.
37+
38+
Terraform has compared your real infrastructure against your configuration
39+
and found no differences, so no changes are needed.
40+
`
41+
42+
got := done(t).Stdout()
43+
if diff := cmp.Diff(want, got); len(diff) > 0 {
44+
t.Errorf("unexpected output\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)
45+
}
46+
}
47+
48+
func TestRenderHuman_EmptyOutputs(t *testing.T) {
49+
color := &colorstring.Colorize{Colors: colorstring.DefaultColors, Disable: true}
50+
streams, done := terminal.StreamsForTesting(t)
51+
52+
outputVal, _ := json.Marshal("some-text")
53+
plan := Plan{
54+
OutputChanges: map[string]jsonplan.Change{
55+
"a_string": {
56+
Actions: []string{"no-op"},
57+
Before: outputVal,
58+
After: outputVal,
59+
},
60+
},
61+
}
62+
63+
renderer := Renderer{Colorize: color, Streams: streams}
64+
plan.renderHuman(renderer, plans.NormalMode)
65+
66+
want := `
67+
No changes. Your infrastructure matches the configuration.
68+
69+
Terraform has compared your real infrastructure against your configuration
70+
and found no differences, so no changes are needed.
71+
`
72+
73+
got := done(t).Stdout()
74+
if diff := cmp.Diff(want, got); len(diff) > 0 {
75+
t.Errorf("unexpected output\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)
76+
}
77+
}
78+
2579
func TestResourceChange_primitiveTypes(t *testing.T) {
2680
testCases := map[string]testCase{
2781
"creation": {

0 commit comments

Comments
 (0)