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.
This commit is contained in:
James Bardin 2017-07-05 17:47:05 -04:00
parent fba5decae5
commit 501cbeaffe
2 changed files with 2 additions and 15 deletions

View File

@ -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 {

View File

@ -677,6 +677,7 @@ func (s *State) init() {
if s.Version == 0 {
s.Version = StateVersion
}
if s.moduleByPath(rootModulePath) == nil {
s.addModule(rootModulePath)
}