clap/tests/examples.rs
Ed Page bd25b5f615 fix(yaml): Don't panic on multiple groups
Because we gradually build the `ArgGroup` as we parse the YAML, we don't
use `ArgGroup::new`.  Clap3 introduced an internal `id` in addition to
the public `name` and it appears that this custom initialization code
was not updated.

This shows the problem with publically exposing `impl Default`.
Choices:
- Remove `impl Default`
  - Always valid
  - Requires spreading invariants
  - Callers can't implement code the same way we do
- Add `ArgGroup::name`
  - Can be constructed in an invalid state
  - Centralizes invariants
  - A caller could implement code like the yaml logic

I decided to go with `ArgGroup::name`.

Fixes #2719
2021-08-18 15:16:44 -05:00

54 lines
1.4 KiB
Rust

#![cfg(not(tarpaulin))]
use std::ffi::OsStr;
use std::fs;
use std::process::{Command, Output};
fn run_example<S: AsRef<str>>(name: S, args: &[&str]) -> Output {
let mut all_args = vec![
"run",
"--example",
name.as_ref(),
"--features",
"yaml",
"--",
];
all_args.extend_from_slice(args);
Command::new(env!("CARGO"))
.args(all_args)
.output()
.expect("failed to run example")
}
#[test]
fn examples_are_functional() {
let example_paths = fs::read_dir("examples")
.expect("couldn't read examples directory")
.map(|result| result.expect("couldn't get directory entry").path())
.filter(|path| path.is_file() && path.extension().and_then(OsStr::to_str) == Some("rs"));
let mut example_count = 0;
for path in example_paths {
example_count += 1;
let example_name = path
.file_stem()
.and_then(OsStr::to_str)
.expect("unable to determine example name");
let help_output = run_example(example_name, &["--help"]);
assert!(
help_output.status.success(),
"{} --help exited with nonzero: {}",
example_name,
String::from_utf8_lossy(&help_output.stderr),
);
assert!(
!help_output.stdout.is_empty(),
"{} --help had no output",
example_name,
);
}
assert!(example_count > 0);
}