use std::fs;
use std::path::{Path, PathBuf};
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn read(path: impl AsRef<Path>) -> String {
let path = repo_root().join(path);
fs::read_to_string(&path).unwrap_or_else(|e| panic!("cannot read {}: {e}", path.display()))
}
fn cargo_field(key: &str) -> String {
let manifest = read("Cargo.toml");
for line in manifest.lines() {
let line = line.trim();
if line.starts_with('[') && line != "[package]" {
break;
}
let Some((name, value)) = line.split_once('=') else { continue };
if name.trim() != key {
continue;
}
return value.trim().trim_matches('"').to_string();
}
panic!("`{key}` not found in the [package] block of Cargo.toml");
}
#[test]
fn readme_links_resolve_off_github() {
let readme = read("README.md");
for (line_no, line) in readme.lines().enumerate() {
for (marker, kind) in [("src=\"", "image"), ("](", "link")] {
let mut rest = line;
while let Some(at) = rest.find(marker) {
let target = &rest[at + marker.len()..];
let end = if marker == "](" { ')' } else { '"' };
let target = &target[..target.find(end).unwrap_or(target.len())];
let relative = !target.starts_with("http") && !target.starts_with('#') && !target.is_empty();
assert!(
!relative,
"README line {}: relative {kind} `{target}` breaks on crates.io; use an absolute URL",
line_no + 1
);
rest = &rest[at + marker.len()..];
}
}
}
}
#[test]
fn the_changelog_covers_the_version_being_shipped() {
let version = cargo_field("version");
let changelog = read("CHANGELOG.md");
let heading = format!("## [{version}]");
assert!(
changelog.contains(&heading),
"CHANGELOG.md has no `{heading}` section; run `git-cliff --tag v{version}` before tagging"
);
}
#[test]
fn the_readme_shows_the_version_being_shipped() {
let version = cargo_field("version");
let readme = read("README.md");
assert!(
readme.contains(&format!("\"version\":\"{version}\"")),
"README.md does not show version {version} in its transcript; re-run the server and paste the current output"
);
}
#[test]
fn readme_is_not_duplicated() {
for candidate in ["docs/README.md", "frontend/README.md"] {
let duplicate = repo_root().join(candidate);
assert!(
!duplicate.exists(),
"{candidate} exists; it will drift from the root README, which is the single source"
);
}
}