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
This commit is contained in:
Liam Cervante 2023-03-10 16:19:33 +01:00 committed by GitHub
parent 843befff29
commit 15ecdb66c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 6 deletions

View File

@ -84,7 +84,11 @@ func (plan Plan) renderHuman(renderer Renderer, mode plans.Mode, opts ...PlanRen
}
}
if len(changes) == 0 && len(diffs.outputs) == 0 {
// Precompute the outputs early, so we can make a decision about whether we
// display the "there are no changes messages".
outputs := renderHumanDiffOutputs(renderer, diffs.outputs)
if len(changes) == 0 && len(outputs) == 0 {
// If we didn't find any changes to report at all then this is a
// "No changes" plan. How we'll present this depends on whether
// 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
counts[plans.Delete]+counts[plans.DeleteThenCreate]+counts[plans.CreateThenDelete])
}
diff := renderHumanDiffOutputs(renderer, diffs.outputs)
if len(diff) > 0 {
if len(outputs) > 0 {
renderer.Streams.Print("\nChanges to Outputs:\n")
renderer.Streams.Printf("%s\n", diff)
renderer.Streams.Printf("%s\n", outputs)
if len(counts) == 0 {
// If we have output changes but not resource changes then we

View File

@ -1,17 +1,17 @@
package jsonformat
import (
"encoding/json"
"fmt"
"testing"
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
"github.com/google/go-cmp/cmp"
"github.com/mitchellh/colorstring"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/command/jsonformat/differ"
"github.com/hashicorp/terraform/internal/command/jsonformat/differ/attribute_path"
"github.com/hashicorp/terraform/internal/command/jsonplan"
"github.com/hashicorp/terraform/internal/command/jsonprovider"
"github.com/hashicorp/terraform/internal/configs/configschema"
@ -19,9 +19,63 @@ import (
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/terminal"
"github.com/hashicorp/terraform/internal/terraform"
)
func TestRenderHuman_EmptyPlan(t *testing.T) {
color := &colorstring.Colorize{Colors: colorstring.DefaultColors, Disable: true}
streams, done := terminal.StreamsForTesting(t)
plan := Plan{}
renderer := Renderer{Colorize: color, Streams: streams}
plan.renderHuman(renderer, plans.NormalMode)
want := `
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`
got := done(t).Stdout()
if diff := cmp.Diff(want, got); len(diff) > 0 {
t.Errorf("unexpected output\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)
}
}
func TestRenderHuman_EmptyOutputs(t *testing.T) {
color := &colorstring.Colorize{Colors: colorstring.DefaultColors, Disable: true}
streams, done := terminal.StreamsForTesting(t)
outputVal, _ := json.Marshal("some-text")
plan := Plan{
OutputChanges: map[string]jsonplan.Change{
"a_string": {
Actions: []string{"no-op"},
Before: outputVal,
After: outputVal,
},
},
}
renderer := Renderer{Colorize: color, Streams: streams}
plan.renderHuman(renderer, plans.NormalMode)
want := `
No changes. Your infrastructure matches the configuration.
Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`
got := done(t).Stdout()
if diff := cmp.Diff(want, got); len(diff) > 0 {
t.Errorf("unexpected output\ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)
}
}
func TestResourceChange_primitiveTypes(t *testing.T) {
testCases := map[string]testCase{
"creation": {