From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- src/tools/linkchecker/tests/checks.rs | 113 ++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/tools/linkchecker/tests/checks.rs (limited to 'src/tools/linkchecker/tests/checks.rs') diff --git a/src/tools/linkchecker/tests/checks.rs b/src/tools/linkchecker/tests/checks.rs new file mode 100644 index 000000000..1a0b1b00e --- /dev/null +++ b/src/tools/linkchecker/tests/checks.rs @@ -0,0 +1,113 @@ +use std::path::Path; +use std::process::{Command, ExitStatus}; + +fn run(dirname: &str) -> (ExitStatus, String, String) { + let output = Command::new(env!("CARGO_BIN_EXE_linkchecker")) + .current_dir(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests")) + .arg(dirname) + .output() + .unwrap(); + let stdout = String::from_utf8(output.stdout).unwrap(); + let stderr = String::from_utf8(output.stderr).unwrap(); + (output.status, stdout, stderr) +} + +fn broken_test(dirname: &str, expected: &str) { + let (status, stdout, stderr) = run(dirname); + assert!(!status.success()); + if !contains(expected, &stdout) { + panic!( + "stdout did not contain expected text: {}\n\ + --- stdout:\n\ + {}\n\ + --- stderr:\n\ + {}\n", + expected, stdout, stderr + ); + } +} + +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() { + panic!( + "test did not succeed as expected\n\ + --- stdout:\n\ + {}\n\ + --- stderr:\n\ + {}\n", + stdout, stderr + ); + } +} + +#[test] +fn valid() { + valid_test("valid/inner"); +} + +#[test] +fn basic_broken() { + broken_test("basic_broken", "foo.html:3: broken link - `bar.html`"); +} + +#[test] +fn broken_fragment_local() { + 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", + "foo.html:3: broken link fragment `#somefrag` pointing to \ + `[..]/broken_fragment_remote/bar.html`", + ); +} + +#[test] +fn broken_redir() { + broken_test( + "broken_redir", + "foo.html:3: broken redirect from `redir-bad.html` to `sometarget`", + ); +} + +#[test] +fn directory_link() { + 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", + "foo.html:3: redirect from `redir-bad.html` to `[..]redirect_loop/redir-bad.html` \ + which is also a redirect (not supported)", + ); +} -- cgit v1.2.3