2015-07-20 19:18:25 +00:00
package cli
2013-09-09 18:51:46 +00:00
import (
2016-05-24 02:00:59 +00:00
"flag"
2014-04-15 14:16:47 +00:00
"fmt"
2014-07-11 22:13:10 +00:00
"os"
2014-03-19 02:26:57 +00:00
"reflect"
2017-03-04 22:28:24 +00:00
"regexp"
2016-04-05 16:35:30 +00:00
"runtime"
2014-04-15 14:16:47 +00:00
"strings"
2013-09-09 18:51:46 +00:00
"testing"
2016-05-02 17:05:21 +00:00
"time"
2013-09-09 18:51:46 +00:00
)
var boolFlagTests = [ ] struct {
name string
expected string
} {
2016-05-22 00:02:08 +00:00
{ "help" , "--help\t(default: false)" } ,
{ "h" , "-h\t(default: false)" } ,
2013-09-09 18:51:46 +00:00
}
func TestBoolFlagHelpOutput ( t * testing . T ) {
for _ , test := range boolFlagTests {
2016-05-22 19:20:52 +00:00
flag := & BoolFlag { Name : test . name }
2013-09-09 18:51:46 +00:00
output := flag . String ( )
if output != test . expected {
2016-05-02 17:05:21 +00:00
t . Errorf ( "%q does not match %q" , output , test . expected )
2013-09-09 18:51:46 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestBoolFlagApply_SetsAllNames ( t * testing . T ) {
v := false
fl := BoolFlag { Name : "wat" , Aliases : [ ] string { "W" , "huh" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--wat" , "-W" , "--huh" } )
expect ( t , err , nil )
expect ( t , v , true )
}
2016-09-17 23:54:29 +00:00
func TestFlagsFromEnv ( t * testing . T ) {
2017-08-13 02:02:54 +00:00
newSetIntSlice := func ( defaults ... int ) IntSlice {
s := NewIntSlice ( defaults ... )
s . hasBeenSet = true
return * s
}
newSetInt64Slice := func ( defaults ... int64 ) Int64Slice {
s := NewInt64Slice ( defaults ... )
s . hasBeenSet = true
return * s
}
newSetStringSlice := func ( defaults ... string ) StringSlice {
s := NewStringSlice ( defaults ... )
s . hasBeenSet = true
return * s
}
2016-09-17 23:54:29 +00:00
var flagTests = [ ] struct {
2017-03-04 22:28:24 +00:00
input string
output interface { }
flag Flag
errRegexp string
2016-09-12 03:32:43 +00:00
} {
2017-08-04 16:00:22 +00:00
{ "" , false , & BoolFlag { Name : "debug" , EnvVars : [ ] string { "DEBUG" } } , "" } ,
{ "1" , true , & BoolFlag { Name : "debug" , EnvVars : [ ] string { "DEBUG" } } , "" } ,
{ "false" , false , & BoolFlag { Name : "debug" , EnvVars : [ ] string { "DEBUG" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "foobar" , true , & BoolFlag { Name : "debug" , EnvVars : [ ] string { "DEBUG" } } , ` could not parse "foobar" as bool value for flag debug: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1s" , 1 * time . Second , & DurationFlag { Name : "time" , EnvVars : [ ] string { "TIME" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "foobar" , false , & DurationFlag { Name : "time" , EnvVars : [ ] string { "TIME" } } , ` could not parse "foobar" as duration for flag time: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1.2" , 1.2 , & Float64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
{ "1" , 1.0 , & Float64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "foobar" , 0 , & Float64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as float64 value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1" , int64 ( 1 ) , & Int64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "1.2" , 0 , & Int64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2" as int value for flag seconds: .* ` } ,
{ "foobar" , 0 , & Int64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as int value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1" , 1 , & IntFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "1.2" , 0 , & IntFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2" as int value for flag seconds: .* ` } ,
{ "foobar" , 0 , & IntFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as int value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-13 02:02:54 +00:00
{ "1,2" , newSetIntSlice ( 1 , 2 ) , & IntSliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
{ "1.2,2" , newSetIntSlice ( ) , & IntSliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2,2" as int slice value for flag seconds: .* ` } ,
{ "foobar" , newSetIntSlice ( ) , & IntSliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as int slice value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-13 02:02:54 +00:00
{ "1,2" , newSetInt64Slice ( 1 , 2 ) , & Int64SliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
{ "1.2,2" , newSetInt64Slice ( ) , & Int64SliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2,2" as int64 slice value for flag seconds: .* ` } ,
{ "foobar" , newSetInt64Slice ( ) , & Int64SliceFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as int64 slice value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "foo" , "foo" , & StringFlag { Name : "name" , EnvVars : [ ] string { "NAME" } } , "" } ,
2017-09-26 12:38:41 +00:00
{ "path" , "path" , & PathFlag { Name : "path" , EnvVars : [ ] string { "PATH" } } , "" } ,
2016-09-17 23:54:29 +00:00
2017-08-13 02:02:54 +00:00
{ "foo,bar" , newSetStringSlice ( "foo" , "bar" ) , & StringSliceFlag { Name : "names" , EnvVars : [ ] string { "NAMES" } } , "" } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1" , uint ( 1 ) , & UintFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "1.2" , 0 , & UintFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2" as uint value for flag seconds: .* ` } ,
{ "foobar" , 0 , & UintFlag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as uint value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "1" , uint64 ( 1 ) , & Uint64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , "" } ,
2017-08-13 02:02:54 +00:00
{ "1.2" , 0 , & Uint64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "1.2" as uint64 value for flag seconds: .* ` } ,
{ "foobar" , 0 , & Uint64Flag { Name : "seconds" , EnvVars : [ ] string { "SECONDS" } } , ` could not parse "foobar" as uint64 value for flag seconds: .* ` } ,
2016-09-17 23:54:29 +00:00
2017-08-04 16:00:22 +00:00
{ "foo,bar" , & Parser { "foo" , "bar" } , & GenericFlag { Name : "names" , Value : & Parser { } , EnvVars : [ ] string { "NAMES" } } , "" } ,
2016-09-12 03:32:43 +00:00
}
2017-08-13 02:02:54 +00:00
for i , test := range flagTests {
2017-08-04 15:59:27 +00:00
clearenv ( )
2017-08-13 02:02:54 +00:00
envVarSlice := reflect . Indirect ( reflect . ValueOf ( test . flag ) ) . FieldByName ( "EnvVars" ) . Slice ( 0 , 1 )
os . Setenv ( envVarSlice . Index ( 0 ) . String ( ) , test . input )
2016-09-12 03:32:43 +00:00
a := App {
2016-09-17 23:54:29 +00:00
Flags : [ ] Flag { test . flag } ,
2016-09-12 03:32:43 +00:00
Action : func ( ctx * Context ) error {
2017-08-04 16:00:22 +00:00
if ! reflect . DeepEqual ( ctx . value ( test . flag . Names ( ) [ 0 ] ) , test . output ) {
2017-08-13 02:02:54 +00:00
t . Errorf ( "ex:%01d expected %q to be parsed as %#v, instead was %#v" , i , test . input , test . output , ctx . value ( test . flag . Names ( ) [ 0 ] ) )
2016-09-12 03:32:43 +00:00
}
return nil
} ,
}
2016-09-17 23:54:29 +00:00
err := a . Run ( [ ] string { "run" } )
2017-03-04 22:28:24 +00:00
if test . errRegexp != "" {
if err == nil {
2017-08-13 02:02:54 +00:00
t . Errorf ( "expected error to match %q, got none" , test . errRegexp )
2017-03-04 22:28:24 +00:00
} else {
if matched , _ := regexp . MatchString ( test . errRegexp , err . Error ( ) ) ; ! matched {
2017-08-13 02:02:54 +00:00
t . Errorf ( "expected error to match %q, got error %s" , test . errRegexp , err )
2017-03-04 22:28:24 +00:00
}
}
} else {
if err != nil && test . errRegexp == "" {
2017-08-13 02:02:54 +00:00
t . Errorf ( "expected no error got %q" , err )
2017-03-04 22:28:24 +00:00
}
2016-09-17 23:54:29 +00:00
}
2016-09-12 03:32:43 +00:00
}
}
2013-09-09 18:51:46 +00:00
var stringFlagTests = [ ] struct {
name string
2016-05-22 01:29:45 +00:00
aliases [ ] string
2016-04-22 03:02:06 +00:00
usage string
2014-04-11 19:09:59 +00:00
value string
2013-09-09 18:51:46 +00:00
expected string
} {
2016-05-22 01:29:45 +00:00
{ "foo" , nil , "" , "" , "--foo value\t" } ,
{ "f" , nil , "" , "" , "-f value\t" } ,
{ "f" , nil , "The total `foo` desired" , "all" , "-f foo\tThe total foo desired (default: \"all\")" } ,
{ "test" , nil , "" , "Something" , "--test value\t(default: \"Something\")" } ,
{ "config" , [ ] string { "c" } , "Load configuration from `FILE`" , "" , "--config FILE, -c FILE\tLoad configuration from FILE" } ,
{ "config" , [ ] string { "c" } , "Load configuration from `CONFIG`" , "config.json" , "--config CONFIG, -c CONFIG\tLoad configuration from CONFIG (default: \"config.json\")" } ,
2013-09-09 18:51:46 +00:00
}
func TestStringFlagHelpOutput ( t * testing . T ) {
for _ , test := range stringFlagTests {
2016-05-22 19:20:52 +00:00
flag := & StringFlag { Name : test . name , Aliases : test . aliases , Usage : test . usage , Value : test . value }
2013-09-09 18:51:46 +00:00
output := flag . String ( )
if output != test . expected {
2016-05-02 17:05:21 +00:00
t . Errorf ( "%q does not match %q" , output , test . expected )
2013-09-09 18:51:46 +00:00
}
}
}
2016-08-28 07:14:39 +00:00
func TestStringFlagDefaultText ( t * testing . T ) {
flag := & StringFlag { Name : "foo" , Aliases : nil , Usage : "amount of `foo` requested" , Value : "none" , DefaultText : "all of it" }
2016-08-30 14:52:24 +00:00
expected := "--foo foo\tamount of foo requested (default: all of it)"
2016-08-28 07:14:39 +00:00
output := flag . String ( )
if output != expected {
t . Errorf ( "%q does not match %q" , output , expected )
}
}
2014-07-11 22:13:10 +00:00
func TestStringFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_FOO" , "derp" )
for _ , test := range stringFlagTests {
2016-05-22 19:20:52 +00:00
flag := & StringFlag { Name : test . name , Aliases : test . aliases , Value : test . value , EnvVars : [ ] string { "APP_FOO" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_FOO]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_FOO%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%s does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestStringFlagApply_SetsAllNames ( t * testing . T ) {
v := "mmm"
fl := StringFlag { Name : "hay" , Aliases : [ ] string { "H" , "hayyy" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--hay" , "u" , "-H" , "yuu" , "--hayyy" , "YUUUU" } )
expect ( t , err , nil )
expect ( t , v , "YUUUU" )
}
2017-09-26 12:38:41 +00:00
var pathFlagTests = [ ] struct {
name string
aliases [ ] string
usage string
value string
expected string
} {
{ "f" , nil , "" , "" , "-f value\t" } ,
{ "f" , nil , "Path is the `path` of file" , "/path/to/file" , "-f path\tPath is the path of file (default: \"/path/to/file\")" } ,
}
func TestPathFlagHelpOutput ( t * testing . T ) {
for _ , test := range pathFlagTests {
flag := & PathFlag { Name : test . name , Aliases : test . aliases , Usage : test . usage , Value : test . value }
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestPathFlagWithEnvVarHelpOutput ( t * testing . T ) {
clearenv ( )
os . Setenv ( "APP_PATH" , "/path/to/file" )
for _ , test := range pathFlagTests {
flag := & PathFlag { Name : test . name , Aliases : test . aliases , Value : test . value , EnvVars : [ ] string { "APP_PATH" } }
output := flag . String ( )
expectedSuffix := " [$APP_PATH]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_PATH%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%s does not end with" + expectedSuffix , output )
}
}
}
func TestPathFlagApply_SetsAllNames ( t * testing . T ) {
v := "mmm"
fl := PathFlag { Name : "path" , Aliases : [ ] string { "p" , "PATH" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--path" , "/path/to/file/path" , "-p" , "/path/to/file/p" , "--PATH" , "/path/to/file/PATH" } )
expect ( t , err , nil )
expect ( t , v , "/path/to/file/PATH" )
}
2014-07-11 22:13:10 +00:00
var stringSliceFlagTests = [ ] struct {
name string
2016-05-22 01:29:45 +00:00
aliases [ ] string
2015-07-20 19:18:25 +00:00
value * StringSlice
2014-07-11 22:13:10 +00:00
expected string
} {
2016-05-22 01:29:45 +00:00
{ "foo" , nil , NewStringSlice ( "" ) , "--foo value\t" } ,
{ "f" , nil , NewStringSlice ( "" ) , "-f value\t" } ,
{ "f" , nil , NewStringSlice ( "Lipstick" ) , "-f value\t(default: \"Lipstick\")" } ,
{ "test" , nil , NewStringSlice ( "Something" ) , "--test value\t(default: \"Something\")" } ,
{ "dee" , [ ] string { "d" } , NewStringSlice ( "Inka" , "Dinka" , "dooo" ) , "--dee value, -d value\t(default: \"Inka\", \"Dinka\", \"dooo\")" } ,
2014-07-11 22:13:10 +00:00
}
func TestStringSliceFlagHelpOutput ( t * testing . T ) {
for _ , test := range stringSliceFlagTests {
2016-05-22 19:20:52 +00:00
flag := & StringSliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestStringSliceFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_QWWX" , "11,4" )
for _ , test := range stringSliceFlagTests {
2016-05-22 19:20:52 +00:00
flag := & StringSliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value , EnvVars : [ ] string { "APP_QWWX" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_QWWX]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_QWWX%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%q does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestStringSliceFlagApply_SetsAllNames ( t * testing . T ) {
fl := StringSliceFlag { Name : "goat" , Aliases : [ ] string { "G" , "gooots" } }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--goat" , "aaa" , "-G" , "bbb" , "--gooots" , "eeeee" } )
expect ( t , err , nil )
}
2013-09-09 18:51:46 +00:00
var intFlagTests = [ ] struct {
name string
expected string
} {
2016-05-02 23:42:08 +00:00
{ "hats" , "--hats value\t(default: 9)" } ,
{ "H" , "-H value\t(default: 9)" } ,
2013-09-09 18:51:46 +00:00
}
func TestIntFlagHelpOutput ( t * testing . T ) {
for _ , test := range intFlagTests {
2016-05-22 19:20:52 +00:00
flag := & IntFlag { Name : test . name , Value : 9 }
2013-09-09 18:51:46 +00:00
output := flag . String ( )
2013-12-03 13:42:09 +00:00
if output != test . expected {
t . Errorf ( "%s does not match %s" , output , test . expected )
}
}
}
2014-07-11 22:13:10 +00:00
func TestIntFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_BAR" , "2" )
for _ , test := range intFlagTests {
2016-05-22 19:20:52 +00:00
flag := & IntFlag { Name : test . name , EnvVars : [ ] string { "APP_BAR" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_BAR]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_BAR%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%s does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestIntFlagApply_SetsAllNames ( t * testing . T ) {
v := 3
fl := IntFlag { Name : "banana" , Aliases : [ ] string { "B" , "banannanana" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--banana" , "1" , "-B" , "2" , "--banannanana" , "5" } )
expect ( t , err , nil )
expect ( t , v , 5 )
}
2016-06-11 22:22:07 +00:00
var int64FlagTests = [ ] struct {
name string
expected string
} {
{ "hats" , "--hats value\t(default: 8589934592)" } ,
{ "H" , "-H value\t(default: 8589934592)" } ,
}
func TestInt64FlagHelpOutput ( t * testing . T ) {
for _ , test := range int64FlagTests {
flag := Int64Flag { Name : test . name , Value : 8589934592 }
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%s does not match %s" , output , test . expected )
}
}
}
func TestInt64FlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-11 22:22:07 +00:00
os . Setenv ( "APP_BAR" , "2" )
for _ , test := range int64FlagTests {
2016-06-17 13:54:54 +00:00
flag := IntFlag { Name : test . name , EnvVars : [ ] string { "APP_BAR" } }
2016-06-11 22:22:07 +00:00
output := flag . String ( )
expectedSuffix := " [$APP_BAR]"
if runtime . GOOS == "windows" {
2016-06-16 15:13:32 +00:00
expectedSuffix = " [%APP_BAR%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%s does not end with" + expectedSuffix , output )
}
}
}
var uintFlagTests = [ ] struct {
name string
expected string
} {
{ "nerfs" , "--nerfs value\t(default: 41)" } ,
{ "N" , "-N value\t(default: 41)" } ,
}
func TestUintFlagHelpOutput ( t * testing . T ) {
for _ , test := range uintFlagTests {
flag := UintFlag { Name : test . name , Value : 41 }
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%s does not match %s" , output , test . expected )
}
}
}
func TestUintFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-16 15:13:32 +00:00
os . Setenv ( "APP_BAR" , "2" )
for _ , test := range uintFlagTests {
2016-06-17 13:54:54 +00:00
flag := UintFlag { Name : test . name , EnvVars : [ ] string { "APP_BAR" } }
2016-06-16 15:13:32 +00:00
output := flag . String ( )
expectedSuffix := " [$APP_BAR]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_BAR%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%s does not end with" + expectedSuffix , output )
}
}
}
var uint64FlagTests = [ ] struct {
name string
expected string
} {
{ "gerfs" , "--gerfs value\t(default: 8589934582)" } ,
{ "G" , "-G value\t(default: 8589934582)" } ,
}
func TestUint64FlagHelpOutput ( t * testing . T ) {
for _ , test := range uint64FlagTests {
flag := Uint64Flag { Name : test . name , Value : 8589934582 }
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%s does not match %s" , output , test . expected )
}
}
}
func TestUint64FlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-16 15:13:32 +00:00
os . Setenv ( "APP_BAR" , "2" )
for _ , test := range uint64FlagTests {
2016-06-17 13:54:54 +00:00
flag := UintFlag { Name : test . name , EnvVars : [ ] string { "APP_BAR" } }
2016-06-16 15:13:32 +00:00
output := flag . String ( )
expectedSuffix := " [$APP_BAR]"
if runtime . GOOS == "windows" {
2016-06-11 22:22:07 +00:00
expectedSuffix = " [%APP_BAR%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%s does not end with" + expectedSuffix , output )
}
}
}
2014-08-02 21:32:32 +00:00
var durationFlagTests = [ ] struct {
name string
expected string
} {
2016-05-02 23:42:08 +00:00
{ "hooting" , "--hooting value\t(default: 1s)" } ,
{ "H" , "-H value\t(default: 1s)" } ,
2014-08-02 21:32:32 +00:00
}
func TestDurationFlagHelpOutput ( t * testing . T ) {
for _ , test := range durationFlagTests {
2016-05-22 19:20:52 +00:00
flag := & DurationFlag { Name : test . name , Value : 1 * time . Second }
2014-08-02 21:32:32 +00:00
output := flag . String ( )
if output != test . expected {
2016-05-02 17:05:21 +00:00
t . Errorf ( "%q does not match %q" , output , test . expected )
2014-08-02 21:32:32 +00:00
}
}
}
func TestDurationFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-08-02 21:32:32 +00:00
os . Setenv ( "APP_BAR" , "2h3m6s" )
for _ , test := range durationFlagTests {
2016-05-22 19:20:52 +00:00
flag := & DurationFlag { Name : test . name , EnvVars : [ ] string { "APP_BAR" } }
2014-08-02 21:32:32 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_BAR]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_BAR%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%s does not end with" + expectedSuffix , output )
2014-08-02 21:32:32 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestDurationFlagApply_SetsAllNames ( t * testing . T ) {
v := time . Second * 20
fl := DurationFlag { Name : "howmuch" , Aliases : [ ] string { "H" , "whyyy" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--howmuch" , "30s" , "-H" , "5m" , "--whyyy" , "30h" } )
expect ( t , err , nil )
expect ( t , v , time . Hour * 30 )
}
2014-07-11 22:13:10 +00:00
var intSliceFlagTests = [ ] struct {
name string
2016-05-22 01:29:45 +00:00
aliases [ ] string
2015-07-20 19:18:25 +00:00
value * IntSlice
2014-07-11 22:13:10 +00:00
expected string
} {
2016-05-22 01:29:45 +00:00
{ "heads" , nil , NewIntSlice ( ) , "--heads value\t" } ,
{ "H" , nil , NewIntSlice ( ) , "-H value\t" } ,
{ "H" , [ ] string { "heads" } , NewIntSlice ( 9 , 3 ) , "-H value, --heads value\t(default: 9, 3)" } ,
2014-07-11 22:13:10 +00:00
}
func TestIntSliceFlagHelpOutput ( t * testing . T ) {
for _ , test := range intSliceFlagTests {
2016-05-22 19:20:52 +00:00
flag := & IntSliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestIntSliceFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_SMURF" , "42,3" )
for _ , test := range intSliceFlagTests {
2016-05-22 19:20:52 +00:00
flag := & IntSliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value , EnvVars : [ ] string { "APP_SMURF" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_SMURF]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_SMURF%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%q does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestIntSliceFlagApply_SetsAllNames ( t * testing . T ) {
fl := IntSliceFlag { Name : "bits" , Aliases : [ ] string { "B" , "bips" } }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--bits" , "23" , "-B" , "3" , "--bips" , "99" } )
expect ( t , err , nil )
}
2016-06-12 04:54:33 +00:00
var int64SliceFlagTests = [ ] struct {
name string
2016-06-17 13:54:54 +00:00
aliases [ ] string
2016-06-12 04:54:33 +00:00
value * Int64Slice
expected string
} {
2016-06-17 13:54:54 +00:00
{ "heads" , nil , NewInt64Slice ( ) , "--heads value\t" } ,
{ "H" , nil , NewInt64Slice ( ) , "-H value\t" } ,
{ "heads" , [ ] string { "H" } , NewInt64Slice ( int64 ( 2 ) , int64 ( 17179869184 ) ) ,
"--heads value, -H value\t(default: 2, 17179869184)" } ,
2016-06-12 04:54:33 +00:00
}
func TestInt64SliceFlagHelpOutput ( t * testing . T ) {
for _ , test := range int64SliceFlagTests {
2016-06-17 13:54:54 +00:00
flag := Int64SliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value }
2016-06-12 04:54:33 +00:00
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestInt64SliceFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-12 04:54:33 +00:00
os . Setenv ( "APP_SMURF" , "42,17179869184" )
for _ , test := range int64SliceFlagTests {
2016-06-17 13:54:54 +00:00
flag := Int64SliceFlag { Name : test . name , Value : test . value , EnvVars : [ ] string { "APP_SMURF" } }
2016-06-12 04:54:33 +00:00
output := flag . String ( )
expectedSuffix := " [$APP_SMURF]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_SMURF%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%q does not end with" + expectedSuffix , output )
}
}
}
2013-12-03 13:42:09 +00:00
var float64FlagTests = [ ] struct {
name string
expected string
} {
2016-05-02 23:42:08 +00:00
{ "hooting" , "--hooting value\t(default: 0.1)" } ,
{ "H" , "-H value\t(default: 0.1)" } ,
2013-12-03 13:42:09 +00:00
}
func TestFloat64FlagHelpOutput ( t * testing . T ) {
for _ , test := range float64FlagTests {
2016-05-22 19:20:52 +00:00
flag := & Float64Flag { Name : test . name , Value : float64 ( 0.1 ) }
2013-12-03 13:42:09 +00:00
output := flag . String ( )
2013-09-09 18:51:46 +00:00
if output != test . expected {
2016-05-02 17:05:21 +00:00
t . Errorf ( "%q does not match %q" , output , test . expected )
2013-09-09 18:51:46 +00:00
}
}
2013-11-01 14:31:37 +00:00
}
2013-11-18 23:35:23 +00:00
2014-07-11 22:13:10 +00:00
func TestFloat64FlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_BAZ" , "99.4" )
for _ , test := range float64FlagTests {
2016-05-22 19:20:52 +00:00
flag := & Float64Flag { Name : test . name , EnvVars : [ ] string { "APP_BAZ" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_BAZ]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_BAZ%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%s does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestFloat64FlagApply_SetsAllNames ( t * testing . T ) {
v := float64 ( 99.1 )
fl := Float64Flag { Name : "noodles" , Aliases : [ ] string { "N" , "nurbles" } , Destination : & v }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--noodles" , "1.3" , "-N" , "11" , "--nurbles" , "43.33333" } )
expect ( t , err , nil )
expect ( t , v , float64 ( 43.33333 ) )
}
2016-06-29 02:52:25 +00:00
var float64SliceFlagTests = [ ] struct {
name string
aliases [ ] string
value * Float64Slice
expected string
} {
{ "heads" , nil , NewFloat64Slice ( ) , "--heads value\t" } ,
{ "H" , nil , NewFloat64Slice ( ) , "-H value\t" } ,
{ "heads" , [ ] string { "H" } , NewFloat64Slice ( float64 ( 0.1234 ) , float64 ( - 10.5 ) ) ,
"--heads value, -H value\t(default: 0.1234, -10.5)" } ,
}
func TestFloat64SliceFlagHelpOutput ( t * testing . T ) {
for _ , test := range float64SliceFlagTests {
flag := Float64SliceFlag { Name : test . name , Aliases : test . aliases , Value : test . value }
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestFloat64SliceFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-29 02:52:25 +00:00
os . Setenv ( "APP_SMURF" , "0.1234,-10.5" )
for _ , test := range float64SliceFlagTests {
flag := Float64SliceFlag { Name : test . name , Value : test . value , EnvVars : [ ] string { "APP_SMURF" } }
output := flag . String ( )
expectedSuffix := " [$APP_SMURF]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_SMURF%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
t . Errorf ( "%q does not end with" + expectedSuffix , output )
}
}
}
2014-07-11 22:13:10 +00:00
var genericFlagTests = [ ] struct {
name string
2015-07-20 19:18:25 +00:00
value Generic
2014-07-11 22:13:10 +00:00
expected string
} {
2016-05-02 23:42:08 +00:00
{ "toads" , & Parser { "abc" , "def" } , "--toads value\ttest flag (default: abc,def)" } ,
{ "t" , & Parser { "abc" , "def" } , "-t value\ttest flag (default: abc,def)" } ,
2014-07-11 22:13:10 +00:00
}
func TestGenericFlagHelpOutput ( t * testing . T ) {
for _ , test := range genericFlagTests {
2016-05-22 19:20:52 +00:00
flag := & GenericFlag { Name : test . name , Value : test . value , Usage : "test flag" }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
if output != test . expected {
t . Errorf ( "%q does not match %q" , output , test . expected )
}
}
}
func TestGenericFlagWithEnvVarHelpOutput ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_ZAP" , "3" )
for _ , test := range genericFlagTests {
2016-05-22 19:20:52 +00:00
flag := & GenericFlag { Name : test . name , EnvVars : [ ] string { "APP_ZAP" } }
2014-07-11 22:13:10 +00:00
output := flag . String ( )
2016-01-22 14:08:27 +00:00
expectedSuffix := " [$APP_ZAP]"
if runtime . GOOS == "windows" {
expectedSuffix = " [%APP_ZAP%]"
}
if ! strings . HasSuffix ( output , expectedSuffix ) {
2016-04-05 16:35:30 +00:00
t . Errorf ( "%s does not end with" + expectedSuffix , output )
2014-07-11 22:13:10 +00:00
}
}
}
2016-05-24 02:00:59 +00:00
func TestGenericFlagApply_SetsAllNames ( t * testing . T ) {
fl := GenericFlag { Name : "orbs" , Aliases : [ ] string { "O" , "obrs" } , Value : & Parser { } }
set := flag . NewFlagSet ( "test" , 0 )
fl . Apply ( set )
err := set . Parse ( [ ] string { "--orbs" , "eleventy,3" , "-O" , "4,bloop" , "--obrs" , "19,s" } )
expect ( t , err , nil )
}
2013-11-18 23:35:23 +00:00
func TestParseMultiString ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringFlag { Name : "serve" , Aliases : [ ] string { "s" } } ,
2013-11-18 23:35:23 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2013-11-18 23:35:23 +00:00
if ctx . String ( "serve" ) != "10" {
t . Errorf ( "main name not set" )
}
if ctx . String ( "s" ) != "10" {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2013-11-18 23:35:23 +00:00
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" } )
}
2015-11-14 21:39:38 +00:00
func TestParseDestinationString ( t * testing . T ) {
var dest string
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringFlag {
2015-11-14 21:39:38 +00:00
Name : "dest" ,
Destination : & dest ,
} ,
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2015-11-14 21:39:38 +00:00
if dest != "10" {
t . Errorf ( "expected destination String 10" )
}
2016-04-27 13:12:34 +00:00
return nil
2015-11-14 21:39:38 +00:00
} ,
}
a . Run ( [ ] string { "run" , "--dest" , "10" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiStringFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_COUNT" , "20" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringFlag { Name : "count" , Aliases : [ ] string { "c" } , EnvVars : [ ] string { "APP_COUNT" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ctx . String ( "count" ) != "20" {
t . Errorf ( "main name not set" )
}
if ctx . String ( "c" ) != "20" {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiStringFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_COUNT" , "20" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringFlag { Name : "count" , Aliases : [ ] string { "c" } , EnvVars : [ ] string { "COMPAT_COUNT" , "APP_COUNT" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ctx . String ( "count" ) != "20" {
t . Errorf ( "main name not set" )
}
if ctx . String ( "c" ) != "20" {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-03-19 02:26:57 +00:00
func TestParseMultiStringSlice ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewStringSlice ( ) } ,
2014-03-19 02:26:57 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2016-04-29 06:53:58 +00:00
expected := [ ] string { "10" , "20" }
if ! reflect . DeepEqual ( ctx . StringSlice ( "serve" ) , expected ) {
t . Errorf ( "main name not set: %v != %v" , expected , ctx . StringSlice ( "serve" ) )
2014-03-19 02:26:57 +00:00
}
2016-04-29 06:53:58 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "s" ) , expected ) {
t . Errorf ( "short name not set: %v != %v" , expected , ctx . StringSlice ( "s" ) )
2014-03-19 02:26:57 +00:00
}
2016-04-27 13:12:34 +00:00
return nil
2014-03-19 02:26:57 +00:00
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" , "-s" , "20" } )
}
2016-04-05 16:35:30 +00:00
func TestParseMultiStringSliceWithDefaults ( t * testing . T ) {
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewStringSlice ( "9" , "2" ) } ,
2016-04-05 16:35:30 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2016-04-29 06:53:58 +00:00
expected := [ ] string { "10" , "20" }
if ! reflect . DeepEqual ( ctx . StringSlice ( "serve" ) , expected ) {
t . Errorf ( "main name not set: %v != %v" , expected , ctx . StringSlice ( "serve" ) )
2016-04-05 16:35:30 +00:00
}
2016-04-29 06:53:58 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "s" ) , expected ) {
t . Errorf ( "short name not set: %v != %v" , expected , ctx . StringSlice ( "s" ) )
2016-04-05 16:35:30 +00:00
}
2016-05-17 19:11:44 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" , "-s" , "20" } )
}
func TestParseMultiStringSliceWithDefaultsUnset ( t * testing . T ) {
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewStringSlice ( "9" , "2" ) } ,
2016-04-05 16:35:30 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "serve" ) , [ ] string { "9" , "2" } ) {
t . Errorf ( "main name not set: %v" , ctx . StringSlice ( "serve" ) )
}
if ! reflect . DeepEqual ( ctx . StringSlice ( "s" ) , [ ] string { "9" , "2" } ) {
t . Errorf ( "short name not set: %v" , ctx . StringSlice ( "s" ) )
}
2016-05-17 19:11:44 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiStringSliceFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewStringSlice ( ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "intervals" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . StringSlice ( "i" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "short name not set from env" )
}
2016-05-17 19:11:44 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2016-04-05 16:35:30 +00:00
func TestParseMultiStringSliceFromEnvWithDefaults ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-04-05 16:35:30 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewStringSlice ( "1" , "2" , "5" ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
2016-04-05 16:35:30 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "intervals" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . StringSlice ( "i" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiStringSliceFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewStringSlice ( ) , EnvVars : [ ] string { "COMPAT_INTERVALS" , "APP_INTERVALS" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "intervals" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . StringSlice ( "i" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "short name not set from env" )
}
2016-05-17 19:11:44 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2016-04-05 16:35:30 +00:00
func TestParseMultiStringSliceFromEnvCascadeWithDefaults ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-04-05 16:35:30 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& StringSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewStringSlice ( "1" , "2" , "5" ) , EnvVars : [ ] string { "COMPAT_INTERVALS" , "APP_INTERVALS" } } ,
2016-04-05 16:35:30 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . StringSlice ( "intervals" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . StringSlice ( "i" ) , [ ] string { "20" , "30" , "40" } ) {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2013-11-18 23:35:23 +00:00
func TestParseMultiInt ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntFlag { Name : "serve" , Aliases : [ ] string { "s" } } ,
2013-11-18 23:35:23 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2013-11-18 23:35:23 +00:00
if ctx . Int ( "serve" ) != 10 {
t . Errorf ( "main name not set" )
}
if ctx . Int ( "s" ) != 10 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2013-11-18 23:35:23 +00:00
} ,
}
2013-11-20 08:05:18 +00:00
a . Run ( [ ] string { "run" , "-s" , "10" } )
2013-11-18 23:35:23 +00:00
}
2013-11-18 23:37:59 +00:00
2015-11-14 21:39:38 +00:00
func TestParseDestinationInt ( t * testing . T ) {
var dest int
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntFlag {
2015-11-14 21:39:38 +00:00
Name : "dest" ,
Destination : & dest ,
} ,
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2015-11-14 21:39:38 +00:00
if dest != 10 {
t . Errorf ( "expected destination Int 10" )
}
2016-04-27 13:12:34 +00:00
return nil
2015-11-14 21:39:38 +00:00
} ,
}
a . Run ( [ ] string { "run" , "--dest" , "10" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiIntFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_TIMEOUT_SECONDS" , "10" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntFlag { Name : "timeout" , Aliases : [ ] string { "t" } , EnvVars : [ ] string { "APP_TIMEOUT_SECONDS" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ctx . Int ( "timeout" ) != 10 {
t . Errorf ( "main name not set" )
}
if ctx . Int ( "t" ) != 10 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiIntFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_TIMEOUT_SECONDS" , "10" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntFlag { Name : "timeout" , Aliases : [ ] string { "t" } , EnvVars : [ ] string { "COMPAT_TIMEOUT_SECONDS" , "APP_TIMEOUT_SECONDS" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ctx . Int ( "timeout" ) != 10 {
t . Errorf ( "main name not set" )
}
if ctx . Int ( "t" ) != 10 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiIntSlice ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewIntSlice ( ) } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "serve" ) , [ ] int { 10 , 20 } ) {
t . Errorf ( "main name not set" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "s" ) , [ ] int { 10 , 20 } ) {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" , "-s" , "20" } )
}
2016-04-05 16:35:30 +00:00
func TestParseMultiIntSliceWithDefaults ( t * testing . T ) {
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewIntSlice ( 9 , 2 ) } ,
2016-04-05 16:35:30 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "serve" ) , [ ] int { 10 , 20 } ) {
t . Errorf ( "main name not set" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "s" ) , [ ] int { 10 , 20 } ) {
t . Errorf ( "short name not set" )
}
2016-05-17 19:11:44 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" , "-s" , "20" } )
}
func TestParseMultiIntSliceWithDefaultsUnset ( t * testing . T ) {
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewIntSlice ( 9 , 2 ) } ,
2016-04-05 16:35:30 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "serve" ) , [ ] int { 9 , 2 } ) {
t . Errorf ( "main name not set" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "s" ) , [ ] int { 9 , 2 } ) {
t . Errorf ( "short name not set" )
}
2016-05-17 19:11:44 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiIntSliceFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewIntSlice ( ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-05-17 19:11:44 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "intervals" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "i" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "short name not set from env" )
}
2016-05-17 19:11:44 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2016-04-05 16:35:30 +00:00
func TestParseMultiIntSliceFromEnvWithDefaults ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-04-05 16:35:30 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewIntSlice ( 1 , 2 , 5 ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
2016-04-05 16:35:30 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2016-04-05 16:35:30 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "intervals" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "i" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2016-04-05 16:35:30 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiIntSliceFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,40" )
2015-07-20 19:18:25 +00:00
( & App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& IntSliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewIntSlice ( ) , EnvVars : [ ] string { "COMPAT_INTERVALS" , "APP_INTERVALS" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ! reflect . DeepEqual ( ctx . IntSlice ( "intervals" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . IntSlice ( "i" ) , [ ] int { 20 , 30 , 40 } ) {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
} ) . Run ( [ ] string { "run" } )
}
2016-06-12 04:54:33 +00:00
func TestParseMultiInt64Slice ( t * testing . T ) {
( & App {
Flags : [ ] Flag {
2016-06-17 13:54:54 +00:00
& Int64SliceFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : NewInt64Slice ( ) } ,
2016-06-12 04:54:33 +00:00
} ,
Action : func ( ctx * Context ) error {
if ! reflect . DeepEqual ( ctx . Int64Slice ( "serve" ) , [ ] int64 { 10 , 17179869184 } ) {
t . Errorf ( "main name not set" )
}
if ! reflect . DeepEqual ( ctx . Int64Slice ( "s" ) , [ ] int64 { 10 , 17179869184 } ) {
t . Errorf ( "short name not set" )
}
return nil
} ,
} ) . Run ( [ ] string { "run" , "-s" , "10" , "-s" , "17179869184" } )
}
func TestParseMultiInt64SliceFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-12 04:54:33 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,17179869184" )
( & App {
Flags : [ ] Flag {
2016-06-17 13:54:54 +00:00
& Int64SliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewInt64Slice ( ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
2016-06-12 04:54:33 +00:00
} ,
Action : func ( ctx * Context ) error {
if ! reflect . DeepEqual ( ctx . Int64Slice ( "intervals" ) , [ ] int64 { 20 , 30 , 17179869184 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . Int64Slice ( "i" ) , [ ] int64 { 20 , 30 , 17179869184 } ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
} ) . Run ( [ ] string { "run" } )
}
func TestParseMultiInt64SliceFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-12 04:54:33 +00:00
os . Setenv ( "APP_INTERVALS" , "20,30,17179869184" )
( & App {
Flags : [ ] Flag {
2016-06-17 13:54:54 +00:00
& Int64SliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewInt64Slice ( ) , EnvVars : [ ] string { "COMPAT_INTERVALS" , "APP_INTERVALS" } } ,
2016-06-12 04:54:33 +00:00
} ,
Action : func ( ctx * Context ) error {
if ! reflect . DeepEqual ( ctx . Int64Slice ( "intervals" ) , [ ] int64 { 20 , 30 , 17179869184 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . Int64Slice ( "i" ) , [ ] int64 { 20 , 30 , 17179869184 } ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
} ) . Run ( [ ] string { "run" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiFloat64 ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& Float64Flag { Name : "serve" , Aliases : [ ] string { "s" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ctx . Float64 ( "serve" ) != 10.2 {
t . Errorf ( "main name not set" )
}
if ctx . Float64 ( "s" ) != 10.2 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
}
a . Run ( [ ] string { "run" , "-s" , "10.2" } )
}
2015-11-14 21:39:38 +00:00
func TestParseDestinationFloat64 ( t * testing . T ) {
var dest float64
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& Float64Flag {
2015-11-14 21:39:38 +00:00
Name : "dest" ,
Destination : & dest ,
} ,
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2015-11-14 21:39:38 +00:00
if dest != 10.2 {
t . Errorf ( "expected destination Float64 10.2" )
}
2016-04-27 13:12:34 +00:00
return nil
2015-11-14 21:39:38 +00:00
} ,
}
a . Run ( [ ] string { "run" , "--dest" , "10.2" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiFloat64FromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_TIMEOUT_SECONDS" , "15.5" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& Float64Flag { Name : "timeout" , Aliases : [ ] string { "t" } , EnvVars : [ ] string { "APP_TIMEOUT_SECONDS" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ctx . Float64 ( "timeout" ) != 15.5 {
t . Errorf ( "main name not set" )
}
if ctx . Float64 ( "t" ) != 15.5 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiFloat64FromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_TIMEOUT_SECONDS" , "15.5" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& Float64Flag { Name : "timeout" , Aliases : [ ] string { "t" } , EnvVars : [ ] string { "COMPAT_TIMEOUT_SECONDS" , "APP_TIMEOUT_SECONDS" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ctx . Float64 ( "timeout" ) != 15.5 {
t . Errorf ( "main name not set" )
}
if ctx . Float64 ( "t" ) != 15.5 {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2016-06-29 02:52:25 +00:00
func TestParseMultiFloat64SliceFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-29 02:52:25 +00:00
os . Setenv ( "APP_INTERVALS" , "0.1,-10.5" )
( & App {
Flags : [ ] Flag {
& Float64SliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewFloat64Slice ( ) , EnvVars : [ ] string { "APP_INTERVALS" } } ,
} ,
Action : func ( ctx * Context ) error {
if ! reflect . DeepEqual ( ctx . Float64Slice ( "intervals" ) , [ ] float64 { 0.1 , - 10.5 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . Float64Slice ( "i" ) , [ ] float64 { 0.1 , - 10.5 } ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
} ) . Run ( [ ] string { "run" } )
}
func TestParseMultiFloat64SliceFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-06-29 02:52:25 +00:00
os . Setenv ( "APP_INTERVALS" , "0.1234,-10.5" )
( & App {
Flags : [ ] Flag {
& Float64SliceFlag { Name : "intervals" , Aliases : [ ] string { "i" } , Value : NewFloat64Slice ( ) , EnvVars : [ ] string { "COMPAT_INTERVALS" , "APP_INTERVALS" } } ,
} ,
Action : func ( ctx * Context ) error {
if ! reflect . DeepEqual ( ctx . Float64Slice ( "intervals" ) , [ ] float64 { 0.1234 , - 10.5 } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . Float64Slice ( "i" ) , [ ] float64 { 0.1234 , - 10.5 } ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
} ) . Run ( [ ] string { "run" } )
}
2013-11-18 23:35:23 +00:00
func TestParseMultiBool ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag { Name : "serve" , Aliases : [ ] string { "s" } } ,
2013-11-18 23:35:23 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2013-11-18 23:35:23 +00:00
if ctx . Bool ( "serve" ) != true {
t . Errorf ( "main name not set" )
}
if ctx . Bool ( "s" ) != true {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2013-11-18 23:35:23 +00:00
} ,
}
a . Run ( [ ] string { "run" , "--serve" } )
}
2014-04-15 14:16:47 +00:00
2015-11-14 21:39:38 +00:00
func TestParseDestinationBool ( t * testing . T ) {
var dest bool
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag {
2015-11-14 21:39:38 +00:00
Name : "dest" ,
Destination : & dest ,
} ,
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2015-11-14 21:39:38 +00:00
if dest != true {
t . Errorf ( "expected destination Bool true" )
}
2016-04-27 13:12:34 +00:00
return nil
2015-11-14 21:39:38 +00:00
} ,
}
a . Run ( [ ] string { "run" , "--dest" } )
}
2014-07-11 22:13:10 +00:00
func TestParseMultiBoolFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_DEBUG" , "1" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag { Name : "debug" , Aliases : [ ] string { "d" } , EnvVars : [ ] string { "APP_DEBUG" } } ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ctx . Bool ( "debug" ) != true {
t . Errorf ( "main name not set from env" )
}
if ctx . Bool ( "d" ) != true {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseMultiBoolFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_DEBUG" , "1" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag { Name : "debug" , Aliases : [ ] string { "d" } , EnvVars : [ ] string { "COMPAT_DEBUG" , "APP_DEBUG" } } ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ctx . Bool ( "debug" ) != true {
t . Errorf ( "main name not set from env" )
}
if ctx . Bool ( "d" ) != true {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2016-05-18 12:30:28 +00:00
func TestParseMultiBoolTrue ( t * testing . T ) {
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag { Name : "implode" , Aliases : [ ] string { "i" } , Value : true } ,
2016-05-18 12:30:28 +00:00
} ,
Action : func ( ctx * Context ) error {
if ctx . Bool ( "implode" ) {
t . Errorf ( "main name not set" )
}
if ctx . Bool ( "i" ) {
t . Errorf ( "short name not set" )
}
return nil
} ,
}
a . Run ( [ ] string { "run" , "--implode=false" } )
}
func TestParseDestinationBoolTrue ( t * testing . T ) {
dest := true
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag {
2016-05-18 12:30:28 +00:00
Name : "dest" ,
Value : true ,
Destination : & dest ,
} ,
} ,
Action : func ( ctx * Context ) error {
if dest {
t . Errorf ( "expected destination Bool false" )
}
return nil
} ,
}
a . Run ( [ ] string { "run" , "--dest=false" } )
}
func TestParseMultiBoolTrueFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-05-18 12:30:28 +00:00
os . Setenv ( "APP_DEBUG" , "0" )
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag {
2016-05-22 01:29:45 +00:00
Name : "debug" ,
Aliases : [ ] string { "d" } ,
Value : true ,
EnvVars : [ ] string { "APP_DEBUG" } ,
2016-05-18 12:30:28 +00:00
} ,
} ,
Action : func ( ctx * Context ) error {
if ctx . Bool ( "debug" ) {
t . Errorf ( "main name not set from env" )
}
if ctx . Bool ( "d" ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
}
a . Run ( [ ] string { "run" } )
}
func TestParseMultiBoolTrueFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2016-05-18 12:30:28 +00:00
os . Setenv ( "APP_DEBUG" , "0" )
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& BoolFlag {
2016-05-22 01:29:45 +00:00
Name : "debug" ,
Aliases : [ ] string { "d" } ,
Value : true ,
EnvVars : [ ] string { "COMPAT_DEBUG" , "APP_DEBUG" } ,
2016-05-18 12:30:28 +00:00
} ,
} ,
Action : func ( ctx * Context ) error {
if ctx . Bool ( "debug" ) {
t . Errorf ( "main name not set from env" )
}
if ctx . Bool ( "d" ) {
t . Errorf ( "short name not set from env" )
}
return nil
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-04-15 14:16:47 +00:00
type Parser [ 2 ] string
func ( p * Parser ) Set ( value string ) error {
parts := strings . Split ( value , "," )
if len ( parts ) != 2 {
return fmt . Errorf ( "invalid format" )
}
( * p ) [ 0 ] = parts [ 0 ]
( * p ) [ 1 ] = parts [ 1 ]
return nil
}
func ( p * Parser ) String ( ) string {
return fmt . Sprintf ( "%s,%s" , p [ 0 ] , p [ 1 ] )
}
2016-09-17 23:54:29 +00:00
func ( p * Parser ) Get ( ) interface { } {
return p
}
2014-04-15 14:16:47 +00:00
func TestParseGeneric ( t * testing . T ) {
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& GenericFlag { Name : "serve" , Aliases : [ ] string { "s" } , Value : & Parser { } } ,
2014-04-15 14:16:47 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-04-15 14:16:47 +00:00
if ! reflect . DeepEqual ( ctx . Generic ( "serve" ) , & Parser { "10" , "20" } ) {
t . Errorf ( "main name not set" )
}
if ! reflect . DeepEqual ( ctx . Generic ( "s" ) , & Parser { "10" , "20" } ) {
t . Errorf ( "short name not set" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-04-15 14:16:47 +00:00
} ,
}
a . Run ( [ ] string { "run" , "-s" , "10,20" } )
}
2014-07-11 22:13:10 +00:00
func TestParseGenericFromEnv ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-07-11 22:13:10 +00:00
os . Setenv ( "APP_SERVE" , "20,30" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& GenericFlag {
2016-05-22 01:29:45 +00:00
Name : "serve" ,
Aliases : [ ] string { "s" } ,
Value : & Parser { } ,
EnvVars : [ ] string { "APP_SERVE" } ,
} ,
2014-07-11 22:13:10 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-07-11 22:13:10 +00:00
if ! reflect . DeepEqual ( ctx . Generic ( "serve" ) , & Parser { "20" , "30" } ) {
t . Errorf ( "main name not set from env" )
}
if ! reflect . DeepEqual ( ctx . Generic ( "s" ) , & Parser { "20" , "30" } ) {
t . Errorf ( "short name not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-07-11 22:13:10 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2014-09-23 03:24:08 +00:00
func TestParseGenericFromEnvCascade ( t * testing . T ) {
2017-08-04 15:59:27 +00:00
clearenv ( )
2014-09-23 03:24:08 +00:00
os . Setenv ( "APP_FOO" , "99,2000" )
2015-07-20 19:18:25 +00:00
a := App {
Flags : [ ] Flag {
2016-05-22 19:20:52 +00:00
& GenericFlag {
2016-05-22 01:29:45 +00:00
Name : "foos" ,
Value : & Parser { } ,
EnvVars : [ ] string { "COMPAT_FOO" , "APP_FOO" } ,
} ,
2014-09-23 03:24:08 +00:00
} ,
2016-04-27 13:12:34 +00:00
Action : func ( ctx * Context ) error {
2014-09-23 03:24:08 +00:00
if ! reflect . DeepEqual ( ctx . Generic ( "foos" ) , & Parser { "99" , "2000" } ) {
t . Errorf ( "value not set from env" )
}
2016-04-27 13:12:34 +00:00
return nil
2014-09-23 03:24:08 +00:00
} ,
}
a . Run ( [ ] string { "run" } )
}
2016-04-29 06:53:58 +00:00
func TestStringSlice_Serialized_Set ( t * testing . T ) {
sl0 := NewStringSlice ( "a" , "b" )
ser0 := sl0 . Serialized ( )
if len ( ser0 ) < len ( slPfx ) {
t . Fatalf ( "serialized shorter than expected: %q" , ser0 )
}
sl1 := NewStringSlice ( "c" , "d" )
sl1 . Set ( ser0 )
if sl0 . String ( ) != sl1 . String ( ) {
t . Fatalf ( "pre and post serialization do not match: %v != %v" , sl0 , sl1 )
}
}
func TestIntSlice_Serialized_Set ( t * testing . T ) {
sl0 := NewIntSlice ( 1 , 2 )
ser0 := sl0 . Serialized ( )
if len ( ser0 ) < len ( slPfx ) {
t . Fatalf ( "serialized shorter than expected: %q" , ser0 )
}
sl1 := NewIntSlice ( 3 , 4 )
sl1 . Set ( ser0 )
if sl0 . String ( ) != sl1 . String ( ) {
t . Fatalf ( "pre and post serialization do not match: %v != %v" , sl0 , sl1 )
}
}
2016-06-17 13:54:54 +00:00
func TestInt64Slice_Serialized_Set ( t * testing . T ) {
sl0 := NewInt64Slice ( int64 ( 1 ) , int64 ( 2 ) )
ser0 := sl0 . Serialized ( )
if len ( ser0 ) < len ( slPfx ) {
t . Fatalf ( "serialized shorter than expected: %q" , ser0 )
}
sl1 := NewInt64Slice ( int64 ( 3 ) , int64 ( 4 ) )
sl1 . Set ( ser0 )
if sl0 . String ( ) != sl1 . String ( ) {
t . Fatalf ( "pre and post serialization do not match: %v != %v" , sl0 , sl1 )
}
}