From aed7162e9a59aa7e3c1d35c7d2166a339c29a70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Bel=C3=A9m?= Date: Mon, 14 Feb 2022 20:20:03 +0000 Subject: [PATCH] cli: Fix state mv exit code for missing resource (#29839) --- internal/command/state_mv.go | 2 ++ internal/command/state_mv_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/command/state_mv.go b/internal/command/state_mv.go index a35cfd6aa..99bc82aa5 100644 --- a/internal/command/state_mv.go +++ b/internal/command/state_mv.go @@ -176,6 +176,8 @@ func (c *StateMvCommand) Run(args []string) int { msgInvalidSource, fmt.Sprintf("Cannot move %s: does not match anything in the current state.", sourceAddr), )) + c.showDiagnostics(diags) + return 1 } for _, rawAddrFrom := range sourceAddrs { switch addrFrom := rawAddrFrom.(type) { diff --git a/internal/command/state_mv_test.go b/internal/command/state_mv_test.go index 2778de61b..b183a8668 100644 --- a/internal/command/state_mv_test.go +++ b/internal/command/state_mv_test.go @@ -1685,6 +1685,34 @@ func TestStateMvHelp(t *testing.T) { } } +func TestStateMvInvalidSourceAddress(t *testing.T) { + state := states.BuildState(func(s *states.SyncState) {}) + statePath := testStateFile(t, state) + + p := testProvider() + ui := new(cli.MockUi) + view, _ := testView(t) + c := &StateMvCommand{ + StateMeta{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + Ui: ui, + View: view, + }, + }, + } + + args := []string{ + "-state", statePath, + "foo.bar1", + "foo.bar2", + } + code := c.Run(args) + if code != 1 { + t.Fatalf("expected error code 1, got:\n%d", code) + } +} + const testStateMvOutputOriginal = ` test_instance.baz: ID = foo