Skip to content

Commit

Permalink
Add more test cases for version help/usage writing
Browse files Browse the repository at this point in the history
  • Loading branch information
hhromic committed Jan 18, 2024
1 parent abf7ff3 commit b72cae7
Showing 1 changed file with 225 additions and 1 deletion.
226 changes: 225 additions & 1 deletion usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func (versioned) Version() string {
return "example 3.2.1"
}

func TestUsageWithVersion(t *testing.T) {
func TestUsageWithBuiltinVersion(t *testing.T) {
expectedUsage := "example 3.2.1\nUsage: example"

expectedHelp := `
Expand All @@ -260,6 +260,230 @@ Options:
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

func TestUsageWithArgsVersion(t *testing.T) {
var args struct {
Version bool `arg:"-V,--version" help:"display version and build info"`
}

expectedUsage := "Usage: example [--version]"

expectedHelp := `
Usage: example [--version]
Options:
--version, -V display version and build info
--help, -h display this help and exit
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

func TestUsageWithBuiltinAndArgsVersion(t *testing.T) {
var args struct {
versioned
VersionFlag bool `arg:"-V,--version" help:"display version and build info"`
}

expectedUsage := "Usage: example [--version]"

expectedHelp := `
Usage: example [--version]
Options:
--version, -V display version and build info
--help, -h display this help and exit
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

func TestUsageWithBuiltinVersionAndSubcommands(t *testing.T) {
type cmd struct {
Test int `arg:"-t,--test" help:"test number"`
}
var args struct {
versioned
Cmd *cmd `arg:"subcommand"`
}

expectedUsage := "example 3.2.1\nUsage: example <command> [<args>]"

expectedHelp := `
example 3.2.1
Usage: example <command> [<args>]
Options:
--help, -h display this help and exit
--version display version and exit
Commands:
cmd
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))

expectedUsage = "example 3.2.1\nUsage: example cmd [--test TEST]"

expectedHelp = `
example 3.2.1
Usage: example cmd [--test TEST]
Options:
--test TEST, -t TEST test number
--help, -h display this help and exit
--version display version and exit
`
_ = p.Parse([]string{"cmd"})

help = bytes.Buffer{}
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

usage = bytes.Buffer{}
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

func TestUsageWithArgsVersionAndSubcommands(t *testing.T) {
type cmd struct {
Test int `arg:"-t,--test" help:"test number"`
}
var args struct {
Cmd *cmd `arg:"subcommand"`
Version bool `arg:"-V,--version" help:"display version and build info"`
}

expectedUsage := "Usage: example [--version] <command> [<args>]"

expectedHelp := `
Usage: example [--version] <command> [<args>]
Options:
--version, -V display version and build info
--help, -h display this help and exit
Commands:
cmd
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))

expectedUsage = "Usage: example cmd [--test TEST]"

expectedHelp = `
Usage: example cmd [--test TEST]
Options:
--test TEST, -t TEST test number
Global options:
--version, -V display version and build info
--help, -h display this help and exit
`
_ = p.Parse([]string{"cmd"})

help = bytes.Buffer{}
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

usage = bytes.Buffer{}
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

func TestUsageWithBuiltinAndArgsVersionAndSubcommands(t *testing.T) {
type cmd struct {
Test int `arg:"-t,--test" help:"test number"`
}
var args struct {
versioned
Cmd *cmd `arg:"subcommand"`
Version bool `arg:"-V,--version" help:"display version and build info"`
}

expectedUsage := "Usage: example [--version] <command> [<args>]"

expectedHelp := `
Usage: example [--version] <command> [<args>]
Options:
--version, -V display version and build info
--help, -h display this help and exit
Commands:
cmd
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)

var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))

expectedUsage = "Usage: example cmd [--test TEST]"

expectedHelp = `
Usage: example cmd [--test TEST]
Options:
--test TEST, -t TEST test number
Global options:
--version, -V display version and build info
--help, -h display this help and exit
`
_ = p.Parse([]string{"cmd"})

help = bytes.Buffer{}
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())

usage = bytes.Buffer{}
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
}

type described struct{}

// Described returns the description for this program
Expand Down

0 comments on commit b72cae7

Please sign in to comment.