From f10163ecc74bed9009d68db6b13c4d10e507e117 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 18 Jul 2017 13:03:57 -0400 Subject: [PATCH] graph should not panic with no config The backends replace a nil module tree with an empty one before building the graph, so the graph command needs to do the same. --- command/graph.go | 6 ++++++ command/graph_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/command/graph.go b/command/graph.go index cd65a682a6..2fb29c7b0a 100644 --- a/command/graph.go +++ b/command/graph.go @@ -88,6 +88,12 @@ func (c *GraphCommand) Run(args []string) int { return 1 } + // Building a graph may require config module to be present, even if it's + // empty. + if mod == nil && plan == nil { + mod = module.NewEmptyTree() + } + // Build the operation opReq := c.Operation() opReq.Module = mod diff --git a/command/graph_test.go b/command/graph_test.go index cf6eaab2bc..89033b01bf 100644 --- a/command/graph_test.go +++ b/command/graph_test.go @@ -81,6 +81,28 @@ func TestGraph_noArgs(t *testing.T) { } } +func TestGraph_noConfig(t *testing.T) { + td := tempDir(t) + os.MkdirAll(td, 0755) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + ui := new(cli.MockUi) + c := &GraphCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(testProvider()), + Ui: ui, + }, + } + + // Running the graph command without a config should not panic, + // but this may be an error at some point in the future. + args := []string{"-type", "apply"} + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } +} + func TestGraph_plan(t *testing.T) { tmp, cwd := testCwd(t) defer testFixCwd(t, tmp, cwd)