Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context.IsSet() #597

Merged
merged 1 commit into from
Apr 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *Context) IsSet(name string) bool {
for _, envVar := range envVarValues.Interface().([]string) {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" {
continue
return true
}
}

Expand Down
63 changes: 63 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"flag"
"os"
"sort"
"testing"
"time"
Expand Down Expand Up @@ -157,6 +158,68 @@ func TestContext_IsSet(t *testing.T) {
expect(t, ctx.IsSet("bogus"), false)
}

// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
// Should be moved to `flag_test` in v2
func TestContext_IsSet_fromEnv(t *testing.T) {
var (
timeoutIsSet, tIsSet, noEnvVarIsSet, nIsSet bool
globalTimeoutIsSet, TIsSet, globalNoEnvVarIsSet, NIsSet bool
)

os.Clearenv()
os.Setenv("GLOBAL_APP_TIMEOUT_SECONDS", "15.5")
os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
a := App{
Flags: []Flag{
&Float64Flag{
Name: "global-timeout",
Aliases: []string{"T"},
EnvVars: []string{"GLOBAL_APP_TIMEOUT_SECONDS"},
},
&Float64Flag{
Name: "global-no-env-var",
Aliases: []string{"N"},
},
},
Commands: []*Command{
{
Name: "hello",
Flags: []Flag{
&Float64Flag{
Name: "timeout",
Aliases: []string{"t"},
EnvVars: []string{"APP_TIMEOUT_SECONDS"},
},
&Float64Flag{
Name: "no-env-var",
Aliases: []string{"n"},
},
},
Action: func(ctx *Context) error {
globalTimeoutIsSet = ctx.IsSet("global-timeout")
TIsSet = ctx.IsSet("T")
globalNoEnvVarIsSet = ctx.IsSet("global-no-env-var")
NIsSet = ctx.IsSet("N")
timeoutIsSet = ctx.IsSet("timeout")
tIsSet = ctx.IsSet("t")
noEnvVarIsSet = ctx.IsSet("no-env-var")
nIsSet = ctx.IsSet("n")
return nil
},
},
},
}
a.Run([]string{"run", "hello"})
expect(t, globalTimeoutIsSet, true)
expect(t, TIsSet, true)
expect(t, globalNoEnvVarIsSet, false)
expect(t, NIsSet, false)
expect(t, timeoutIsSet, true)
expect(t, tIsSet, true)
expect(t, noEnvVarIsSet, false)
expect(t, nIsSet, false)
}

func TestContext_NumFlags(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
Expand Down