@ -1,7 +1,9 @@
package cli
import (
"context"
"flag"
"sort"
"os"
"strings"
"testing"
@ -22,7 +24,7 @@ func TestNewContext(t *testing.T) {
globalSet . Uint64 ( "myflagUint64" , uint64 ( 33 ) , "doc" )
globalSet . Float64 ( "myflag64" , float64 ( 47 ) , "doc" )
globalCtx := NewContext ( nil , globalSet , nil )
command := Command { Name : "mycommand" }
command := & Command { Name : "mycommand" }
c := NewContext ( nil , set , globalCtx )
c . Command = command
expect ( t , c . Int ( "myflag" ) , 12 )
@ -30,123 +32,108 @@ func TestNewContext(t *testing.T) {
expect ( t , c . Uint ( "myflagUint" ) , uint ( 93 ) )
expect ( t , c . Uint64 ( "myflagUint64" ) , uint64 ( 93 ) )
expect ( t , c . Float64 ( "myflag64" ) , float64 ( 17 ) )
expect ( t , c . GlobalInt ( "myflag" ) , 42 )
expect ( t , c . GlobalInt64 ( "myflagInt64" ) , int64 ( 42 ) )
expect ( t , c . GlobalUint ( "myflagUint" ) , uint ( 33 ) )
expect ( t , c . GlobalUint64 ( "myflagUint64" ) , uint64 ( 33 ) )
expect ( t , c . GlobalFloat64 ( "myflag64" ) , float64 ( 47 ) )
expect ( t , c . Command . Name , "mycommand" )
}
func TestContext_Int ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Int ( "myflag" , 12 , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Int ( "top-flag" , 13 , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Int ( "myflag" ) , 12 )
expect ( t , c . Int ( "top-flag" ) , 13 )
}
func TestContext_Int64 ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Int64 ( "myflagInt64" , 12 , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Int64 ( "top-flag" , 13 , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Int64 ( "myflagInt64" ) , int64 ( 12 ) )
expect ( t , c . Int64 ( "top-flag" ) , int64 ( 13 ) )
}
func TestContext_Uint ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Uint ( "myflagUint" , uint ( 13 ) , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Uint ( "top-flag" , uint ( 14 ) , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Uint ( "myflagUint" ) , uint ( 13 ) )
expect ( t , c . Uint ( "top-flag" ) , uint ( 14 ) )
}
func TestContext_Uint64 ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Uint64 ( "myflagUint64" , uint64 ( 9 ) , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Uint64 ( "top-flag" , uint64 ( 10 ) , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Uint64 ( "myflagUint64" ) , uint64 ( 9 ) )
}
func TestContext_GlobalInt ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Int ( "myflag" , 12 , "doc" )
c := NewContext ( nil , set , nil )
expect ( t , c . GlobalInt ( "myflag" ) , 12 )
expect ( t , c . GlobalInt ( "nope" ) , 0 )
}
func TestContext_GlobalInt64 ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Int64 ( "myflagInt64" , 12 , "doc" )
c := NewContext ( nil , set , nil )
expect ( t , c . GlobalInt64 ( "myflagInt64" ) , int64 ( 12 ) )
expect ( t , c . GlobalInt64 ( "nope" ) , int64 ( 0 ) )
expect ( t , c . Uint64 ( "top-flag" ) , uint64 ( 10 ) )
}
func TestContext_Float64 ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Float64 ( "myflag" , float64 ( 17 ) , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Float64 ( "top-flag" , float64 ( 18 ) , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Float64 ( "myflag" ) , float64 ( 17 ) )
}
func TestContext_GlobalFloat64 ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Float64 ( "myflag" , float64 ( 17 ) , "doc" )
c := NewContext ( nil , set , nil )
expect ( t , c . GlobalFloat64 ( "myflag" ) , float64 ( 17 ) )
expect ( t , c . GlobalFloat64 ( "nope" ) , float64 ( 0 ) )
expect ( t , c . Float64 ( "top-flag" ) , float64 ( 18 ) )
}
func TestContext_Duration ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Duration ( "myflag" , 12 * time . Second , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Duration ( "top-flag" , 13 * time . Second , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Duration ( "myflag" ) , 12 * time . Second )
expect ( t , c . Duration ( "top-flag" ) , 13 * time . Second )
}
func TestContext_String ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . String ( "myflag" , "hello world" , "doc" )
c := NewContext ( nil , set , nil )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . String ( "top-flag" , "hai veld" , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . String ( "myflag" ) , "hello world" )
expect ( t , c . String ( "top-flag" ) , "hai veld" )
}
func TestContext_ Bool ( t * testing . T ) {
func TestContext_ Path ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "myflag" , false , "doc" )
c := NewContext ( nil , set , nil )
expect ( t , c . Bool ( "myflag" ) , false )
}
func TestContext_BoolT ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "myflag" , true , "doc" )
c := NewContext ( nil , set , nil )
expect ( t , c . BoolT ( "myflag" ) , true )
}
func TestContext_GlobalBool ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
globalSet := flag . NewFlagSet ( "test-global" , 0 )
globalSet . Bool ( "myflag" , false , "doc" )
globalCtx := NewContext ( nil , globalSet , nil )
c := NewContext ( nil , set , globalCtx )
expect ( t , c . GlobalBool ( "myflag" ) , false )
expect ( t , c . GlobalBool ( "nope" ) , false )
set . String ( "path" , "path/to/file" , "path to file" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . String ( "top-path" , "path/to/top/file" , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Path ( "path" ) , "path/to/file" )
expect ( t , c . Path ( "top-path" ) , "path/to/top/file" )
}
func TestContext_ Global BoolT ( t * testing . T ) {
func TestContext_Bool ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
globalSet := flag . NewFlagSet ( "test-global" , 0 )
globalSet . Bool ( "myflag" , true , "doc" )
globalCtx := NewContext ( nil , globalSet , nil )
c := NewContext ( nil , set , globalCtx )
expect ( t , c . GlobalBoolT ( "myflag" ) , true )
expect ( t , c . GlobalBoolT ( "nope" ) , false )
set . Bool ( "myflag" , false , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
c := NewContext ( nil , set , parentCtx )
expect ( t , c . Bool ( "myflag" ) , false )
expect ( t , c . Bool ( "top-flag" ) , true )
}
func TestContext_Args ( t * testing . T ) {
@ -154,7 +141,7 @@ func TestContext_Args(t *testing.T) {
set . Bool ( "myflag" , false , "doc" )
c := NewContext ( nil , set , nil )
_ = set . Parse ( [ ] string { "--myflag" , "bat" , "baz" } )
expect ( t , len ( c . Args ( ) ) , 2 )
expect ( t , c . Args ( ) . Len ( ) , 2 )
expect ( t , c . Bool ( "myflag" ) , true )
}
@ -168,18 +155,22 @@ func TestContext_NArg(t *testing.T) {
func TestContext_IsSet ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "myflag" , false , "doc" )
set . String ( "otherflag" , "hello world" , "doc" )
globalSet := flag . NewFlagSet ( "test" , 0 )
globalSet . Bool ( "myflagGlobal" , true , "doc" )
globalCtx := NewContext ( nil , globalSet , nil )
c := NewContext ( nil , set , globalCtx )
_ = set . Parse ( [ ] string { "--myflag" , "bat" , "baz" } )
_ = globalSet . Parse ( [ ] string { "--myflagGlobal" , "bat" , "baz" } )
expect ( t , c . IsSet ( "myflag" ) , true )
expect ( t , c . IsSet ( "otherflag" ) , false )
expect ( t , c . IsSet ( "bogusflag" ) , false )
expect ( t , c . IsSet ( "myflagGlobal" ) , false )
set . Bool ( "one-flag" , false , "doc" )
set . Bool ( "two-flag" , false , "doc" )
set . String ( "three-flag" , "hello world" , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
ctx := NewContext ( nil , set , parentCtx )
_ = set . Parse ( [ ] string { "--one-flag" , "--two-flag" , "--three-flag" , "frob" } )
_ = parentSet . Parse ( [ ] string { "--top-flag" } )
expect ( t , ctx . IsSet ( "one-flag" ) , true )
expect ( t , ctx . IsSet ( "two-flag" ) , true )
expect ( t , ctx . IsSet ( "three-flag" ) , true )
expect ( t , ctx . IsSet ( "top-flag" ) , true )
expect ( t , ctx . IsSet ( "bogus" ) , false )
}
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
@ -192,15 +183,15 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
unparsableIsSet , uIsSet bool
)
c learenv( )
os . C learenv( )
_ = os . Setenv ( "APP_TIMEOUT_SECONDS" , "15.5" )
_ = os . Setenv ( "APP_PASSWORD" , "" )
a := App {
Flags : [ ] Flag {
Float64Flag { Name : "timeout , t" , EnvVar : "APP_TIMEOUT_SECONDS" } ,
StringFlag { Name : "password , p" , EnvVar : "APP_PASSWORD" } ,
Float64Flag { Name : "unparsable , u" , EnvVar : "APP_UNPARSABLE" } ,
Float64Flag { Name : "no-env-var , n"} ,
& Float64Flag { Name : "timeout " , Aliases : [ ] string { " t"} , EnvVar s : [ ] string { "APP_TIMEOUT_SECONDS" } } ,
& StringFlag { Name : "password " , Aliases : [ ] string { " p"} , EnvVar s : [ ] string { "APP_PASSWORD" } } ,
& Float64Flag { Name : "unparsable " , Aliases : [ ] string { " u"} , EnvVar s : [ ] string { "APP_UNPARSABLE" } } ,
& Float64Flag { Name : "no-env-var " , Aliases : [ ] string { " n"} } ,
} ,
Action : func ( ctx * Context ) error {
timeoutIsSet = ctx . IsSet ( "timeout" )
@ -228,94 +219,6 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
expect ( t , uIsSet , false )
}
func TestContext_GlobalIsSet ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "myflag" , false , "doc" )
set . String ( "otherflag" , "hello world" , "doc" )
globalSet := flag . NewFlagSet ( "test" , 0 )
globalSet . Bool ( "myflagGlobal" , true , "doc" )
globalSet . Bool ( "myflagGlobalUnset" , true , "doc" )
globalCtx := NewContext ( nil , globalSet , nil )
c := NewContext ( nil , set , globalCtx )
_ = set . Parse ( [ ] string { "--myflag" , "bat" , "baz" } )
_ = globalSet . Parse ( [ ] string { "--myflagGlobal" , "bat" , "baz" } )
expect ( t , c . GlobalIsSet ( "myflag" ) , false )
expect ( t , c . GlobalIsSet ( "otherflag" ) , false )
expect ( t , c . GlobalIsSet ( "bogusflag" ) , false )
expect ( t , c . GlobalIsSet ( "myflagGlobal" ) , true )
expect ( t , c . GlobalIsSet ( "myflagGlobalUnset" ) , false )
expect ( t , c . GlobalIsSet ( "bogusGlobal" ) , false )
}
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
// Should be moved to `flag_test` in v2
func TestContext_GlobalIsSet_fromEnv ( t * testing . T ) {
var (
timeoutIsSet , tIsSet bool
noEnvVarIsSet , nIsSet bool
passwordIsSet , pIsSet bool
passwordValue string
unparsableIsSet , uIsSet bool
overrideIsSet , oIsSet bool
overrideValue string
)
clearenv ( )
_ = os . Setenv ( "APP_TIMEOUT_SECONDS" , "15.5" )
_ = os . Setenv ( "APP_PASSWORD" , "badpass" )
_ = os . Setenv ( "APP_OVERRIDE" , "overridden" )
a := App {
Flags : [ ] Flag {
Float64Flag { Name : "timeout, t" , EnvVar : "APP_TIMEOUT_SECONDS" } ,
StringFlag { Name : "password, p" , EnvVar : "APP_PASSWORD" } ,
Float64Flag { Name : "no-env-var, n" } ,
Float64Flag { Name : "unparsable, u" , EnvVar : "APP_UNPARSABLE" } ,
StringFlag { Name : "overrides-default, o" , Value : "default" , EnvVar : "APP_OVERRIDE" } ,
} ,
Commands : [ ] Command {
{
Name : "hello" ,
Action : func ( ctx * Context ) error {
timeoutIsSet = ctx . GlobalIsSet ( "timeout" )
tIsSet = ctx . GlobalIsSet ( "t" )
passwordIsSet = ctx . GlobalIsSet ( "password" )
pIsSet = ctx . GlobalIsSet ( "p" )
passwordValue = ctx . GlobalString ( "password" )
unparsableIsSet = ctx . GlobalIsSet ( "unparsable" )
uIsSet = ctx . GlobalIsSet ( "u" )
noEnvVarIsSet = ctx . GlobalIsSet ( "no-env-var" )
nIsSet = ctx . GlobalIsSet ( "n" )
overrideIsSet = ctx . GlobalIsSet ( "overrides-default" )
oIsSet = ctx . GlobalIsSet ( "o" )
overrideValue = ctx . GlobalString ( "overrides-default" )
return nil
} ,
} ,
} ,
}
if err := a . Run ( [ ] string { "run" , "hello" } ) ; err != nil {
t . Logf ( "error running Run(): %+v" , err )
}
expect ( t , timeoutIsSet , true )
expect ( t , tIsSet , true )
expect ( t , passwordIsSet , true )
expect ( t , pIsSet , true )
expect ( t , passwordValue , "badpass" )
expect ( t , unparsableIsSet , false )
expect ( t , noEnvVarIsSet , false )
expect ( t , nIsSet , false )
expect ( t , overrideIsSet , true )
expect ( t , oIsSet , true )
expect ( t , overrideValue , "overridden" )
_ = os . Setenv ( "APP_UNPARSABLE" , "foobar" )
if err := a . Run ( [ ] string { "run" } ) ; err != nil {
t . Logf ( "error running Run(): %+v" , err )
}
expect ( t , unparsableIsSet , false )
expect ( t , uIsSet , false )
}
func TestContext_NumFlags ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "myflag" , false , "doc" )
@ -329,62 +232,6 @@ func TestContext_NumFlags(t *testing.T) {
expect ( t , c . NumFlags ( ) , 2 )
}
func TestContext_GlobalFlag ( t * testing . T ) {
var globalFlag string
var globalFlagSet bool
app := NewApp ( )
app . Flags = [ ] Flag {
StringFlag { Name : "global, g" , Usage : "global" } ,
}
app . Action = func ( c * Context ) error {
globalFlag = c . GlobalString ( "global" )
globalFlagSet = c . GlobalIsSet ( "global" )
return nil
}
_ = app . Run ( [ ] string { "command" , "-g" , "foo" } )
expect ( t , globalFlag , "foo" )
expect ( t , globalFlagSet , true )
}
func TestContext_GlobalFlagsInSubcommands ( t * testing . T ) {
subcommandRun := false
parentFlag := false
app := NewApp ( )
app . Flags = [ ] Flag {
BoolFlag { Name : "debug, d" , Usage : "Enable debugging" } ,
}
app . Commands = [ ] Command {
{
Name : "foo" ,
Flags : [ ] Flag {
BoolFlag { Name : "parent, p" , Usage : "Parent flag" } ,
} ,
Subcommands : [ ] Command {
{
Name : "bar" ,
Action : func ( c * Context ) error {
if c . GlobalBool ( "debug" ) {
subcommandRun = true
}
if c . GlobalBool ( "parent" ) {
parentFlag = true
}
return nil
} ,
} ,
} ,
} ,
}
_ = app . Run ( [ ] string { "command" , "-d" , "foo" , "-p" , "bar" } )
expect ( t , subcommandRun , true )
expect ( t , parentFlag , true )
}
func TestContext_Set ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Int ( "int" , 5 , "an int" )
@ -396,25 +243,105 @@ func TestContext_Set(t *testing.T) {
expect ( t , c . IsSet ( "int" ) , true )
}
func TestContext_GlobalSet ( t * testing . T ) {
gSet := flag . NewFlagSet ( "test" , 0 )
gSet . Int ( "int" , 5 , "an int" )
func TestContext_LocalFlagNames ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "one-flag" , false , "doc" )
set . String ( "two-flag" , "hello world" , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
ctx := NewContext ( nil , set , parentCtx )
_ = set . Parse ( [ ] string { "--one-flag" , "--two-flag=foo" } )
_ = parentSet . Parse ( [ ] string { "--top-flag" } )
actualFlags := ctx . LocalFlagNames ( )
sort . Strings ( actualFlags )
expect ( t , actualFlags , [ ] string { "one-flag" , "two-flag" } )
}
set := flag . NewFlagSet ( "sub" , 0 )
set . Int ( "int" , 3 , "an int" )
func TestContext_FlagNames ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "one-flag" , false , "doc" )
set . String ( "two-flag" , "hello world" , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
ctx := NewContext ( nil , set , parentCtx )
_ = set . Parse ( [ ] string { "--one-flag" , "--two-flag=foo" } )
_ = parentSet . Parse ( [ ] string { "--top-flag" } )
actualFlags := ctx . FlagNames ( )
sort . Strings ( actualFlags )
expect ( t , actualFlags , [ ] string { "one-flag" , "top-flag" , "two-flag" } )
}
pc := NewContext ( nil , gSet , nil )
c := NewContext ( nil , set , pc )
func TestContext_Lineage ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "local-flag" , false , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
ctx := NewContext ( nil , set , parentCtx )
_ = set . Parse ( [ ] string { "--local-flag" } )
_ = parentSet . Parse ( [ ] string { "--top-flag" } )
lineage := ctx . Lineage ( )
expect ( t , len ( lineage ) , 2 )
expect ( t , lineage [ 0 ] , ctx )
expect ( t , lineage [ 1 ] , parentCtx )
}
_ = c . Set ( "int" , "1" )
expect ( t , c . Int ( "int" ) , 1 )
expect ( t , c . GlobalInt ( "int" ) , 5 )
func TestContext_lookupFlagSet ( t * testing . T ) {
set := flag . NewFlagSet ( "test" , 0 )
set . Bool ( "local-flag" , false , "doc" )
parentSet := flag . NewFlagSet ( "test" , 0 )
parentSet . Bool ( "top-flag" , true , "doc" )
parentCtx := NewContext ( nil , parentSet , nil )
ctx := NewContext ( nil , set , parentCtx )
_ = set . Parse ( [ ] string { "--local-flag" } )
_ = parentSet . Parse ( [ ] string { "--top-flag" } )
fs := lookupFlagSet ( "top-flag" , ctx )
expect ( t , fs , parentCtx . flagSet )
fs = lookupFlagSet ( "local-flag" , ctx )
expect ( t , fs , ctx . flagSet )
if fs := lookupFlagSet ( "frob" , ctx ) ; fs != nil {
t . Fail ( )
}
}
expect ( t , c . GlobalIsSet ( "int" ) , false )
_ = c . GlobalSet ( "int" , "1" )
expect ( t , c . Int ( "int" ) , 1 )
expect ( t , c . GlobalInt ( "int" ) , 1 )
expect ( t , c . GlobalIsSet ( "int" ) , true )
func TestNonNilContext ( t * testing . T ) {
ctx := NewContext ( nil , nil , nil )
if ctx . Context == nil {
t . Fatal ( "expected a non nil context when no parent is present" )
}
}
// TestContextPropagation tests that
// *cli.Context always has a valid
// context.Context
func TestContextPropagation ( t * testing . T ) {
parent := NewContext ( nil , nil , nil )
parent . Context = context . WithValue ( context . Background ( ) , "key" , "val" )
ctx := NewContext ( nil , nil , parent )
val := ctx . Value ( "key" )
if val == nil {
t . Fatal ( "expected a parent context to be inherited but got nil" )
}
valstr , _ := val . ( string )
if valstr != "val" {
t . Fatalf ( "expected the context value to be %q but got %q" , "val" , valstr )
}
parent = NewContext ( nil , nil , nil )
parent . Context = nil
ctx = NewContext ( nil , nil , parent )
if ctx . Context == nil {
t . Fatal ( "expected context to not be nil even if the parent's context is nil" )
}
}
func TestCheckRequiredFlags ( t * testing . T ) {
@ -432,13 +359,13 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "optional" ,
flags : [ ] Flag {
StringFlag { Name : "optionalFlag" } ,
& StringFlag { Name : "optionalFlag" } ,
} ,
} ,
{
testCase : "required" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
} ,
expectedAnError : true ,
expectedErrorContents : [ ] string { "requiredFlag" } ,
@ -446,30 +373,30 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "required_and_present" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
} ,
parseInput : [ ] string { "--requiredFlag" , "myinput" } ,
} ,
{
testCase : "required_and_present_via_env_var" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true , EnvVar : "REQUIRED_FLAG" } ,
& StringFlag { Name : "requiredFlag" , Required : true , EnvVar s : [ ] string { "REQUIRED_FLAG" } } ,
} ,
envVarInput : [ 2 ] string { "REQUIRED_FLAG" , "true" } ,
} ,
{
testCase : "required_and_optional" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "optionalFlag" } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "optionalFlag" } ,
} ,
expectedAnError : true ,
} ,
{
testCase : "required_and_optional_and_optional_present" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "optionalFlag" } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "optionalFlag" } ,
} ,
parseInput : [ ] string { "--optionalFlag" , "myinput" } ,
expectedAnError : true ,
@ -477,8 +404,8 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "required_and_optional_and_optional_present_via_env_var" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "optionalFlag" , EnvVar : "OPTIONAL_FLAG" } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "optionalFlag" , EnvVar s : [ ] string { "OPTIONAL_FLAG" } } ,
} ,
envVarInput : [ 2 ] string { "OPTIONAL_FLAG" , "true" } ,
expectedAnError : true ,
@ -486,16 +413,16 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "required_and_optional_and_required_present" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "optionalFlag" } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "optionalFlag" } ,
} ,
parseInput : [ ] string { "--requiredFlag" , "myinput" } ,
} ,
{
testCase : "two_required" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlagOne" , Required : true } ,
StringFlag { Name : "requiredFlagTwo" , Required : true } ,
& StringFlag { Name : "requiredFlagOne" , Required : true } ,
& StringFlag { Name : "requiredFlagTwo" , Required : true } ,
} ,
expectedAnError : true ,
expectedErrorContents : [ ] string { "requiredFlagOne" , "requiredFlagTwo" } ,
@ -503,8 +430,8 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "two_required_and_one_present" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "requiredFlagTwo" , Required : true } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "requiredFlagTwo" , Required : true } ,
} ,
parseInput : [ ] string { "--requiredFlag" , "myinput" } ,
expectedAnError : true ,
@ -512,44 +439,47 @@ func TestCheckRequiredFlags(t *testing.T) {
{
testCase : "two_required_and_both_present" ,
flags : [ ] Flag {
StringFlag { Name : "requiredFlag" , Required : true } ,
StringFlag { Name : "requiredFlagTwo" , Required : true } ,
& StringFlag { Name : "requiredFlag" , Required : true } ,
& StringFlag { Name : "requiredFlagTwo" , Required : true } ,
} ,
parseInput : [ ] string { "--requiredFlag" , "myinput" , "--requiredFlagTwo" , "myinput" } ,
} ,
{
testCase : "required_flag_with_short_name" ,
flags : [ ] Flag {
StringSliceFlag { Name : "names , N", Required : true } ,
& StringSliceFlag { Name : "names " , Aliases : [ ] string { " N"} , Required : true } ,
} ,
parseInput : [ ] string { "-N" , "asd" , "-N" , "qwe" } ,
} ,
{
testCase : "required_flag_with_multiple_short_names" ,
flags : [ ] Flag {
StringSliceFlag { Name : "names , N, n", Required : true } ,
& StringSliceFlag { Name : "names " , Aliases : [ ] string { " N" , " n"} , Required : true } ,
} ,
parseInput : [ ] string { "-n" , "asd" , "-n" , "qwe" } ,
} ,
}
for _ , test := range tdata {
t . Run ( test . testCase , func ( t * testing . T ) {
// setup
set := flag . NewFlagSet ( "test" , 0 )
for _ , flags := range test . flags {
flags . Apply ( set )
}
_ = set . Parse ( test . parseInput )
if test . envVarInput [ 0 ] != "" {
os . Clearenv ( )
_ = os . Setenv ( test . envVarInput [ 0 ] , test . envVarInput [ 1 ] )
}
ctx := & Context { }
context := NewContext ( ctx . App , set , ctx )
context . Command . Flags = test . flags
set := flag . NewFlagSet ( "test" , 0 )
for _ , flags := range test . flags {
_ = flags . Apply ( set )
}
_ = set . Parse ( test . parseInput )
c := & Context { }
ctx := NewContext ( c . App , set , c )
ctx . Command . Flags = test . flags
// logic under test
err := checkRequiredFlags ( test . flags , context )
err := checkRequiredFlags ( test . flags , c tx)
// assertions
if test . expectedAnError && err == nil {
@ -559,10 +489,12 @@ func TestCheckRequiredFlags(t *testing.T) {
t . Errorf ( "did not expected an error, but there was one: %s" , err )
}
for _ , errString := range test . expectedErrorContents {
if err != nil {
if ! strings . Contains ( err . Error ( ) , errString ) {
t . Errorf ( "expected error %q to contain %q, but it didn't!" , err . Error ( ) , errString )
}
}
}
} )
}
}