clap/clap_generate/tests/completions/mod.rs
Ed Page a61b60816c fix(gen): Ensure subcommands are post-processed
`App::get_matches` lazily post-processes `App`s and `Arg`s so we don't
do it to subcommands that are never run (downside being people have to
exercise their full app to get debug_asserts).

`clap_generate` was only post-processing the top-level `App` and `Arg`s,
ignoring the sub-commands.  In #2858, we noticed that `--version` was
being left in the completions instead of being removed during the
`_build` step.  We would also have an incorrect `num_vals` and a host of
other problems.

This change adds a `App::_build_all` function for `clap_generate` to use
to eagerly build everything.  By having it there, we make sure
everywhere that needs eager building, gets it (like some tests).

In `clap_generate::utils`, we add a unit test to ensure the subcommand's
`--version` was removed.

For some other tests specifying `.version()`, I added
`AppSettings::PropagateVersion` to make it behave more consistently.
The places I didn't were generally where the version was conditionally
set.

For `clap_generate/tests/generate_completions.rs`, I had to adjust the
`conflicts_with` because the subcommand was inheriting the argument with
it defined *but* the subcommand did not have the argument, tripping up a
debug assert.

Fixes #2860
2021-10-12 15:54:26 -05:00

32 lines
757 B
Rust

use clap::{App, AppSettings, Arg, ValueHint};
use clap_generate::{generate, generators::*};
use std::fmt;
mod bash;
mod elvish;
mod fish;
mod powershell;
mod zsh;
#[derive(PartialEq, Eq)]
pub struct PrettyString<'a>(pub &'a str);
impl<'a> fmt::Debug for PrettyString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.0)
}
}
macro_rules! assert_eq {
($left:expr, $right:expr) => {
pretty_assertions::assert_eq!(PrettyString($left), PrettyString($right));
};
}
pub fn common<G: Generator>(gen: G, app: &mut App, name: &str, fixture: &str) {
let mut buf = vec![];
generate(gen, app, name, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert_eq!(&string, fixture);
}