From d95b27d48d7f4e53b264c933def414da02899721 Mon Sep 17 00:00:00 2001 From: Thomas Mailund Date: Fri, 25 Jun 2021 08:43:37 +0200 Subject: [PATCH] testing that NewCommand can panic... --- commands.go | 29 ++++++----------------------- commands_test.go | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/commands.go b/commands.go index b071670..a1afb90 100644 --- a/commands.go +++ b/commands.go @@ -237,8 +237,8 @@ func DefaultUsage(cmd *Command) func() { } } -// NewMenuError Create a new menu command, i.e. a command that will dispatch to -// sub-commands. This is a convinience wrapper around a NewCommand with a spec +// NewMenu creates a menu command and panics if there are errors. +// This is a convinience wrapper around a NewCommand with a spec // that has subcommands. It returns a new command or an error. It can only error // if there is an error in the specification, so usually you do not need this function // but should use NewMenu instead. @@ -249,32 +249,15 @@ func DefaultUsage(cmd *Command) func() { // usage when the command is part of a Menu or when long is empty. // - long: Long description of the command, used when printing usage. // - subcmds: The subcommands you can invoke through this menu. -func NewMenuError(name, short, long string, subcmds ...*Command) (*Command, error) { - return NewCommandError(CommandSpec{ +func NewMenu(name, short, long string, subcmds ...*Command) *Command { + // A menu cannot fail. The subcommands are already parsed, and we don't + // provide any specs that can fail through the documentation strings. + cmd, _ := NewCommandError(CommandSpec{ Name: name, Short: short, Long: long, Subcommands: subcmds, }) -} - -// NewMenu creates a menu command and panics if there are errors. -// This is a convinience wrapper around a NewCommand with a spec -// that has subcommands. It returns a new command or an error. It can only error -// if there is an error in the specification, so usually you do not need this function -// but should use NewMenu instead. -// -// Parameters: -// - name: Name of the command, used when printing usage. -// - short: Short description of the command, used when printing -// usage when the command is part of a Menu or when long is empty. -// - long: Long description of the command, used when printing usage. -// - subcmds: The subcommands you can invoke through this menu. -func NewMenu(name, short, long string, subcmds ...*Command) *Command { - cmd, err := NewMenuError(name, short, long, subcmds...) - if err != nil { - panic(fmt.Sprintf("Error: %s", err)) - } return cmd } diff --git a/commands_test.go b/commands_test.go index b764efb..c9e3930 100644 --- a/commands_test.go +++ b/commands_test.go @@ -313,3 +313,19 @@ func TestMenuFailure(t *testing.T) { t.Errorf("Expected different error message than %s\n", errmsg) } } + +func TestCommandPanic(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Error("The code did not panic") + } + }() + + type Invalid struct { + X complex128 `pos:"invalid type"` + } + + _ = cli.NewCommand(cli.CommandSpec{ + Init: func() interface{} { return new(Invalid) }, + }) +}