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

Add support for --version flag #3442

Merged

Conversation

ffapitalle
Copy link
Contributor

What?

Adds support for --version global flag that matches the version subcommand.

Why?

Running the usual Unix-like flags returns an error.

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

#2352

@CLAassistant
Copy link

CLAassistant commented Nov 6, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ffapitalle,
thanks for your contribution! 🙇

@@ -42,7 +42,7 @@ func TestVersion(t *testing.T) {
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, "k6 v"+consts.Version)
assert.Contains(t, stdout, "k6 version "+consts.Version)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prints

k6 version 0.47.0 (go1.21.1, linux/amd64)

That means it would be a breaking change for someone relying on it on automated tools. And, it doesn't match really the version we usually announce, check https://github.com/grafana/k6/releases/tag/v0.47.0 for an example.

Can you roll back it, please? I expect you're doing it because it is the default template for Cobra but if you use SetVersionTemplate then you should be able to change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @codebien !

Thank you for your feedback. I did change that and the corresponding test to match Cobra's default.
I'll rollback that and change the default.

@ffapitalle ffapitalle requested a review from codebien November 7, 2023 17:46
@codecov-commenter
Copy link

codecov-commenter commented Nov 8, 2023

Codecov Report

Attention: 10 lines in your changes are missing coverage. Please review.

Comparison is base (6dc5783) 73.35% compared to head (aa81d7a) 73.30%.
Report is 3 commits behind head on master.

❗ Current head aa81d7a differs from pull request most recent head 4bc63fd. Consider uploading reports for the commit 4bc63fd to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3442      +/-   ##
==========================================
- Coverage   73.35%   73.30%   -0.05%     
==========================================
  Files         261      259       -2     
  Lines       19742    19773      +31     
==========================================
+ Hits        14481    14495      +14     
- Misses       4367     4376       +9     
- Partials      894      902       +8     
Flag Coverage Δ
ubuntu 73.30% <78.72%> (+0.03%) ⬆️
windows ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
cmd/root.go 94.37% <100.00%> (+0.37%) ⬆️
cmd/state/state.go 6.94% <100.00%> (+1.31%) ⬆️
cmd/version.go 100.00% <100.00%> (ø)
cmd/run.go 74.38% <80.00%> (+0.11%) ⬆️
cmd/ui.go 69.65% <0.00%> (-0.71%) ⬇️
api/server.go 68.75% <40.00%> (-13.86%) ⬇️

... and 10 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffapitalle Thanks for the changes.

I think we're now missing two more things:

Must to have: add to the related test here https://github.com/grafana/k6/blob/e44a08fa5d642ba148c08d4a1eb291de576009c5/cmd/tests/cmd_run_test.go#L37C1-L52C2 the case with the new flag. It requires a bit of refactoring to have a table test. Or a simpler alternative could be to move the test to a new cmd_version_test.go file and have a dedicated test like TestVersionFlag.

Nice to have: having the k6 version command calling the root's command or vice versa. In this way, we are 100% sure that one change will be also applied to the other.

cmd/root.go Outdated
}

rootCmd.SetVersionTemplate(
`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}\n`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}\n`,
`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s" .Version}}`,

Is it required? On Alacritty I get the following output:

k6 v0.47.0 (go1.21.1, linux/amd64)\n% 

In any case, even after removing the newline the % char still remains. It requires a bit of investigation about why.

Copy link
Contributor Author

@ffapitalle ffapitalle Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault on this. It was the wrong place to include the newline character. I've fixed it.

@ffapitalle
Copy link
Contributor Author

Must to have: add to the related test here https://github.com/grafana/k6/blob/e44a08fa5d642ba148c08d4a1eb291de576009c5/cmd/tests/cmd_run_test.go#L37C1-L52C2 the case with the new flag. It requires a bit of refactoring to have a table test.

I went for the table test schema.

Nice to have: having the k6 version command calling the root's command or vice versa. In this way, we are 100% sure that one change will be also applied to the other.

I gave it a few shots but with no luck. Both (flag and subcommand) rely on the versionString function, so the actual version comes from this function.

@ffapitalle ffapitalle requested a review from codebien November 9, 2023 13:57
cmd/tests/cmd_run_test.go:49  whitespace  unnecessary leading newline
cmd/version.go Outdated Show resolved Hide resolved
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to apply go fmt otherwise the linter will complain about indentation.

cmd/version.go Outdated Show resolved Hide resolved
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, waiting for a green CI

Copy link
Contributor

@olegbespalov olegbespalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! 🙇

@codebien codebien added this to the v0.48.0 milestone Nov 10, 2023
@olegbespalov olegbespalov added ux documentation-needed A PR which will need a separate PR for documentation labels Nov 10, 2023
@codebien codebien merged commit 46bb65e into grafana:master Nov 10, 2023
20 checks passed
@ffapitalle ffapitalle deleted the ISSUE-2352/add-support--version-flag branch November 10, 2023 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation-needed A PR which will need a separate PR for documentation ux
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants