use clap::CommandFactory;
use super::output::FORMAT_ARGS_ABOUT;
fn collect_leaves<'a>(
cmd: &'a clap::Command,
prefix: &mut Vec<String>,
out: &mut Vec<(String, &'a clap::Command)>,
) {
let subs: Vec<&clap::Command> = cmd
.get_subcommands()
.filter(|c| c.get_name() != "help")
.collect();
if subs.is_empty() {
out.push((prefix.join(" "), cmd));
return;
}
for sub in subs {
prefix.push(sub.get_name().to_owned());
collect_leaves(sub, prefix, out);
prefix.pop();
}
}
#[test]
fn every_leaf_has_a_real_about() {
let cmd = super::Cli::command();
let mut leaves = Vec::new();
collect_leaves(&cmd, &mut Vec::new(), &mut leaves);
let offenders: Vec<String> = leaves
.iter()
.filter_map(|(path, leaf)| {
let about = leaf.get_about().map(|a| a.to_string());
match about.as_deref() {
None => Some(format!("{path}: about is missing")),
Some("") => Some(format!("{path}: about is empty")),
Some(text) if text == FORMAT_ARGS_ABOUT => {
Some(format!("{path}: about bled from FormatArgs's doc comment"))
}
_ => None,
}
})
.collect();
assert!(
offenders.is_empty(),
"leaves with a missing/empty/bled --help about:\n{}",
offenders.join("\n")
);
}