mod common;
use assert_cmd::Command;
use std::time::Duration;
const PROC_TIMEOUT: Duration = Duration::from_secs(60);
fn version_line(flag: &str) -> String {
let output = common::hermetic(Command::cargo_bin("atomwrite").expect("binary"))
.timeout(PROC_TIMEOUT)
.arg(flag)
.output()
.expect("run atomwrite");
assert_eq!(output.status.code(), Some(0), "{flag} must exit 0");
assert!(output.stderr.is_empty(), "{flag} must not write to stderr");
String::from_utf8(output.stdout)
.expect("utf8 stdout")
.trim_end()
.to_string()
}
fn is_valid_source(source: &str) -> bool {
if source == "no-git" {
return true;
}
let sha = source.strip_suffix("-dirty").unwrap_or(source);
(4..=40).contains(&sha.len()) && sha.chars().all(|c| c.is_ascii_hexdigit())
}
#[test]
fn version_has_crate_version_source_and_target() {
let line = version_line("--version");
let rest = line
.strip_prefix("atomwrite ")
.unwrap_or_else(|| panic!("missing binary name prefix: {line:?}"));
let rest = rest
.strip_prefix(env!("CARGO_PKG_VERSION"))
.unwrap_or_else(|| panic!("missing crate version: {line:?}"));
let rest = rest
.strip_prefix(" (")
.unwrap_or_else(|| panic!("missing source group: {line:?}"));
let (source, target) = rest
.split_once(") ")
.unwrap_or_else(|| panic!("missing target after source: {line:?}"));
assert!(
is_valid_source(source),
"invalid build source {source:?} in {line:?}"
);
assert!(
!target.is_empty() && !target.contains(' '),
"invalid target {target:?} in {line:?}"
);
}
#[test]
fn build_source_is_never_unknown() {
let line = version_line("--version");
assert!(
!line.contains("(unknown)"),
"build source must not say unknown: {line:?}"
);
}
#[test]
fn short_flag_matches_long_flag() {
assert_eq!(version_line("-V"), version_line("--version"));
}