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 2.1.0 regression #1014

Merged
merged 13 commits into from
Dec 25, 2019
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
3 changes: 3 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
if parentCtx != nil {
c.Context = parentCtx.Context
c.shellComplete = parentCtx.shellComplete
if parentCtx.flagSet == nil {
parentCtx.flagSet = &flag.FlagSet{}
}
}

c.Command = &Command{}
Expand Down
75 changes: 75 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,81 @@ func TestContextPropagation(t *testing.T) {
}
}

func TestContextAttributeAccessing(t *testing.T) {
tdata := []struct {
testCase string
setBoolInput string
ctxBoolInput string
newContextInput *Context
}{
{
testCase: "empty",
setBoolInput: "",
ctxBoolInput: "",
newContextInput: nil,
},
{
testCase: "empty_with_background_context",
setBoolInput: "",
ctxBoolInput: "",
newContextInput: &Context{Context: context.Background()},
},
{
testCase: "empty_set_bool_and_present_ctx_bool",
setBoolInput: "",
ctxBoolInput: "ctx-bool",
newContextInput: nil,
},
{
testCase: "present_set_bool_and_present_ctx_bool_with_background_context",
setBoolInput: "",
ctxBoolInput: "ctx-bool",
newContextInput: &Context{Context: context.Background()},
},
{
testCase: "present_set_bool_and_present_ctx_bool",
setBoolInput: "ctx-bool",
ctxBoolInput: "ctx-bool",
newContextInput: nil,
},
{
testCase: "present_set_bool_and_present_ctx_bool_with_background_context",
setBoolInput: "ctx-bool",
ctxBoolInput: "ctx-bool",
newContextInput: &Context{Context: context.Background()},
},
{
testCase: "present_set_bool_and_different_ctx_bool",
setBoolInput: "ctx-bool",
ctxBoolInput: "not-ctx-bool",
newContextInput: nil,
},
{
testCase: "present_set_bool_and_different_ctx_bool_with_background_context",
setBoolInput: "ctx-bool",
ctxBoolInput: "not-ctx-bool",
newContextInput: &Context{Context: context.Background()},
},
}

for _, test := range tdata {
t.Run(test.testCase, func(t *testing.T) {
// setup
set := flag.NewFlagSet("some-flag-set-name", 0)
set.Bool(test.setBoolInput, false, "usage documentation")
ctx := NewContext(nil, set, test.newContextInput)

// logic under test
value := ctx.Bool(test.ctxBoolInput)

// assertions
if value != false {
t.Errorf("expected \"value\" to be false, but it was not")
}
})
}
}

func TestCheckRequiredFlags(t *testing.T) {
tdata := []struct {
testCase string
Expand Down
9 changes: 8 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

View [unreleased 2.X] series changes.

## [2.1.1] - 2019-12-24

### Fixed

* Fixed a `Context` regression introduced in `v2.1.0` in [urfave/cli/pull/1014](https://github.com/urfave/cli/pull/1014) via [@lynncyrin](https://github.com/lynncyrin)

## [2.1.0] - 2019-12-24

These release notes were written for the git hash [ae84df4cef4a2a6f1a0cb1d41ea0f3af8755e5a8](https://github.com/urfave/cli/tree/ae84df4cef4a2a6f1a0cb1d41ea0f3af8755e5a8)
Expand Down Expand Up @@ -541,7 +547,8 @@ signature of `func(*cli.Context) error`, as defined by `cli.ActionFunc`.
### Added
- Initial implementation.

[unreleased 2.X]: https://github.com/urfave/cli/compare/v2.1.0...HEAD
[unreleased 2.X]: https://github.com/urfave/cli/compare/v2.1.1...HEAD
[2.1.1]: https://github.com/urfave/cli/compare/v2.1.0...v2.1.1
[2.1.0]: https://github.com/urfave/cli/compare/v2.0.0...v2.1.0
[2.0.0]: https://github.com/urfave/cli/compare/v1.22.2...v2.0.0

Expand Down
61 changes: 32 additions & 29 deletions docs/v2/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1402,13 +1402,15 @@ func main() {
cli.ShowVersion(c)

fmt.Printf("%#v\n", c.App.Command("doo"))
if c.Bool("infinite") {
c.App.Run([]string{"app", "doo", "wop"})
}

if c.Bool("forevar") {
c.App.RunAsSubcommand(c)
}
// // uncomment when https://github.com/urfave/cli/pull/1014 is released
// if c.Bool("infinite") {
// c.App.Run([]string{"app", "doo", "wop"})
// }

// // uncomment when https://github.com/urfave/cli/pull/1014 is released
// if c.Bool("forevar") {
// c.App.RunAsSubcommand(c)
// }
c.App.Setup()
fmt.Printf("%#v\n", c.App.VisibleCategories())
fmt.Printf("%#v\n", c.App.VisibleCommands())
Expand All @@ -1424,28 +1426,29 @@ func main() {
set := flag.NewFlagSet("contrive", 0)
nc := cli.NewContext(c.App, set, c)

fmt.Printf("%#v\n", nc.Args())
fmt.Printf("%#v\n", nc.Bool("nope"))
fmt.Printf("%#v\n", !nc.Bool("nerp"))
fmt.Printf("%#v\n", nc.Duration("howlong"))
fmt.Printf("%#v\n", nc.Float64("hay"))
fmt.Printf("%#v\n", nc.Generic("bloop"))
fmt.Printf("%#v\n", nc.Int64("bonk"))
fmt.Printf("%#v\n", nc.Int64Slice("burnks"))
fmt.Printf("%#v\n", nc.Int("bips"))
fmt.Printf("%#v\n", nc.IntSlice("blups"))
fmt.Printf("%#v\n", nc.String("snurt"))
fmt.Printf("%#v\n", nc.StringSlice("snurkles"))
fmt.Printf("%#v\n", nc.Uint("flub"))
fmt.Printf("%#v\n", nc.Uint64("florb"))

fmt.Printf("%#v\n", nc.FlagNames())
fmt.Printf("%#v\n", nc.IsSet("wat"))
fmt.Printf("%#v\n", nc.Set("wat", "nope"))
fmt.Printf("%#v\n", nc.NArg())
fmt.Printf("%#v\n", nc.NumFlags())
fmt.Printf("%#v\n", nc.Lineage()[1])

// // uncomment when https://github.com/urfave/cli/pull/1014 is released
// fmt.Printf("%#v\n", nc.Args())
// fmt.Printf("%#v\n", nc.Bool("nope"))
// fmt.Printf("%#v\n", !nc.Bool("nerp"))
// fmt.Printf("%#v\n", nc.Duration("howlong"))
// fmt.Printf("%#v\n", nc.Float64("hay"))
// fmt.Printf("%#v\n", nc.Generic("bloop"))
// fmt.Printf("%#v\n", nc.Int64("bonk"))
// fmt.Printf("%#v\n", nc.Int64Slice("burnks"))
// fmt.Printf("%#v\n", nc.Int("bips"))
// fmt.Printf("%#v\n", nc.IntSlice("blups"))
// fmt.Printf("%#v\n", nc.String("snurt"))
// fmt.Printf("%#v\n", nc.StringSlice("snurkles"))
// fmt.Printf("%#v\n", nc.Uint("flub"))
// fmt.Printf("%#v\n", nc.Uint64("florb"))

// // uncomment when https://github.com/urfave/cli/pull/1014 is released
// fmt.Printf("%#v\n", nc.FlagNames())
// fmt.Printf("%#v\n", nc.IsSet("wat"))
// fmt.Printf("%#v\n", nc.Set("wat", "nope"))
// fmt.Printf("%#v\n", nc.NArg())
// fmt.Printf("%#v\n", nc.NumFlags())
// fmt.Printf("%#v\n", nc.Lineage()[1])
nc.Set("wat", "also-nope")

ec := cli.Exit("ohwell", 86)
Expand Down