use super::{BuildIdentity, UNKNOWN};
fn is_commit_hash(value: &str) -> bool {
value.len() == 40 && value.bytes().all(|byte| byte.is_ascii_hexdigit())
}
#[test]
fn the_binary_knows_what_revision_it_is() {
let identity = BuildIdentity::current();
assert_eq!(
identity.version,
env!("CARGO_PKG_VERSION"),
"the version must be this crate's own, never a copy that can drift"
);
assert!(
is_commit_hash(identity.commit) || identity.commit == UNKNOWN,
"the commit must be a full hash or the honest absence, got `{}` — a \
truncated or fabricated value is worse than no value, because it reads \
exactly like an answer",
identity.commit
);
assert!(
matches!(identity.dirty, "true" | "false" | UNKNOWN),
"dirty must be one of three stated states, got `{}`",
identity.dirty
);
}
#[test]
fn a_build_from_a_repository_stamps_a_real_commit() {
let identity = BuildIdentity::current();
if identity.commit == UNKNOWN {
tracing::info!(
"skipped: this binary was built with no reachable git repository, which is a \
supported build and the stamped `unknown` is the correct answer for it"
);
return;
}
assert!(
is_commit_hash(identity.commit),
"a stamped commit must be a full 40-character hash, got `{}`",
identity.commit
);
assert_ne!(
identity.built_at, UNKNOWN,
"a build that could reach git could also read a clock; `unknown` here means the \
epoch stamp did not survive the boundary"
);
assert!(
chrono::DateTime::parse_from_rfc3339(&identity.built_at).is_ok(),
"built_at must be RFC 3339, got `{}`",
identity.built_at
);
}
#[test]
fn the_stamped_commit_is_the_commit_that_is_checked_out() {
let identity = BuildIdentity::current();
let Some(head) = head_commit() else {
tracing::info!(
"skipped: git is not reachable from the test, so there is no independent \
answer to compare the stamp against"
);
return;
};
assert_eq!(
identity.commit, head,
"the binary claims a different revision than the tree it was built from — a \
stale stamp is indistinguishable from a current one to every reader, which is \
the whole reason the build script registers rerun triggers"
);
}
fn head_commit() -> Option<String> {
let output = std::process::Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output()
.ok()?;
if !output.status.success() {
return None;
}
let text = String::from_utf8(output.stdout).ok()?;
Some(text.trim().to_owned())
}
#[test]
fn the_banner_line_names_the_version_and_the_commit() {
let identity = BuildIdentity::current();
let line = identity.line();
assert!(line.contains(identity.version), "{line}");
assert!(line.contains(identity.commit), "{line}");
assert!(line.contains(&identity.built_at), "{line}");
}
#[test]
fn the_banner_line_marks_a_dirty_tree_and_only_a_dirty_tree() {
let dirty = BuildIdentity {
version: "0.0.0",
commit: "0123456789abcdef0123456789abcdef01234567",
dirty: "true",
built_at: String::from("1970-01-01T00:00:00+00:00"),
};
let clean = BuildIdentity {
dirty: "false",
..dirty.clone()
};
let unestablished = BuildIdentity {
dirty: UNKNOWN,
..dirty.clone()
};
assert!(dirty.line().contains("-dirty"), "{}", dirty.line());
assert!(!clean.line().contains("-dirty"), "{}", clean.line());
assert!(
!unestablished.line().contains("-dirty"),
"{}",
unestablished.line()
);
}