use std::{ fs::File, io::{BufRead, BufReader}, process::Command, }; use similar_asserts::assert_eq; use walkdir::{DirEntry, WalkDir}; #[test] fn links_to_html_spec_use_multipage_version() { for entry in WalkDir::new(".") .min_depth(1) .into_iter() .filter_entry(is_source_file) .flatten() { if !entry.file_type().is_file() { continue; } let actual = match std::fs::read_to_string(entry.path()) { Ok(content) => content, Err(err) => panic!("invalid UTF-8 in file content: {:?}: {}", entry.path(), err), }; let expected = actual.replace( concat!("://html.spec.whatwg.org/", "#"), concat!("://html.spec.whatwg.org/multipage/???.html#"), ); assert_eq!( actual, expected, "Found a link to the one-page version of the HTML spec, which is huge and takes long to load. We want to link the multipage version instead." ); } } fn is_source_file(entry: &DirEntry) -> bool { let Some(filename) = entry.file_name().to_str() else { panic!("invalid UTF-8 in filename: {:?}", entry.path()) }; if entry.depth() == 1 && filename == "target" || filename == "Cargo.lock" { return false; // cargo files } if filename == "html5lib-tests" { return false; // git submodule } !filename.starts_with('.') // .git, etc. } #[test] fn example_output_in_readme() { let output = Command::new("cargo") .args(["run", "--example", "spans"]) .output() .unwrap() .stdout; let expected = std::str::from_utf8(&output) .unwrap() .trim_end() .lines() .map(|s| s.trim_end().to_string()) .collect::>() .join("\n"); let actual = BufReader::new(File::open("README.md").unwrap()) .lines() .flatten() .skip_while(|l| l != "```output id=spans") .skip(1) .take_while(|l| l != "```") .collect::>() .join("\n"); assert_eq!(actual, expected); }