Merge pull request #15246 from hashicorp/b-fix-cmd-provider-crash
command/providers: Avoid crash when no configs found
This commit is contained in:
commit
4e85f552e7
|
@ -24,6 +24,7 @@ func (c *ProvidersCommand) Synopsis() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ProvidersCommand) Run(args []string) int {
|
func (c *ProvidersCommand) Run(args []string) int {
|
||||||
|
c.Meta.process(args, false)
|
||||||
|
|
||||||
cmdFlags := c.Meta.flagSet("providers")
|
cmdFlags := c.Meta.flagSet("providers")
|
||||||
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
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))
|
c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err))
|
||||||
return 1
|
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)
|
// Validate the config (to ensure the version constraints are valid)
|
||||||
err = root.Validate()
|
err = root.Validate()
|
||||||
|
|
|
@ -41,3 +41,33 @@ func TestProviders(t *testing.T) {
|
||||||
t.Errorf("output missing provider.baz\n\n%s", output)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue