use std::process::Command;
use std::time::Duration;
const VERSION_TIMEOUT: Duration = Duration::from_secs(5);
fn run_with_arg(arg: &str) -> std::process::Output {
let mut child = Command::new(env!("CARGO_BIN_EXE_velesdb-memory"))
.arg(arg)
.env(
"VELESDB_MEMORY_PATH",
"/nonexistent/path/that/must/never/be/opened",
)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("failed to spawn velesdb-memory binary");
let start = std::time::Instant::now();
loop {
if let Some(status) = child.try_wait().expect("failed to poll child status") {
let output = child
.wait_with_output()
.expect("failed to collect child output after exit");
return std::process::Output { status, ..output };
}
if start.elapsed() > VERSION_TIMEOUT {
let _ = child.kill();
panic!("velesdb-memory {arg} did not exit within {VERSION_TIMEOUT:?} (still trying to open the store?)");
}
std::thread::sleep(Duration::from_millis(20));
}
}
#[test]
fn test_version_flag_long_prints_version_and_exits_zero() {
let output = run_with_arg("--version");
assert!(
output.status.success(),
"expected exit code 0, got {:?}; stderr: {}",
output.status.code(),
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).expect("stdout was not valid UTF-8");
let expected = format!("velesdb-memory {}\n", env!("CARGO_PKG_VERSION"));
assert_eq!(
stdout, expected,
"expected stdout to be exactly {expected:?}, got {stdout:?}"
);
}
#[test]
fn test_version_flag_short_prints_version_and_exits_zero() {
let output = run_with_arg("-V");
assert!(
output.status.success(),
"expected exit code 0, got {:?}; stderr: {}",
output.status.code(),
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8(output.stdout).expect("stdout was not valid UTF-8");
let expected = format!("velesdb-memory {}\n", env!("CARGO_PKG_VERSION"));
assert_eq!(
stdout, expected,
"expected stdout to be exactly {expected:?}, got {stdout:?}"
);
}