Merge pull request #244 from alexflint/restore-100pct-coverage
Restore 100% test coverage
This commit is contained in:
commit
188bd31bf6
7
parse.go
7
parse.go
|
@ -86,13 +86,6 @@ func MustParse(dest ...interface{}) *Parser {
|
|||
|
||||
// mustParse is a helper that facilitates testing
|
||||
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...)
|
||||
if err != nil {
|
||||
fmt.Fprintln(config.Out, err)
|
||||
|
|
|
@ -1737,3 +1737,11 @@ func TestSubcommandGlobalFlag_InCommand_Strict_Inner(t *testing.T) {
|
|||
require.NotNil(t, args.Sub)
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -260,6 +260,39 @@ Options:
|
|||
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{}
|
||||
|
||||
// Described returns the description for this program
|
||||
|
@ -415,6 +448,50 @@ Options:
|
|||
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) {
|
||||
expectedUsage := "Usage: example child nested [--enable] OUTPUT"
|
||||
|
||||
|
|
Loading…
Reference in New Issue