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

support passing custom arguments to cargo test #11339

Open
hardfist opened this issue Nov 5, 2022 · 2 comments
Open

support passing custom arguments to cargo test #11339

hardfist opened this issue Nov 5, 2022 · 2 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@hardfist
Copy link

hardfist commented Nov 5, 2022

Problem

We want to support custom snapshot feature to our test, and we need a way to control whether we in test mode or in update mode.
There are currently three ways to implement this.

  • implement a custom command like cargo insta
  • control it by environment like UPDATE to control whether we are in test mode or in update mode
  • disable harness and convert all test to main function to run test
    They are both not the idea way, and we want something like jest or mocha.
$ jest  // test mode
$ jest --updateSnapshot // update mode
$ mocha // test mode
$ mocha --update // update mode

if cargo test could support passing custom arguments to test, we could do it like jest

$ cargo test // test mode
$ cargo test --update // update mode

Proposed Solution

support passing custom arguments to cargo test, maybe we need a configuration for this feature.

Notes

someone also met same problem but not got a ideal answer

@hardfist hardfist added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Nov 5, 2022
@epage
Copy link
Contributor

epage commented Nov 5, 2022

There are two workarounds

Challenges with improving this at cargo's level

  • Most of this actually lives in libtest
  • clap doesn't support this yet
  • Even if clap supports it, there are weird parse cases (unknown flag followed by a value, is that value a positional argument or meant for the flag)

@ehuss
Copy link
Contributor

ehuss commented Dec 11, 2022

There are some issues that I would be concerned about with extensible flags:

  • I think it would be difficult to be able to convey "custom flags" to tell Cargo to parse. Additionally, I'm not even sure that is a good thing, as I think having a standard experience for which flags are valid is good for consistency and avoiding confusion.
  • libtest similarly has the same issue, where it won't know what flags your test is interested in, and which ones it should be parsing.

I understand this is a fairly common use case, so it might be nice to have something to make it easier. I think the cargo insta approach isn't too bad, and I'm curious what makes it seem not ideal. Environment variables I think are a common approach, and I'm also curious what makes them seem not ideal. I can make some guesses, but I'd like to hear more about what's wrong with the current approaches, and if there are other alternatives that would make them easier or more palatable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

3 participants