Skip to content
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
8 changes: 4 additions & 4 deletions internal/command/apply_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ func TestApply_destroyApproveNo(t *testing.T) {

p := applyFixtureProvider()

defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "no",
})()
})

// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
Expand Down Expand Up @@ -219,9 +219,9 @@ func TestApply_destroyApproveYes(t *testing.T) {

p := applyFixtureProvider()

defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
})()
})

// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
Expand Down
10 changes: 5 additions & 5 deletions internal/command/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ func TestApply_approveNo(t *testing.T) {

statePath := testTempFile(t)

defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "no",
})()
})

// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
Expand Down Expand Up @@ -156,9 +156,9 @@ func TestApply_approveYes(t *testing.T) {

p := applyFixtureProvider()

defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
})()
})

// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
Expand Down Expand Up @@ -319,7 +319,6 @@ func TestApply_parallelism(t *testing.T) {
}
}
provider.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {

// If we ever have more than our intended parallelism number of
// apply operations running concurrently, the semaphore will fail.
select {
Expand Down Expand Up @@ -1943,6 +1942,7 @@ func TestApply_refreshFalse(t *testing.T) {
t.Fatal("should not call ReadResource when refresh=false")
}
}

func TestApply_shutdown(t *testing.T) {
// Create a temporary working directory that is empty
td := t.TempDir()
Expand Down
18 changes: 12 additions & 6 deletions internal/command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,23 +733,27 @@ func testInteractiveInput(t *testing.T, answers []string) func() {
// testInputMap configures tests so that the given answers are returned
// for calls to Input when the right question is asked. The key is the
// question "Id" that is used.
func testInputMap(t *testing.T, answers map[string]string) func() {
//
// Calling code can optionally use the returned buffer to make assertions
// about the prompts shown the to the user.
func testInputMap(t *testing.T, answers map[string]string) *bytes.Buffer {
t.Helper()

// Disable test mode so input is called
test = false

// Set up reader/writers
defaultInputReader = bytes.NewBufferString("")
defaultInputWriter = new(bytes.Buffer)
inputWriter := new(bytes.Buffer)
defaultInputWriter = inputWriter

// Setup answers
testInputResponse = nil
testInputResponseMap = answers

// Return the cleanup
return func() {
var unusedAnswers = testInputResponseMap
// Queue the cleanup for the end of the test
t.Cleanup(func() {
unusedAnswers := testInputResponseMap

// First, clean up!
test = true
Expand All @@ -758,7 +762,9 @@ func testInputMap(t *testing.T, answers map[string]string) func() {
if len(unusedAnswers) > 0 {
t.Fatalf("expected no unused answers provided to command.testInputMap, got: %v", unusedAnswers)
}
}
})

return inputWriter
}

// testBackendState is used to make a test HTTP server to test a configured
Expand Down
4 changes: 2 additions & 2 deletions internal/command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3799,9 +3799,9 @@ func TestInit_stateStore_newWorkingDir(t *testing.T) {

// Allow the test to respond to the prompt to pick an
// existing workspace, given the selected one doesn't exist.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"select-workspace": "1", // foobar1 in numbered list
})()
})

ui := new(cli.MockUi)
view, done := testView(t)
Expand Down
31 changes: 14 additions & 17 deletions internal/command/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ func TestLogin(t *testing.T) {
t.Run("app.terraform.io (no login support)", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt, then paste a token with some
// accidental whitespace.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
"token": " good-token ",
})()
})
status := c.Run([]string{"app.terraform.io"})
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand All @@ -131,9 +131,9 @@ func TestLogin(t *testing.T) {

t.Run("example.com with authorization code flow", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
})()
})
status := c.Run([]string{"example.com"})
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand All @@ -154,7 +154,6 @@ func TestLogin(t *testing.T) {
}))

t.Run("example.com results in no scopes", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {

host, _ := c.Services.Discover("example.com")
client, _ := host.ServiceOAuthClient("login.v1")
if len(client.Scopes) != 0 {
Expand All @@ -164,17 +163,16 @@ func TestLogin(t *testing.T) {

t.Run("with-scopes.example.com with authorization code flow and scopes", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
})()
})
status := c.Run([]string{"with-scopes.example.com"})
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
}

credsSrc := c.Services.CredentialsSource()
creds, err := credsSrc.ForHost(svchost.Hostname("with-scopes.example.com"))

if err != nil {
t.Errorf("failed to retrieve credentials: %s", err)
}
Expand All @@ -189,7 +187,6 @@ func TestLogin(t *testing.T) {
}))

