From 501cbeaffe905eb5a5ec83864b89b21dff25f9f6 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 5 Jul 2017 17:47:05 -0400 Subject: [PATCH] testState shouldn't rely on mods from WriteState The state returned from the testState helper shouldn't rely on any mutations caused by WriteState. The Init function (which is analogous to NewState) shoudl set any required fields. --- command/command_test.go | 16 +--------------- terraform/state.go | 1 + 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/command/command_test.go b/command/command_test.go index a416256f8..1507b5b96 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -160,7 +160,6 @@ func testReadPlan(t *testing.T, path string) *terraform.Plan { // testState returns a test State structure that we use for a lot of tests. func testState() *terraform.State { state := &terraform.State{ - Version: 2, Modules: []*terraform.ModuleState{ &terraform.ModuleState{ Path: []string{"root"}, @@ -177,20 +176,7 @@ func testState() *terraform.State { }, } state.Init() - - // Write and read the state so that it is properly initialized. We - // do this since we didn't call the normal NewState constructor. - var buf bytes.Buffer - if err := terraform.WriteState(state, &buf); err != nil { - panic(err) - } - - result, err := terraform.ReadState(&buf) - if err != nil { - panic(err) - } - - return result + return state } func testStateFile(t *testing.T, s *terraform.State) string { diff --git a/terraform/state.go b/terraform/state.go index a8fc5277b..0c46194d6 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -677,6 +677,7 @@ func (s *State) init() { if s.Version == 0 { s.Version = StateVersion } + if s.moduleByPath(rootModulePath) == nil { s.addModule(rootModulePath) }