use std::path::Path;
const DOCS_URL: &str = "https://github.com/lovelygenerator/pikopiko/blob/main/docs";
fn guide_copy(src: &str) -> String {
let src = src.replace("](../", &format!("]({DOCS_URL}/"));
let mut out = String::with_capacity(src.len());
let mut rest = src.as_str();
while let Some(i) = rest.find("](") {
out.push_str(&rest[..i + 2]);
rest = &rest[i + 2..];
let b = rest.as_bytes();
if b.len() >= 3 && b[0].is_ascii_digit() && b[1].is_ascii_digit() && b[2] == b'-' {
out.push_str(DOCS_URL);
out.push_str("/guide/");
}
}
out.push_str(rest);
out
}
#[test]
fn guide_copies_match_the_docs() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
for (doc, copy) in [("04-mml.md", "mml.md"), ("10-ai.md", "ai.md")] {
let Ok(want) = std::fs::read_to_string(root.join("../../docs/guide").join(doc)) else {
return;
};
let have = std::fs::read_to_string(root.join("guide").join(copy)).unwrap();
assert!(
guide_copy(&want) == have,
"docs/guide/{doc} と guide/{copy} が違う。`scripts/release.sh sync` で写し直す"
);
}
}
#[test]
fn copies_have_no_relative_links() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
for copy in ["mml.md", "ai.md"] {
let text = std::fs::read_to_string(root.join("guide").join(copy)).unwrap();
for (i, _) in text.match_indices("](") {
let target = &text[i + 2..];
assert!(
target.starts_with("http") || target.starts_with('#'),
"guide/{copy} に相対リンクが残っている: {}",
&target[..target.len().min(40)]
);
}
}
}