IsSet breaks with Persistent option
chirino opened this issue · comments
Hiram Chirino commented
Checklist
- Are you running the latest v3 release? The list of releases is here.
- Did you check the manual for your release? The v3 manual is here.
- Did you perform a search about this feature? Here's the GitHub guide about searching.
What problem does this solve?
Is set should work even when the persistent flag is used with it:
package main_test
import (
"context"
"github.com/urfave/cli/v3"
"testing"
)
func TestCommonFlags(t *testing.T) {
result := ""
resultIsSet := false
app := &cli.Command{
Name: "root",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "result",
Persistent: true,
},
},
Commands: []*cli.Command{
{
Name: "sub",
Action: func(ctx *cli.Context) error {
result = ctx.String("result")
resultIsSet = ctx.IsSet("result")
return nil
},
},
},
}
if err := app.Run(context.Background(), []string{"root", "sub", "--result", "after"}); err != nil {
t.Fatal(err)
}
if result != "after" {
t.Fatalf("result is not after")
}
if !resultIsSet {
t.Fatalf("result is not set after")
}
if err := app.Run(context.Background(), []string{"root", "--result", "before", "sub"}); err != nil {
t.Fatal(err)
}
if result != "before" {
t.Fatalf("result is not before")
}
if !resultIsSet {
t.Fatalf("result is not set before")
}
}
Solution description
The above test should pass.
Describe alternatives you've considered
Avoid using the IsSet function.
Hiram Chirino commented
here ya go.. I think you can add this to your unit tests...
func TestPersistentFlagIsSet(t *testing.T) {
result := ""
resultIsSet := false
app := &Command{
Name: "root",
Flags: []Flag{
&StringFlag{
Name: "result",
Persistent: true,
},
},
Commands: []*Command{
{
Name: "sub",
Action: func(_ context.Context, cmd *Command) error {
result = cmd.String("result")
resultIsSet = cmd.IsSet("result")
return nil
},
},
},
}
r := require.New(t)
err := app.Run(context.Background(), []string{"root", "--result", "before", "sub"})
r.NoError(err)
r.Equal("before", result)
r.True(resultIsSet)
err = app.Run(context.Background(), []string{"root", "sub", "--result", "after"})
r.NoError(err)
r.Equal("after", result)
r.True(resultIsSet)
}
Hiram Chirino commented
That was quick work @dearchap, thanks!