From 8684a85e26bbaa9b17e3ed7c043c92443554fa6b Mon Sep 17 00:00:00 2001 From: Chris Arcand Date: Tue, 21 Sep 2021 22:00:32 -0500 Subject: [PATCH] command: Ensure all answers were used in command.testInputResponseMap Remove answers from testInputResponse as they are given, and raise an error during cleanup if any answers remain unused. This enables tests to ensure that the expected mock answers are actually used in a test; previously, an entire branch of code including an input sequence could be omitted and the test(s) would not fail. The only test that had unused answers in this map is one leftover from legacy state migrations, a prompt that was removed in 7c93b2e5e637bdee37c5e505d13121d9bfee223d --- internal/command/command_test.go | 4 ++++ internal/command/init_test.go | 5 ----- internal/command/ui_input.go | 1 + 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/command/command_test.go b/internal/command/command_test.go index 70da240e8..d1a43cf65 100644 --- a/internal/command/command_test.go +++ b/internal/command/command_test.go @@ -716,6 +716,10 @@ func testInputMap(t *testing.T, answers map[string]string) func() { // Return the cleanup return func() { + if len(testInputResponseMap) > 0 { + t.Fatalf("expected no unused answers provided to command.testInputMap, got: %v", testInputResponseMap) + } + test = true testInputResponseMap = nil } diff --git a/internal/command/init_test.go b/internal/command/init_test.go index a0a5e0b51..b75da1f4f 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -540,11 +540,6 @@ func TestInit_backendConfigFileChange(t *testing.T) { defer os.RemoveAll(td) defer testChdir(t, td)() - // Ask input - defer testInputMap(t, map[string]string{ - "backend-migrate-to-new": "no", - })() - ui := new(cli.MockUi) view, _ := testView(t) c := &InitCommand{ diff --git a/internal/command/ui_input.go b/internal/command/ui_input.go index 930bbd842..071982dec 100644 --- a/internal/command/ui_input.go +++ b/internal/command/ui_input.go @@ -90,6 +90,7 @@ func (i *UIInput) Input(ctx context.Context, opts *terraform.InputOpts) (string, return "", fmt.Errorf("unexpected input request in test: %s", opts.Id) } + delete(testInputResponseMap, opts.Id) return v, nil }