command: Fix several "terraform init" tests

This commit is contained in:
Martin Atkins 2018-10-14 16:35:59 -07:00
parent 89d0944e5b
commit 7b77e20bdc
2 changed files with 31 additions and 11 deletions

View File

@ -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()
}

View File

@ -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())
}
}