From 86fbcfac83a82bc758874d100c860322bfb21f5b Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 10 Jun 2017 11:04:44 +0100 Subject: [PATCH 1/3] command/providers: Avoid crash when no configs found --- command/providers.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/command/providers.go b/command/providers.go index 3c1d14433..aeae44b15 100644 --- a/command/providers.go +++ b/command/providers.go @@ -43,6 +43,13 @@ func (c *ProvidersCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) return 1 } + if root == nil { + c.Ui.Error(fmt.Sprintf( + "No configuration files found in the directory: %s\n\n"+ + "This command requires configuration to run.", + configPath)) + return 1 + } // Validate the config (to ensure the version constraints are valid) err = root.Validate() From ee30df3efd0515696f2417a2dc680520c510fc82 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 10 Jun 2017 11:49:57 +0100 Subject: [PATCH 2/3] command/providers: Enable processing of meta-parameters This will enable proper colouring of the output, like for other commands. --- command/providers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/command/providers.go b/command/providers.go index aeae44b15..2a755f126 100644 --- a/command/providers.go +++ b/command/providers.go @@ -24,6 +24,7 @@ func (c *ProvidersCommand) Synopsis() string { } func (c *ProvidersCommand) Run(args []string) int { + c.Meta.process(args, false) cmdFlags := c.Meta.flagSet("providers") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } From f5ff67c5302887f3f8b3c194ae92809ee6eaaa87 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sat, 10 Jun 2017 12:02:01 +0100 Subject: [PATCH 3/3] command/providers: Add regression test --- command/providers_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/command/providers_test.go b/command/providers_test.go index eb6c0dfc2..163cae129 100644 --- a/command/providers_test.go +++ b/command/providers_test.go @@ -41,3 +41,33 @@ func TestProviders(t *testing.T) { t.Errorf("output missing provider.baz\n\n%s", output) } } + +func TestProviders_noConfigs(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(testFixturePath("")); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + ui := new(cli.MockUi) + c := &ProvidersCommand{ + Meta: Meta{ + Ui: ui, + }, + } + + args := []string{} + if code := c.Run(args); code == 0 { + t.Fatal("expected command to return non-zero exit code" + + " when no configs are available") + } + + output := ui.ErrorWriter.String() + expectedErrMsg := "No configuration files found" + if !strings.Contains(output, expectedErrMsg) { + t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output) + } +}