Skip to content

Commit

Permalink
Merge pull request #9640 from hashicorp/jbardin/GH-9572
Browse files Browse the repository at this point in the history
Fix panic during "terraform show" with empty state
  • Loading branch information
jbardin authored Oct 26, 2016
2 parents deca12a + 43f860d commit 07d10da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions command/state_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (c *StateShowCommand) Run(args []string) int {
return 1
}

if instance == nil {
return 0
}

is := instance.Value.(*terraform.InstanceState)

// Sort the keys
Expand Down
30 changes: 29 additions & 1 deletion command/state_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestStateShow_noState(t *testing.T) {
}

func TestStateShow_emptyState(t *testing.T) {
state := &terraform.State{}
state := terraform.NewState()

statePath := testStateFile(t, state)

Expand All @@ -149,6 +149,34 @@ func TestStateShow_emptyState(t *testing.T) {
}
}

func TestStateShow_emptyStateWithModule(t *testing.T) {
// empty state with empty module
state := terraform.NewState()

mod := &terraform.ModuleState{
Path: []string{"root", "mod"},
}
state.Modules = append(state.Modules, mod)

statePath := testStateFile(t, state)

p := testProvider()
ui := new(cli.MockUi)
c := &StateShowCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

args := []string{
"-state", statePath,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}

const testStateShowOutput = `
id = bar
bar = value
Expand Down

0 comments on commit 07d10da

Please sign in to comment.