t.Run("with-scopes.example.com results in expected scopes", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {

host, _ := c.Services.Discover("with-scopes.example.com")
client, _ := host.ServiceOAuthClient("login.v1")

Expand All @@ -206,10 +203,10 @@ func TestLogin(t *testing.T) {
t.Run("TFE host without login support", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt, then paste a token with some
// accidental whitespace.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
"token": " good-token ",
})()
})
status := c.Run([]string{"tfe.acme.com"})
if status != 0 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand All @@ -231,10 +228,10 @@ func TestLogin(t *testing.T) {

t.Run("TFE host without login support, incorrectly pasted token", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "yes" at the consent prompt, then paste an invalid token.
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "yes",
"token": "good-tok",
})()
})
status := c.Run([]string{"tfe.acme.com"})
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand Down Expand Up @@ -263,9 +260,9 @@ func TestLogin(t *testing.T) {

t.Run("answering no cancels", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "no" at the consent prompt
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "no",
})()
})
status := c.Run(nil)
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand All @@ -278,9 +275,9 @@ func TestLogin(t *testing.T) {

t.Run("answering y cancels", loginTestCase(func(t *testing.T, c *LoginCommand, ui *cli.MockUi) {
// Enter "y" at the consent prompt
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"approve": "y",
})()
})
status := c.Run(nil)
if status != 1 {
t.Fatalf("unexpected error code %d\nstderr:\n%s", status, ui.ErrorWriter.String())
Expand Down
4 changes: 1 addition & 3 deletions internal/command/meta_backend_migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestBackendMigrate_promptMultiStatePattern(t *testing.T) {
t.Log("Test: ", name)
m := testMetaBackend(t, nil)
input := map[string]string{}
cleanup := testInputMap(t, input)
_ = testInputMap(t, input)
if tc.renamePrompt != "" {
input["backend-migrate-multistate-to-tfc"] = tc.renamePrompt
}
Expand All @@ -58,7 +58,5 @@ func TestBackendMigrate_promptMultiStatePattern(t *testing.T) {
if tc.expectedErr != "" && tc.expectedErr != err.Error() {
t.Fatalf("expected error to eq %s but got %s", tc.expectedErr, err.Error())
}

cleanup()
}
}
37 changes: 21 additions & 16 deletions internal/command/meta_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,21 @@ func TestMetaBackend_initBackendSelectedWorkspaceDoesNotExist(t *testing.T) {
// Setup the meta
m := testMetaBackend(t, nil)

defer testInputMap(t, map[string]string{
terminalPrompts := testInputMap(t, map[string]string{
"select-workspace": "2",
})()
})

// Get the backend
_, diags := m.Backend(&BackendOpts{Init: true})
if diags.HasErrors() {
t.Fatal(diags.Err())
}

expectedMsg := "The currently selected workspace (bar) does not exist"
if !strings.Contains(terminalPrompts.String(), expectedMsg) {
t.Errorf("expected error message to contain %q, but got %q", expectedMsg, terminalPrompts.String())
}

expected := "foo"
actual, err := m.Workspace()
if err != nil {
Expand Down Expand Up @@ -963,9 +968,9 @@ func TestMetaBackend_configuredBackendChangeCopy_singleState(t *testing.T) {
defer backendInit.Set("local-single", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-copy-to-empty": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1017,9 +1022,9 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToSingleDefault(t *testing
defer backendInit.Set("local-single", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-copy-to-empty": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1070,10 +1075,10 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToSingle(t *testing.T) {
defer backendInit.Set("local-single", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-multistate-to-single": "yes",
"backend-migrate-copy-to-empty": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1139,10 +1144,10 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToSingleCurrentEnv(t *test
defer backendInit.Set("local-single", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-multistate-to-single": "yes",
"backend-migrate-copy-to-empty": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1200,9 +1205,9 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToMulti(t *testing.T) {
t.Chdir(td)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-multistate-to-multistate": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1300,10 +1305,10 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToNoDefaultWithDefault(t *
defer backendInit.Set("local-no-default", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-multistate-to-multistate": "yes",
"new-state-name": "env1",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down Expand Up @@ -1378,9 +1383,9 @@ func TestMetaBackend_configuredBackendChangeCopy_multiToNoDefaultWithoutDefault(
defer backendInit.Set("local-no-default", nil)

// Ask input
defer testInputMap(t, map[string]string{
_ = testInputMap(t, map[string]string{
"backend-migrate-multistate-to-multistate": "yes",
})()
})

// Setup the meta
m := testMetaBackend(t, nil)
Expand Down
Loading