use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn binary() -> PathBuf {
if let Ok(p) = std::env::var("CARGO_BIN_EXE_markdown-reader") {
return p.into();
}
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("debug")
.join("markdown-reader")
}
#[test]
fn check_links_exits_zero_when_all_links_valid() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
fs::write(root.join("target.md"), "# Target Section\n\nContent.\n").unwrap();
fs::write(
root.join("source.md"),
"# Source\n\n[cross](./target.md#target-section)\n\n[anchor](#source)\n",
)
.unwrap();
let output = Command::new(binary())
.args(["--check-links", &root.to_string_lossy()])
.output()
.expect("failed to run binary");
assert!(
output.status.success(),
"expected exit 0 for valid links; exit={}\nstdout={}",
output.status,
String::from_utf8_lossy(&output.stdout),
);
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(
stdout.contains("All links OK"),
"expected 'All links OK' in output:\n{stdout}"
);
}
#[test]
fn check_links_exits_one_and_reports_broken_link() {
let dir = tempfile::tempdir().expect("tempdir");
let root = dir.path();
fs::write(root.join("target.md"), "# Real Section\n\nContent.\n").unwrap();
fs::write(
root.join("source.md"),
"# Source\n\n[bad cross](./target.md#does-not-exist)\n\n[bad anchor](#nowhere)\n",
)
.unwrap();
let output = Command::new(binary())
.args(["--check-links", &root.to_string_lossy()])
.output()
.expect("failed to run binary");
assert_eq!(
output.status.code(),
Some(1),
"expected exit code 1 for broken links;\nstdout={}",
String::from_utf8_lossy(&output.stdout),
);
let stdout = String::from_utf8(output.stdout).unwrap();
assert!(
stdout.contains("#does-not-exist") || stdout.contains("does-not-exist"),
"expected broken cross-file anchor in output:\n{stdout}"
);
assert!(
stdout.contains("#nowhere") || stdout.contains("nowhere"),
"expected broken same-file anchor in output:\n{stdout}"
);
assert!(
stdout.contains("broken link"),
"expected 'broken link' summary in output:\n{stdout}"
);
}