Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize linkchecker and add report. #85652

Merged
merged 3 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
545 changes: 318 additions & 227 deletions src/tools/linkchecker/main.rs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/basic_broken/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="bar.html">test</a>
</body>
</html>
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/broken_fragment_local/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="#somefrag">test</a>
</body>
</html>
4 changes: 4 additions & 0 deletions src/tools/linkchecker/tests/broken_fragment_remote/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="../bar.html#somefrag">test</a>
</body>
</html>
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/broken_redir/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="redir-bad.html">bad redir</a>
</body>
</html>
10 changes: 10 additions & 0 deletions src/tools/linkchecker/tests/broken_redir/redir-bad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=sometarget">
</head>
<body>
<p>Redirecting to <a href="sometarget">sometarget</a>...</p>
<script>location.replace("sometarget" + location.search + location.hash);</script>
</body>
</html>
77 changes: 77 additions & 0 deletions src/tools/linkchecker/tests/checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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 !stdout.contains(expected) {
panic!(
"stdout did not contain expected text: {}\n\
--- stdout:\n\
{}\n\
--- stderr:\n\
{}\n",
expected, stdout, stderr
);
}
}

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", "bar.html");
}

#[test]
fn broken_fragment_local() {
broken_test("broken_fragment_local", "#somefrag");
}

#[test]
fn broken_fragment_remote() {
broken_test("broken_fragment_remote/inner", "#somefrag");
}

#[test]
fn broken_redir() {
broken_test("broken_redir", "sometarget");
}

#[test]
fn directory_link() {
broken_test("directory_link", "somedir");
}

#[test]
fn redirect_loop() {
broken_test("redirect_loop", "redir-bad.html");
}
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/directory_link/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="somedir">dir link</a>
</body>
</html>
4 changes: 4 additions & 0 deletions src/tools/linkchecker/tests/directory_link/somedir/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html>
<body>
</body>
</html>
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/redirect_loop/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a href="redir-bad.html">loop link</a>
</body>
</html>
10 changes: 10 additions & 0 deletions src/tools/linkchecker/tests/redirect_loop/redir-bad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=redir-bad.html">
</head>
<body>
<p>Redirecting to <a href="redir-bad.html">redir-bad.html</a>...</p>
<script>location.replace("redir-bad.html" + location.search + location.hash);</script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/tools/linkchecker/tests/valid/inner/bar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>

<h2 id="barfrag">Bar</h2>

</body>
</html>
14 changes: 14 additions & 0 deletions src/tools/linkchecker/tests/valid/inner/foo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<body>
<a href="#localfrag">test local frag</a>
<a href="../outer.html">remote link</a>
<a href="../outer.html#somefrag">remote link with fragment</a>
<a href="bar.html">this book</a>
<a href="bar.html#barfrag">this book with fragment</a>
<a href="https://example.com/doesnotexist">external links not validated</a>
<a href="redir.html#redirfrag">Redirect</a>

<h2 id="localfrag">Local</h2>

</body>
</html>
11 changes: 11 additions & 0 deletions src/tools/linkchecker/tests/valid/inner/redir-bad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=xxx">
</head>
<body>
<p>Redirecting to <a href="xxx">xxx</a>...</p>
<script>location.replace("xxx" + location.search + location.hash);</script>
These files are skipped, but probably shouldn't be.
</body>
</html>
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/valid/inner/redir-target.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h2 id="redirfrag">Redir</h2>
</body>
</html>
10 changes: 10 additions & 0 deletions src/tools/linkchecker/tests/valid/inner/redir.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0;URL=redir-target.html">
</head>
<body>
<p>Redirecting to <a href="redir-target.html">redir-target.html</a>...</p>
<script>location.replace("redir-target.html" + location.search + location.hash);</script>
</body>
</html>
5 changes: 5 additions & 0 deletions src/tools/linkchecker/tests/valid/outer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<a id="somefrag"></a>
</body>
</html>