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

Please provide an argument to make the derive automatically generate a test #4838

Open
2 tasks done
joshtriplett opened this issue Apr 17, 2023 · 1 comment
Open
2 tasks done
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing

Comments

@joshtriplett
Copy link
Contributor

Please complete the following tasks

Clap Version

4.2.2

Describe your use case

I currently have a manual test written that calls debug_assert.

#[cfg(test)]
mod tests {
    #[test]
    pub fn test_clap() {
        use clap::CommandFactory;
        super::Bit::command().debug_assert();
    }
}

I'd like to, instead, provide a test argument to the #[command] derive, and have clap generate this test for me:

#[derive(Parser, Debug)]
#[command(name = "bit", test)]
pub struct Bit {

Describe the solution you'd like

Please add a test argument for the #[command] derive that generates a test calling debug_assert.

Alternatives, if applicable

As another alternative, clap could generate this test by default, on the theory that it doesn't do any harm if not building/running tests, and it's likely what the user wants. (In that case, though, there should probably be an option test = false just in case the user is doing something strange.)

Additional Context

No response

@joshtriplett joshtriplett added the C-enhancement Category: Raise on the bar on expectations label Apr 17, 2023
@epage epage added E-easy Call for participation: Experience needed to fix: Easy / not much A-derive Area: #[derive]` macro API labels Apr 17, 2023
@epage
Copy link
Member

epage commented Apr 17, 2023

If we did this automatically, we'd want to do it only for the Parser trait as that communicates "top-level". Anything not-"top-level" is more likely for the user to be doing something strange.

On a first pass, I'd want to treat that as a breaking change though which means it would be delayed to v5. A #[command(test)] would allow access now.

Another alternative is for clap to unconditionally call debug_assert() when doing a top-level build. This would require building the whole tree of commands. As we want consistent runtime behavior between debug and release and building all could mask things, we'd likely want to do a clone and then debug_assert. I would expect the different in runtime performance in debug builds to not be bad enough to be a problem though I might want to ping the deno people as they are currently trying to optimize start up times even further (#4774) and so they are more likely to notice and care about debug performance.

@epage epage added S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing and removed E-easy Call for participation: Experience needed to fix: Easy / not much labels Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-enhancement Category: Raise on the bar on expectations S-waiting-on-design Status: Waiting on user-facing design to be resolved before implementing
Projects
None yet
Development

No branches or pull requests

2 participants