Merge pull request #15246 from hashicorp/b-fix-cmd-provider-crash

command/providers: Avoid crash when no configs found
This commit is contained in:
Radek Simko 2017-06-12 15:48:55 +01:00 committed by GitHub
commit 4e85f552e7
2 changed files with 38 additions and 0 deletions

View File

@ -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()) }
@ -43,6 +44,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()

View File

@ -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)
}
}