Skip to content

Commit

Permalink
Panic if no commands
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 31, 2021
1 parent 0a67f26 commit ba9e95d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions acmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type Config struct {

// RunnerOf creates a Runner.
func RunnerOf(cmds []Command, cfg Config) *Runner {
if len(cmds) == 0 {
panic("acmd: cannot run without commands")
}

r := &Runner{
cmds: cmds,
cfg: cfg,
Expand Down
14 changes: 11 additions & 3 deletions acmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func TestRunnerMustSortCommands(t *testing.T) {
return r.cmds[i].Name < r.cmds[j].Name
})
}
func TestRunnerPanicWithoutCommands(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Fatal("must be panic")
}
}()
RunnerOf(nil, Config{})
}

func TestRunnerInit(t *testing.T) {
testCases := []struct {
Expand Down Expand Up @@ -125,17 +133,17 @@ func TestRunner_suggestCommand(t *testing.T) {
want: `"fooo" is not a subcommand, did you mean "foo"?` + "\n",
},
{
cmds: []Command{},
cmds: []Command{{Name: "for", Do: nopFunc}},
args: []string{"hell"},
want: `"hell" is not a subcommand, did you mean "help"?` + "\n",
},
{
cmds: []Command{},
cmds: []Command{{Name: "for", Do: nopFunc}},
args: []string{"verZION"},
want: "",
},
{
cmds: []Command{},
cmds: []Command{{Name: "for", Do: nopFunc}},
args: []string{"verZion"},
want: `"verZion" is not a subcommand, did you mean "version"?` + "\n",
},
Expand Down

0 comments on commit ba9e95d

Please sign in to comment.