From 2b8e876bdb62bfae0d6b93df0b04b265bbb815ba Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Thu, 5 Dec 2019 16:00:19 -0500 Subject: [PATCH 1/2] Don't inspect an empty set, return false --- command/format/diff.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/command/format/diff.go b/command/format/diff.go index 8b1a7edf1e2b..2f2258de0c99 100644 --- a/command/format/diff.go +++ b/command/format/diff.go @@ -1014,8 +1014,9 @@ func (p *blockBodyDiffPrinter) writeActionSymbol(action plans.Action) { } func (p *blockBodyDiffPrinter) pathForcesNewResource(path cty.Path) bool { - if !p.action.IsReplace() { - // "requiredReplace" only applies when the instance is being replaced + if !p.action.IsReplace() || p.requiredReplace.Empty() { + // "requiredReplace" only applies when the instance is being replaced, + // and we should only inspect that set if it is not empty return false } return p.requiredReplace.Has(path) From 9c4d3cc1b1b6c8be8ce87aa4c419b67852ef3417 Mon Sep 17 00:00:00 2001 From: Pam Selle <204372+pselle@users.noreply.github.com> Date: Fri, 6 Dec 2019 11:53:43 -0500 Subject: [PATCH 2/2] Add a test --- command/show_test.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/command/show_test.go b/command/show_test.go index 30da8c572482..ba70ade401af 100644 --- a/command/show_test.go +++ b/command/show_test.go @@ -101,8 +101,34 @@ func TestShow_plan(t *testing.T) { } } +func TestShow_planWithChanges(t *testing.T) { + planPathWithChanges := showFixturePlanFile(t, plans.DeleteThenCreate) + + ui := cli.NewMockUi() + c := &ShowCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(showFixtureProvider()), + Ui: ui, + }, + } + + args := []string{ + planPathWithChanges, + } + + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } + + want := `test_instance.foo must be replaced` + got := ui.OutputWriter.String() + if !strings.Contains(got, want) { + t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got) + } +} + func TestShow_plan_json(t *testing.T) { - planPath := showFixturePlanFile(t) + planPath := showFixturePlanFile(t, plans.Create) ui := new(cli.MockUi) c := &ShowCommand{ @@ -377,9 +403,10 @@ func showFixtureProvider() *terraform.MockProvider { } // showFixturePlanFile creates a plan file at a temporary location containing a -// single change to create the test_instance.foo that is included in the "show" +// single change to create or update the test_instance.foo that is included in the "show" // test fixture, returning the location of that plan file. -func showFixturePlanFile(t *testing.T) string { +// `action` is the planned change you would like to elicit +func showFixturePlanFile(t *testing.T, action plans.Action) string { _, snap := testModuleWithSnapshot(t, "show") plannedVal := cty.ObjectVal(map[string]cty.Value{ "id": cty.UnknownVal(cty.String), @@ -402,7 +429,7 @@ func showFixturePlanFile(t *testing.T) string { }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance), ProviderAddr: addrs.ProviderConfig{Type: "test"}.Absolute(addrs.RootModuleInstance), ChangeSrc: plans.ChangeSrc{ - Action: plans.Create, + Action: action, Before: priorValRaw, After: plannedValRaw, },