use std::process::{Command, Output};
fn ctrm(args: &[&str], no_color: bool) -> Option<Output> {
let mut command = Command::new(env!("CARGO_BIN_EXE_ctrm"));
command.args(args).current_dir(env!("CARGO_MANIFEST_DIR"));
if no_color {
command.env("NO_COLOR", "1");
} else {
command.env_remove("NO_COLOR");
}
command.output().ok()
}
#[test]
fn both_version_flags_name_the_crate_and_its_version() {
let want =
format!("{} {}\n", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
for flag in ["--version", "-V"] {
let got = ctrm(&[flag], false);
let code = got.as_ref().and_then(|o| o.status.code());
let text = got.map(|o| o.stdout).unwrap_or_default();
assert_eq!(code, Some(0), "{flag}");
assert_eq!(String::from_utf8_lossy(&text), want, "{flag}");
}
}
#[test]
fn no_color_changes_nothing() {
let plain = ctrm(&["sets"], false);
let asked = ctrm(&["sets"], true);
let code = asked.as_ref().and_then(|o| o.status.code());
assert_eq!(code, Some(0));
let bytes = |o: Option<Output>| o.map(|o| o.stdout).unwrap_or_default();
let (plain, asked) = (bytes(plain), bytes(asked));
assert!(!plain.is_empty());
assert_eq!(plain, asked);
}
#[test]
fn help_is_printed_to_stdout_and_exits_zero() {
let asks: [&[&str]; 5] = [
&["--help"],
&["-h"],
&["check", "--help"],
&["explain", "-h"],
&["fix", "--rule", "* asci", "--help"],
];
for words in asks {
let got = ctrm(words, false);
let code = got.as_ref().and_then(|o| o.status.code());
let text = got.map(|o| o.stdout).unwrap_or_default();
assert_eq!(code, Some(0), "{words:?}");
let text = String::from_utf8_lossy(&text);
assert!(is_page_for(words, &text), "{words:?}: {text}");
}
}
fn is_page_for(words: &[&str], text: &str) -> bool {
let verb = words.first().filter(|w| !w.starts_with('-'));
let page =
verb.map_or_else(|| "verbs:".to_owned(), |v| format!("ctrm {v}"));
text.contains(&page) && text.contains("exit codes")
}
#[test]
fn no_arguments_and_a_help_that_is_not_a_flag_stay_errors() {
let asks: [&[&str]; 2] = [&[], &["sets", "--", "--help"]];
for words in asks {
let got = ctrm(words, false);
let code = got.as_ref().and_then(|o| o.status.code());
let text = got.map(|o| o.stdout).unwrap_or_default();
assert_eq!(code, Some(2), "{words:?}");
assert!(text.is_empty(), "{words:?}");
}
}
#[test]
fn a_shape_flag_that_would_be_ignored_is_refused() {
let asks: [&[&str]; 3] = [
&["check", "--format", "sarif", "--summary"],
&["check", "--format", "json", "--max", "3"],
&["check", "--max", "lots"],
];
for words in asks {
let code = ctrm(words, false).and_then(|o| o.status.code());
assert_eq!(code, Some(2), "{words:?}");
}
}
#[test]
fn a_json_run_that_fails_says_so_in_json() {
let asks: [(&[&str], &str); 2] = [
(&["check", "--format", "json", "--stirct"], "check"),
(&["stats", "--rule", "* asci", "--format", "json"], "stats"),
];
for (words, verb) in asks {
let got = ctrm(words, false);
let code = got.as_ref().and_then(|o| o.status.code());
let text = got.map(|o| o.stdout).unwrap_or_default();
let text = String::from_utf8_lossy(&text);
let head = format!("{{\"schema\":1,\"verb\":\"{verb}\",\"error\":\"");
assert_eq!(code, Some(2), "{words:?}");
assert!(text.starts_with(&head), "{words:?}: {text}");
}
let plain = ctrm(&["check", "--stirct"], false);
assert!(plain.map(|o| o.stdout).unwrap_or_default().is_empty());
}
fn refused(words: &[&str]) -> (Option<i32>, String) {
let got = ctrm(words, false);
let code = got.as_ref().and_then(|o| o.status.code());
let err = got.map(|o| o.stderr).unwrap_or_default();
(code, String::from_utf8_lossy(&err).into_owned())
}
#[test]
fn a_wrong_command_line_says_what_was_wrong() {
let (code, err) = refused(&["chek"]);
assert_eq!(code, Some(2));
assert!(
err.starts_with("ctrm: unknown verb 'chek' (did you mean 'check'?)\n")
);
assert_eq!(err.lines().count(), 2, "{err}");
for words in [&["--help", "--bogus"][..], &["check", "--help", "--bogus"]] {
let (code, err) = refused(words);
assert_eq!(code, Some(2), "{words:?}");
assert!(err.contains("--bogus"), "{words:?}: {err}");
}
}
#[test]
fn flags_may_precede_the_verb_and_guard_has_help() {
let got = ctrm(&["--no-color", "sets"], false);
assert_eq!(got.and_then(|o| o.status.code()), Some(0));
let got = ctrm(&["guard", "--help"], false);
let code = got.as_ref().and_then(|o| o.status.code());
let text = got.map(|o| o.stdout).unwrap_or_default();
assert_eq!(code, Some(0));
assert!(String::from_utf8_lossy(&text).contains("ctrm guard"));
}
#[test]
fn a_json_error_names_the_verb_after_leading_flags() {
let got = ctrm(&["--format", "json", "check", "--stirct"], false);
let text = got.map(|o| o.stdout).unwrap_or_default();
let text = String::from_utf8_lossy(&text);
assert!(
text.starts_with("{\"schema\":1,\"verb\":\"check\""),
"{text}"
);
}