From 83cb277583164d05d28707e18418569ebe37512e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Mar 2015 16:35:27 -0700 Subject: [PATCH] command/output: don't panic if no root module in state [GH-1263] --- command/output.go | 2 +- command/output_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/command/output.go b/command/output.go index 05fd31f34..2e3c1bee0 100644 --- a/command/output.go +++ b/command/output.go @@ -39,7 +39,7 @@ func (c *OutputCommand) Run(args []string) int { } state := stateStore.State() - if len(state.RootModule().Outputs) == 0 { + if state.Empty() || len(state.RootModule().Outputs) == 0 { c.Ui.Error(fmt.Sprintf( "The state file has no outputs defined. Define an output\n" + "in your configuration with the `output` directive and re-run\n" + diff --git a/command/output_test.go b/command/output_test.go index fbcda9962..d3444c389 100644 --- a/command/output_test.go +++ b/command/output_test.go @@ -142,6 +142,27 @@ func TestOutput_noArgs(t *testing.T) { } } +func TestOutput_noState(t *testing.T) { + originalState := &terraform.State{} + statePath := testStateFile(t, originalState) + + ui := new(cli.MockUi) + c := &OutputCommand{ + Meta: Meta{ + ContextOpts: testCtxConfig(testProvider()), + Ui: ui, + }, + } + + args := []string{ + "-state", statePath, + "foo", + } + if code := c.Run(args); code != 1 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } +} + func TestOutput_noVars(t *testing.T) { originalState := &terraform.State{ Modules: []*terraform.ModuleState{