Skip to content

Commit

Permalink
Change the linkchecker self-tests to validate more output.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 9, 2021
1 parent bbd0532 commit dbc8a1c
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/tools/linkchecker/tests/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn run(dirname: &str) -> (ExitStatus, String, String) {
fn broken_test(dirname: &str, expected: &str) {
let (status, stdout, stderr) = run(dirname);
assert!(!status.success());
if !stdout.contains(expected) {
if !contains(expected, &stdout) {
panic!(
"stdout did not contain expected text: {}\n\
--- stdout:\n\
Expand All @@ -27,6 +27,25 @@ fn broken_test(dirname: &str, expected: &str) {
}
}

fn contains(expected: &str, actual: &str) -> bool {
// Normalize for Windows paths.
let actual = actual.replace('\\', "/");
actual.lines().any(|mut line| {
for (i, part) in expected.split("[..]").enumerate() {
match line.find(part) {
Some(j) => {
if i == 0 && j != 0 {
return false;
}
line = &line[j + part.len()..];
}
None => return false,
}
}
line.is_empty() || expected.ends_with("[..]")
})
}

fn valid_test(dirname: &str) {
let (status, stdout, stderr) = run(dirname);
if !status.success() {
Expand All @@ -48,30 +67,46 @@ fn valid() {

#[test]
fn basic_broken() {
broken_test("basic_broken", "bar.html");
broken_test("basic_broken", "foo.html:3: broken link - `bar.html`");
}

#[test]
fn broken_fragment_local() {
broken_test("broken_fragment_local", "#somefrag");
broken_test(
"broken_fragment_local",
"foo.html:3: broken link fragment `#somefrag` pointing to `foo.html`",
);
}

#[test]
fn broken_fragment_remote() {
broken_test("broken_fragment_remote/inner", "#somefrag");
broken_test(
"broken_fragment_remote/inner",
"foo.html:3: broken link fragment `#somefrag` pointing to `foo.html`",
);
}

#[test]
fn broken_redir() {
broken_test("broken_redir", "sometarget");
broken_test(
"broken_redir",
"foo.html:3: broken redirect from `redir-bad.html` to `sometarget`",
);
}

#[test]
fn directory_link() {
broken_test("directory_link", "somedir");
broken_test(
"directory_link",
"foo.html:3: directory link to `somedir` (directory links should use index.html instead)",
);
}

#[test]
fn redirect_loop() {
broken_test("redirect_loop", "redir-bad.html");
broken_test(
"redirect_loop",
"foo.html:3: redirect from `redir-bad.html` to `[..]redirect_loop/redir-bad.html` \
which is also a redirect (not supported)",
);
}

0 comments on commit dbc8a1c

Please sign in to comment.