Skip to content

Commit

Permalink
Add new test for required flags
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Jun 28, 2024
1 parent 31ae48d commit 1c7a91e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
4 changes: 1 addition & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func ExampleCommand_Run_appHelp() {
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
&cli.StringFlag{Name: "id", Value: "abcd-dndndnd", Usage: "an id", Required: true},
},
Commands: []*cli.Command{
{
Expand All @@ -118,7 +117,7 @@ func ExampleCommand_Run_appHelp() {
defer cancel()

// Simulate the command line arguments
os.Args = []string{"greet", "--id", "foo-x", "help"}
os.Args = []string{"greet", "help"}

_ = cmd.Run(ctx, os.Args)
// Output:
Expand All @@ -144,7 +143,6 @@ func ExampleCommand_Run_appHelp() {
//
// GLOBAL OPTIONS:
// --name value a name to say (default: "bob")
// --id value an id
// --help, -h show help (default: false)
// --version, -v print the version (default: false)
}
Expand Down
30 changes: 30 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ func Test_ShowAppHelp_MultiLineDescription(t *testing.T) {
}
}

func Test_Help_RequiredFlagsNoDefault(t *testing.T) {
output := new(bytes.Buffer)

cmd := &Command{
Flags: []Flag{
&IntFlag{Name: "foo", Aliases: []string{"f"}, Required: true},
},
Writer: output,
}

_ = cmd.Run(buildTestContext(t), []string{"test", "-h"})

expected := `NAME:
test - A new cli application
USAGE:
test [global options] [command [command options]] [arguments...]
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--foo value, -f value
--help, -h show help (default: false)
`

assert.Contains(t, output.String(), expected,
"expected output to include usage text")
}

func Test_Help_Custom_Flags(t *testing.T) {
oldFlag := HelpFlag
defer func() {
Expand Down

0 comments on commit 1c7a91e

Please sign in to comment.