#![allow(clippy::expect_used)]
use std::io::Read as _;
use std::process::{Command, Stdio};
use std::sync::mpsc;
use std::time::Duration;
fn run_with_timeout(arg: &str) -> Option<(i32, String)> {
let mut child = Command::new(env!("CARGO_BIN_EXE_csaf-crud"))
.arg(arg)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.expect("failed to spawn csaf-crud");
let (tx, rx) = mpsc::channel();
std::thread::spawn(move || {
let status = child.wait();
let mut stdout = String::new();
if let Some(mut out) = child.stdout.take() {
let _ = out.read_to_string(&mut stdout);
}
let _ = tx.send((status, stdout));
});
match rx.recv_timeout(Duration::from_secs(30)) {
Ok((Ok(status), stdout)) => Some((status.code().unwrap_or(-1), stdout)),
Ok((Err(_), _)) | Err(_) => None,
}
}
#[test]
fn help_long_flag_prints_usage_and_exits_promptly() {
let (code, stdout) = run_with_timeout("--help")
.expect("csaf-crud --help hung instead of printing usage and exiting");
assert_eq!(code, 0);
assert!(stdout.contains("USAGE:"));
assert!(stdout.contains("csaf-crud"));
}
#[test]
fn help_short_flag_prints_usage_and_exits_promptly() {
let (code, stdout) =
run_with_timeout("-h").expect("csaf-crud -h hung instead of printing usage and exiting");
assert_eq!(code, 0);
assert!(stdout.contains("USAGE:"));
}
#[test]
fn version_long_flag_prints_version_and_exits_promptly() {
let (code, stdout) = run_with_timeout("--version")
.expect("csaf-crud --version hung instead of printing the version and exiting");
assert_eq!(code, 0);
assert!(stdout.contains(env!("CARGO_PKG_VERSION")));
}
#[test]
fn version_short_flag_prints_version_and_exits_promptly() {
let (code, stdout) = run_with_timeout("-V")
.expect("csaf-crud -V hung instead of printing the version and exiting");
assert_eq!(code, 0);
assert!(stdout.contains(env!("CARGO_PKG_VERSION")));
}