Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix no-op outputs causing the plan renderer to skip the 'no changes' message #32820

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions internal/command/jsonformat/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
58 changes: 56 additions & 2 deletions internal/command/jsonformat/plan_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,81 @@
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"
"github.com/hashicorp/terraform/internal/lang/marks"
"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": {
Expand Down