We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
When I test Cobra command, I do this:
cmd := NewCmdStatus(cmdutils.NewFactory()) t.Run("no repo", func(t *testing.T) { err := cmd.Execute() assert.Error(t, err) }) t.Run("no repo with -R", func(t *testing.T) { cmd.SetArgs([]string{"-R", "forkrepo"}) err := cmd.Execute() assert.NoError(t, err) })
The problem is that -R is defined in parent command, so the test fails.
-R
I tried to add the flag explicitly to NewCmdStatus, but that didn't work too.
NewCmdStatus
cmd.InheritedFlags().StringP("repo", "R", "", "Select another repository.")
So how to test that?
The text was updated successfully, but these errors were encountered:
Try to use cmd.Flags().StringP(…)
cmd.Flags().StringP(…)
Sorry, something went wrong.
@marckhouzam then the flag won't appear in inherited flags section.
Try cmd.PersistentFlags().StringP(…) instead. Although I’m not sure it will add it to the inherited flags on the current command.
cmd.PersistentFlags().StringP(…)
An alternative is to create a fake root command in your test, add the flag as persistent and add your real command as a child of this fake root.
No branches or pull requests
When I test Cobra command, I do this:
The problem is that
-R
is defined in parent command, so the test fails.I tried to add the flag explicitly to
NewCmdStatus
, but that didn't work too.So how to test that?
The text was updated successfully, but these errors were encountered: