Skip to content

Commit

Permalink
Merge pull request #1014 from urfave/lynncyrin-patch-12
Browse files Browse the repository at this point in the history
Fix 2.1.0 regression
  • Loading branch information
coilysiren authored Dec 25, 2019
2 parents ed1a2ab + 5b57b2b commit 723f284
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 30 deletions.
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

0 comments on commit 723f284

Please sign in to comment.