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

IsSet breaks with Persistent option #1826

Closed
3 tasks done
chirino opened this issue Nov 29, 2023 · 3 comments · Fixed by #1827
Closed
3 tasks done

IsSet breaks with Persistent option #1826

chirino opened this issue Nov 29, 2023 · 3 comments · Fixed by #1827
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@chirino
Copy link

chirino commented Nov 29, 2023

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.

@chirino chirino added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Nov 29, 2023
@dearchap
Copy link
Contributor

@chirino cli.Context was removed sometime in v3. Can you update to latest v3 ?

@chirino
Copy link
Author

chirino commented Nov 29, 2023

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)

}

@chirino
Copy link
Author

chirino commented Nov 30, 2023

That was quick work @dearchap, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants