2020-02-04 08:10:53 +00:00
|
|
|
#![allow(unused_imports, dead_code)]
|
|
|
|
|
|
|
|
use std::io::{Cursor, Write};
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
use clap::{arg, Arg, ArgGroup, Command};
|
2020-02-04 08:10:53 +00:00
|
|
|
|
2021-10-15 21:06:21 +00:00
|
|
|
pub fn compare<S, S2>(l: S, r: S2) -> bool
|
2020-02-04 08:10:53 +00:00
|
|
|
where
|
|
|
|
S: AsRef<str>,
|
|
|
|
S2: AsRef<str>,
|
|
|
|
{
|
|
|
|
let re = Regex::new("\x1b[^m]*m").unwrap();
|
|
|
|
// Strip out any mismatching \r character on windows that might sneak in on either side
|
2022-01-04 07:20:03 +00:00
|
|
|
let ls = l.as_ref().replace('\r', "");
|
|
|
|
let rs = r.as_ref().replace('\r', "");
|
2021-10-11 22:01:33 +00:00
|
|
|
let left_ = re.replace_all(&*ls, "");
|
2020-02-04 08:10:53 +00:00
|
|
|
let right = re.replace_all(&*rs, "");
|
2021-10-11 22:01:33 +00:00
|
|
|
let b = left_ == right;
|
2020-02-04 08:10:53 +00:00
|
|
|
if !b {
|
2021-10-11 22:01:33 +00:00
|
|
|
dbg!(&left_);
|
|
|
|
dbg!(&right);
|
2020-02-04 08:10:53 +00:00
|
|
|
println!();
|
|
|
|
println!("--> left");
|
2021-10-11 22:01:33 +00:00
|
|
|
println!("{}", left_);
|
2020-02-04 08:10:53 +00:00
|
|
|
println!("--> right");
|
|
|
|
println!("{}", right);
|
|
|
|
println!("--")
|
|
|
|
}
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
pub fn compare_output(l: Command, args: &str, right: &str, stderr: bool) -> bool {
|
2020-02-04 08:10:53 +00:00
|
|
|
let mut buf = Cursor::new(Vec::with_capacity(50));
|
|
|
|
let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
|
|
|
|
let err = res.unwrap_err();
|
2020-04-12 01:39:13 +00:00
|
|
|
write!(&mut buf, "{}", err).unwrap();
|
2020-02-04 08:10:53 +00:00
|
|
|
let content = buf.into_inner();
|
|
|
|
let left = String::from_utf8(content).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
stderr,
|
|
|
|
err.use_stderr(),
|
|
|
|
"Should Use STDERR failed. Should be {} but is {}",
|
|
|
|
stderr,
|
|
|
|
err.use_stderr()
|
|
|
|
);
|
|
|
|
compare(left, right)
|
|
|
|
}
|
|
|
|
|
2020-07-06 14:26:53 +00:00
|
|
|
// Legacy tests from the python script days
|
2020-02-04 08:10:53 +00:00
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
pub fn complex_app() -> Command<'static> {
|
2020-02-04 08:10:53 +00:00
|
|
|
let opt3_vals = ["fast", "slow"];
|
|
|
|
let pos3_vals = ["vi", "emacs"];
|
|
|
|
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("clap-test")
|
2020-02-04 08:10:53 +00:00
|
|
|
.version("v1.4.8")
|
|
|
|
.about("tests clap library")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(
|
|
|
|
-o --option <opt> "tests options"
|
|
|
|
)
|
|
|
|
.required(false)
|
|
|
|
.multiple_values(true)
|
|
|
|
.multiple_occurrences(true),
|
|
|
|
)
|
|
|
|
.arg(arg!([positional] "tests positionals"))
|
|
|
|
.arg(arg!(-f --flag ... "tests flags").global(true))
|
2020-02-04 08:10:53 +00:00
|
|
|
.args(&[
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(flag2: -F "tests flags with exclusions")
|
2020-02-04 08:10:53 +00:00
|
|
|
.conflicts_with("flag")
|
|
|
|
.requires("long-option-2"),
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!(--"long-option-2" <option2> "tests long options with exclusions")
|
|
|
|
.required(false)
|
2020-02-04 08:10:53 +00:00
|
|
|
.conflicts_with("option")
|
|
|
|
.requires("positional2"),
|
2021-11-19 20:33:11 +00:00
|
|
|
arg!([positional2] "tests positionals with exclusions"),
|
|
|
|
arg!(-O --option3 <option3> "specific vals")
|
|
|
|
.required(false)
|
|
|
|
.possible_values(opt3_vals),
|
|
|
|
arg!([positional3] ... "tests specific values").possible_values(pos3_vals),
|
|
|
|
arg!(--multvals "Tests multiple values, not mult occs")
|
|
|
|
.value_names(&["one", "two"])
|
|
|
|
.required(false),
|
|
|
|
arg!(--multvalsmo ... "Tests multiple values, and mult occs")
|
|
|
|
.value_names(&["one", "two"])
|
|
|
|
.required(false),
|
|
|
|
arg!(--minvals2 <minvals> "Tests 2 min vals")
|
|
|
|
.required(false)
|
|
|
|
.min_values(2),
|
|
|
|
arg!(--maxvals3 <maxvals> "Tests 3 max vals")
|
|
|
|
.required(false)
|
|
|
|
.max_values(3),
|
2022-01-26 15:23:35 +00:00
|
|
|
arg!(--optvaleq <optval> "Tests optional value, require = sign")
|
|
|
|
.required(false)
|
|
|
|
.min_values(0)
|
|
|
|
.number_of_values(1)
|
|
|
|
.require_equals(true),
|
|
|
|
arg!(--optvalnoeq <optval> "Tests optional value")
|
|
|
|
.required(false)
|
|
|
|
.min_values(0)
|
|
|
|
.number_of_values(1),
|
2020-02-04 08:10:53 +00:00
|
|
|
])
|
|
|
|
.subcommand(
|
2022-02-12 03:48:29 +00:00
|
|
|
Command::new("subcmd")
|
2020-02-04 08:10:53 +00:00
|
|
|
.about("tests subcommands")
|
|
|
|
.version("0.1")
|
|
|
|
.author("Kevin K. <kbknapp@gmail.com>")
|
2021-11-19 20:33:11 +00:00
|
|
|
.arg(
|
|
|
|
arg!(-o --option <scoption> "tests options")
|
|
|
|
.required(false)
|
|
|
|
.multiple_values(true),
|
|
|
|
)
|
|
|
|
.arg(arg!(-s --subcmdarg <subcmdarg> "tests other args").required(false))
|
|
|
|
.arg(arg!([scpositional] "tests positionals")),
|
2020-02-04 08:10:53 +00:00
|
|
|
)
|
|
|
|
}
|