From 7b77e20bdc804aff5e6f87f8bc61472494e0fa49 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sun, 14 Oct 2018 16:35:59 -0700 Subject: [PATCH] command: Fix several "terraform init" tests --- command/command_test.go | 12 ++++++++++++ command/init_test.go | 30 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index 26a96123e..4cb872eb5 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -812,3 +812,15 @@ func testLockState(sourceDir, path string) (func(), error) { } return deferFunc, nil } + +// normalizeJSON removes all insignificant whitespace from the given JSON buffer +// and returns it as a string for easier comparison. +func normalizeJSON(t *testing.T, src []byte) string { + t.Helper() + var buf bytes.Buffer + err := json.Compact(&buf, src) + if err != nil { + t.Fatalf("error normalizing JSON: %s", err) + } + return buf.String() +} diff --git a/command/init_test.go b/command/init_test.go index dd6e77d38..f2b39fd6b 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -63,6 +63,10 @@ func TestInit_multipleArgs(t *testing.T) { func TestInit_fromModule_explicitDest(t *testing.T) { dir := tempDir(t) + err := os.Mkdir(dir, os.ModePerm) + if err != nil { + t.Fatal(err) + } ui := new(cli.MockUi) c := &InitCommand{ @@ -129,6 +133,10 @@ func TestInit_fromModule_dstInSrc(t *testing.T) { } defer os.Chdir(cwd) + if err := os.Mkdir("foo", os.ModePerm); err != nil { + t.Fatal(err) + } + if _, err := os.Create("issue518.tf"); err != nil { t.Fatalf("err: %s", err) } @@ -176,8 +184,8 @@ func TestInit_get(t *testing.T) { // Check output output := ui.OutputWriter.String() - if !strings.Contains(output, "Getting source") { - t.Fatalf("doesn't look like get: %s", output) + if !strings.Contains(output, "foo in foo") { + t.Fatalf("doesn't look like we installed module 'foo': %s", output) } } @@ -209,7 +217,7 @@ func TestInit_getUpgradeModules(t *testing.T) { // Check output output := ui.OutputWriter.String() - if !strings.Contains(output, "Updating source") { + if !strings.Contains(output, "Upgrading modules...") { t.Fatalf("doesn't look like get upgrade: %s", output) } } @@ -314,7 +322,7 @@ func TestInit_backendConfigFile(t *testing.T) { // Read our saved backend config and verify we have our settings state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"hello"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } } @@ -346,7 +354,7 @@ func TestInit_backendConfigFileChange(t *testing.T) { // Read our saved backend config and verify we have our settings state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"hello"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } } @@ -373,7 +381,7 @@ func TestInit_backendConfigKV(t *testing.T) { // Read our saved backend config and verify we have our settings state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"hello"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } } @@ -447,7 +455,7 @@ func TestInit_backendReinitWithExtra(t *testing.T) { // Read our saved backend config and verify we have our settings state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"hello"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } @@ -460,7 +468,7 @@ func TestInit_backendReinitWithExtra(t *testing.T) { t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) } state = testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"hello"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"hello","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } if state.Backend.Hash != cHash { @@ -489,7 +497,7 @@ func TestInit_backendReinitConfigToExtra(t *testing.T) { // Read our saved backend config and verify we have our settings state := testDataStateRead(t, filepath.Join(DefaultDataDir, DefaultStateFilename)) - if got, want := string(state.Backend.ConfigRaw), `{"path":"foo"}`; got != want { + if got, want := normalizeJSON(t, state.Backend.ConfigRaw), `{"path":"foo","workspace_dir":null}`; got != want { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } @@ -932,7 +940,7 @@ func TestInit_getProviderCheckRequiredVersion(t *testing.T) { defer os.RemoveAll(td) defer testChdir(t, td)() - ui := new(cli.MockUi) + ui := cli.NewMockUi() c := &InitCommand{ Meta: Meta{ testingOverrides: metaOverridesForProvider(testProvider()), @@ -942,7 +950,7 @@ func TestInit_getProviderCheckRequiredVersion(t *testing.T) { args := []string{} if code := c.Run(args); code != 1 { - t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String()) } }