-
Notifications
You must be signed in to change notification settings - Fork 3k
Make detection for test-binary more universal #2173
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
Conversation
e494f3d
to
cc99a68
Compare
cc99a68
to
c6a0f3c
Compare
@thaJeztah Thanks for this. I had never dug into the history of this I'm trying to reproduce the issue by removing this
Then removed the
Notice that the test flags are processed by cobra, but the test still passes. I wonder why things fail in your case? |
Ah, yeah, I probably should've provided a good reproducer when I was working on this 😂 (It's been a while). Let me try come with one 👍 IIRC this happened either when the test was pre-compiled and run, but also in some cases when running tests from my IDE (I recall I ran into these when working on docker/cli, and re-generating the |
I think I have an example (I saw I wrote one up in docker/cli#5224), and it was indeed when pre-compiling the tests; Before this patch: go test -c -o foo.test
./foo.test -test.run TestNoArgs
--- FAIL: TestNoArgs (0.00s)
args_test.go:37: Unexpected output: Error: unknown command "TestNoArgs" for "c"
Usage:
c [flags]
Flags:
-h, --help help for c
args_test.go:40: Unexpected error: unknown command "TestNoArgs" for "c"
FAIL After this patch: go test -c -o foo.test
./foo.test -test.run TestNoArgs
PASS I can update the commit message with that information 👍 |
When running tests in verbose mode (or other options), tests involving Cobra may fail if the test does not explicitly set Command.args to an empty slice; in this case, Cobra defaults to using `os.Args`, which will contain arguments passed to the test (such as `-v` (verbose)). Commits e576205 and 1ef0913 implemented a workaround for this when running (unit) tests for Cobra itself, but this check is specifig to Cobra (checking for `cobra.test`), and don't work on Windows (which will have a `.exe` extension), This patch implements a more universal check, so that users of Cobra as a module also benefit from this workaround. go1.21 and up provides a `testing.Testing()` utility ([1]); as the Cobra module still supports Go1.16 and up, an alternative implementation was added for older versions, based on golang.org/x/mod/lazyregexp [2]. Before this patch: Before this patch: go test -c -o foo.test ./foo.test -test.run TestNoArgs --- FAIL: TestNoArgs (0.00s) args_test.go:37: Unexpected output: Error: unknown command "TestNoArgs" for "c" Usage: c [flags] Flags: -h, --help help for c args_test.go:40: Unexpected error: unknown command "TestNoArgs" for "c" FAIL After this patch: go test -c -o foo.test ./foo.test -test.run TestNoArgs PASS [1]: https://pkg.go.dev/testing#Testing [2]: https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78 Signed-off-by: Sebastiaan van Stijn <[email protected]>
c6a0f3c
to
ac1ca09
Compare
Updated the commit message for future reference 😄 |
Aha, I see. So the I see that using the = form works, as I would expect: |
Signed-off-by: Marc Khouzam <[email protected]>
I pushed a commit addressing the comments and adding a unit test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thaJeztah If you have comments on the latest commit I pushed, please let me know and we can fix it in a follow up.
Ah! Sorry, I was away from my keyboard, so didn't push updates; thanks @marckhouzam ! |
This is causing some unit tests to fail from some projects using Cobra. For example, in the And in I am able to fix the tests for the cc @thaJeztah @jpmcb |
Hm.. that's unfortunate! I'd not be horribly upset if it was reverted; it was really a small quality-of-life improvement so that consumers wouldn't have to adjust their tests (I think I got most of them in our tests, but once in a while I run into another one 😂) |
I feel like the case that got broken is much less common than the case this PR fixed. I was thinking Cobra could provide a global array of strings that a test can set when it needs to set the os.Args. Something like:
However, I’m thinking that for developers to find this special variable when they need it, will be almost impossible, so I’m leaning toward reverting this PR. @thaJeztah @jpmcb what do you think? |
Reverting for now sounds good to me; we can open a new PR to un-revert to work on an approach that would work and/or check with affected projects to see what a reasonable approach would be. |
Reverting sounds fine although, I agree, I think there should be something besides developers having to set |
This reverts commit d1e9d85. Some programs set os.Args in their unit tests and this change would break those tests. Ref: spf13#2173 (comment)
I've opened #2235 to blindly revert for the moment, so that we can do a 1.9.1 release today. |
This reverts commit d1e9d85. Some programs set os.Args in their unit tests and this change would break those tests. Ref: #2173 (comment)
This reverts commit d1e9d85fcf592461f3bc2f4b6d5e140c4c0aabf8. Some programs set os.Args in their unit tests and this change would break those tests. Ref: spf13/cobra#2173 (comment)
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/spf13/cobra](https://github.com/spf13/cobra) | require | minor | `v1.8.1` -> `v1.9.1` | --- ### Release Notes <details> <summary>spf13/cobra (github.com/spf13/cobra)</summary> ### [`v1.9.1`](https://github.com/spf13/cobra/releases/tag/v1.9.1) [Compare Source](spf13/cobra@v1.9.0...v1.9.1) ##### 🐛 Fixes - Fix CompletionFunc implementation by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2234 - Revert "Make detection for test-binary more universal ([#​2173](spf13/cobra#2173))" by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2235 **Full Changelog**: spf13/cobra@v1.9.0...v1.9.1 ### [`v1.9.0`](https://github.com/spf13/cobra/releases/tag/v1.9.0) [Compare Source](spf13/cobra@v1.8.1...v1.9.0) #### ✨ Features - Allow linker to perform deadcode elimination for program using Cobra by [@​aarzilli](https://github.com/aarzilli) in spf13/cobra#1956 - Add default completion command even if there are no other sub-commands by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#1559 - Add CompletionWithDesc helper by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2231 #### 🐛 Fixes - Fix deprecation comment for Command.SetOutput by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2172 - Replace deprecated ioutil usage by [@​nirs](https://github.com/nirs) in spf13/cobra#2181 - Fix --version help and output for plugins by [@​nirs](https://github.com/nirs) in spf13/cobra#2180 - Allow to reset the templates to the default by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2229 #### 🤖 Completions - Make Powershell completion work in constrained mode by [@​lstemplinger](https://github.com/lstemplinger) in spf13/cobra#2196 - Improve detection for flags that accept multiple values by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2210 - add CompletionFunc type to help with completions by [@​ccoVeille](https://github.com/ccoVeille) in spf13/cobra#2220 - Add similar whitespace escape logic to bash v2 completions than in other completions by [@​kangasta](https://github.com/kangasta) in spf13/cobra#1743 - Print ActiveHelp for bash along other completions by [@​marckhouzam](https://github.com/marckhouzam) in spf13/cobra#2076 - fix(completions): Complete map flags multiple times by [@​gabe565](https://github.com/gabe565) in spf13/cobra#2174 - fix(bash): nounset unbound file filter variable on empty extension by [@​scop](https://github.com/scop) in spf13/cobra#2228 #### 🧪 Testing - Test also with go 1.23 by [@​nirs](https://github.com/nirs) in spf13/cobra#2182 - Make detection for test-binary more universal by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2173 #### ✍🏼 Documentation - docs: update README.md by [@​eltociear](https://github.com/eltociear) in spf13/cobra#2197 - Improve site formatting by [@​nirs](https://github.com/nirs) in spf13/cobra#2183 - doc: add Conduit by [@​raulb](https://github.com/raulb) in spf13/cobra#2230 - doc: azion project added to the list of CLIs that use cobra by [@​maxwelbm](https://github.com/maxwelbm) in spf13/cobra#2198 - Fix broken links in active_help.md by [@​vuil](https://github.com/vuil) in spf13/cobra#2202 - chore: fix function name in comment by [@​zhuhaicity](https://github.com/zhuhaicity) in spf13/cobra#2216 #### 🔧 Dependency upgrades - build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.5 to 2.0.6 by [@​thaJeztah](https://github.com/thaJeztah) in spf13/cobra#2206 - Update to latest go-md2man by [@​mikelolasagasti](https://github.com/mikelolasagasti) in spf13/cobra#2201 - Upgrade `pflag` dependencies for v1.9.0 by [@​jpmcb](https://github.com/jpmcb) in spf13/cobra#2233 *** Thank you to all of our amazing contributors and all the great work that's been going into the completions feature!! ##### 👋🏼 New Contributors - [@​gabe565](https://github.com/gabe565) made their first contribution in spf13/cobra#2174 - [@​maxwelbm](https://github.com/maxwelbm) made their first contribution in spf13/cobra#2198 - [@​lstemplinger](https://github.com/lstemplinger) made their first contribution in spf13/cobra#2196 - [@​vuil](https://github.com/vuil) made their first contribution in spf13/cobra#2202 - [@​mikelolasagasti](https://github.com/mikelolasagasti) made their first contribution in spf13/cobra#2201 - [@​zhuhaicity](https://github.com/zhuhaicity) made their first contribution in spf13/cobra#2216 - [@​ccoVeille](https://github.com/ccoVeille) made their first contribution in spf13/cobra#2220 - [@​kangasta](https://github.com/kangasta) made their first contribution in spf13/cobra#1743 - [@​aarzilli](https://github.com/aarzilli) made their first contribution in spf13/cobra#1956 **Full Changelog**: spf13/cobra@v1.8.1...v1.9.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MC4wLjgiLCJ1cGRhdGVkSW5WZXIiOiI0MC4wLjgiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbXX0=--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/557 Reviewed-by: earl-warren <[email protected]> Co-authored-by: Renovate Bot <[email protected]> Co-committed-by: Renovate Bot <[email protected]>
This has been reverted by #2235 in release v1.9.1
==
When running tests in verbose mode (or other options), tests involving Cobra may fail if the test does not explicitly set Command.args to an empty slice; in this case, Cobra defaults to using
os.Args
, which will contain arguments passed to the test (such as-v
(verbose)).Commits e576205 and 1ef0913 implemented a workaround for this when running (unit) tests for Cobra itself, but this check is specifig to Cobra (checking for
cobra.test
), and don't work on Windows (which will have a.exe
extension),This patch implements a more universal check, so that users of Cobra as a module also benefit from this workaround.
go1.21 and up provides a
testing.Testing()
utility (1); as the Cobra module still supports Go1.16 and up, an alternative implementation was added for older versions, based on golang.org/x/mod/lazyregexp 2.