aboutsummaryrefslogtreecommitdiff
path: root/tests/misc.rs
blob: 0db0606a475b78355bb610f1f0ee64a167232eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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::<Vec<_>>()
        .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::<Vec<_>>()
        .join("\n");

    assert_eq!(actual, expected);
}