-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: Command.SetVersionFunc #2313
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2952,3 +2952,18 @@ func TestHelpFuncExecuted(t *testing.T) { | |
|
||
checkStringContains(t, output, helpText) | ||
} | ||
|
||
func TestVersionFuncExecuted(t *testing.T) { | ||
rootCmd := &Command{Use: "root", Run: emptyRun, Version: "v2.3.4"} | ||
rootCmd.SetVersionFunc(func(cmd *Command) error { | ||
_, err := fmt.Fprint(cmd.OutOrStdout(), "custom version function: "+rootCmd.Version) | ||
return err | ||
}) | ||
|
||
output, err := executeCommandWithContext(context.Background(), rootCmd, "-v") | ||
if err != nil { | ||
t.Errorf("Unexpected error: %v", err) | ||
} | ||
|
||
checkStringContains(t, output, "custom version function: v2.3.4") | ||
} | ||
Comment on lines
+2956
to
+2969
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you test when versionFunc is only available on the parent? Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm actually not sure how that should work, as the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know, but the parent.parent.parent thing makes me think there is something to double check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @caarlos0 unless I'm wrong this wasn't addressed yet. I would like to see a test to cover this. |
Uh oh!
There was an error while loading. Please reload this page.