Merge pull request #244 from alexflint/restore-100pct-coverage

Restore 100% test coverage
This commit is contained in:
Alex Flint 2024-04-02 12:14:49 -04:00 committed by GitHub
commit 188bd31bf6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 85 additions and 7 deletions

View File

@ -86,13 +86,6 @@ func MustParse(dest ...interface{}) *Parser {
// mustParse is a helper that facilitates testing // mustParse is a helper that facilitates testing
func mustParse(config Config, dest ...interface{}) *Parser { func mustParse(config Config, dest ...interface{}) *Parser {
if config.Exit == nil {
config.Exit = os.Exit
}
if config.Out == nil {
config.Out = os.Stdout
}
p, err := NewParser(config, dest...) p, err := NewParser(config, dest...)
if err != nil { if err != nil {
fmt.Fprintln(config.Out, err) fmt.Fprintln(config.Out, err)

View File

@ -1737,3 +1737,11 @@ func TestSubcommandGlobalFlag_InCommand_Strict_Inner(t *testing.T) {
require.NotNil(t, args.Sub) require.NotNil(t, args.Sub)
assert.True(t, args.Sub.Guard) assert.True(t, args.Sub.Guard)
} }
func TestExitFunctionAndOutStreamGetFilledIn(t *testing.T) {
var args struct{}
p, err := NewParser(Config{}, &args)
require.NoError(t, err)
assert.NotNil(t, p.config.Exit) // go prohibits function pointer comparison
assert.Equal(t, p.config.Out, os.Stdout)
}

View File

@ -260,6 +260,39 @@ Options:
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String())) assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
} }
type userDefinedVersionFlag struct {
ShowVersion bool `arg:"--version" help:"this is a user-defined version flag"`
}
// Version returns the version for this program
func (userDefinedVersionFlag) Version() string {
return "example 3.2.1"
}
func TestUsageWithUserDefinedVersionFlag(t *testing.T) {
expectedUsage := "example 3.2.1\nUsage: example [--version]"
expectedHelp := `
example 3.2.1
Usage: example [--version]
Options:
--version this is a user-defined version flag
--help, -h display this help and exit
`
os.Args[0] = "example"
p, err := NewParser(Config{}, &userDefinedVersionFlag{})
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()))
}
type described struct{} type described struct{}
// Described returns the description for this program // Described returns the description for this program
@ -415,6 +448,50 @@ Options:
assert.Equal(t, expectedUsage, usage.String()) assert.Equal(t, expectedUsage, usage.String())
} }
func TestUsageWithSubcommands(t *testing.T) {
expectedUsage := "Usage: example child [--values VALUES]"
expectedHelp := `
Usage: example child [--values VALUES]
Options:
--values VALUES Values
Global options:
--verbose, -v verbosity level
--help, -h display this help and exit
`
var args struct {
Verbose bool `arg:"-v" help:"verbosity level"`
Child *struct {
Values []float64 `help:"Values"`
} `arg:"subcommand:child"`
}
os.Args[0] = "example"
p, err := NewParser(Config{}, &args)
require.NoError(t, err)
_ = p.Parse([]string{"child"})
var help bytes.Buffer
p.WriteHelp(&help)
assert.Equal(t, expectedHelp[1:], help.String())
var help2 bytes.Buffer
p.WriteHelpForSubcommand(&help2, "child")
assert.Equal(t, expectedHelp[1:], help2.String())
var usage bytes.Buffer
p.WriteUsage(&usage)
assert.Equal(t, expectedUsage, strings.TrimSpace(usage.String()))
var usage2 bytes.Buffer
p.WriteUsageForSubcommand(&usage2, "child")
assert.Equal(t, expectedUsage, strings.TrimSpace(usage2.String()))
}
func TestUsageWithNestedSubcommands(t *testing.T) { func TestUsageWithNestedSubcommands(t *testing.T) {
expectedUsage := "Usage: example child nested [--enable] OUTPUT" expectedUsage := "Usage: example child nested [--enable] OUTPUT"