Skip to content

Commit 86ac0b4

Browse files
committed
Auto merge of rust-lang#85652 - ehuss:linkchecker-perf, r=Mark-Simulacrum
Optimize linkchecker and add report. This makes three changes to the linkchecker: * Adds a report displayed after it finishes. * Improves the performance by caching all filesystem access. The linkchecker can take over a minute to run on some systems, and this should make it about 2-3 times faster. * Added a few tests.
2 parents 9111b8a + b6532eb commit 86ac0b4

File tree

18 files changed

+505
-227
lines changed

18 files changed

+505
-227
lines changed

src/tools/linkchecker/main.rs

+318-227
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="bar.html">test</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="#somefrag">test</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html>
2+
<body>
3+
</body>
4+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="../bar.html#somefrag">test</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="redir-bad.html">bad redir</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="refresh" content="0;URL=sometarget">
5+
</head>
6+
<body>
7+
<p>Redirecting to <a href="sometarget">sometarget</a>...</p>
8+
<script>location.replace("sometarget" + location.search + location.hash);</script>
9+
</body>
10+
</html>

src/tools/linkchecker/tests/checks.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use std::path::Path;
2+
use std::process::{Command, ExitStatus};
3+
4+
fn run(dirname: &str) -> (ExitStatus, String, String) {
5+
let output = Command::new(env!("CARGO_BIN_EXE_linkchecker"))
6+
.current_dir(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests"))
7+
.arg(dirname)
8+
.output()
9+
.unwrap();
10+
let stdout = String::from_utf8(output.stdout).unwrap();
11+
let stderr = String::from_utf8(output.stderr).unwrap();
12+
(output.status, stdout, stderr)
13+
}
14+
15+
fn broken_test(dirname: &str, expected: &str) {
16+
let (status, stdout, stderr) = run(dirname);
17+
assert!(!status.success());
18+
if !stdout.contains(expected) {
19+
panic!(
20+
"stdout did not contain expected text: {}\n\
21+
--- stdout:\n\
22+
{}\n\
23+
--- stderr:\n\
24+
{}\n",
25+
expected, stdout, stderr
26+
);
27+
}
28+
}
29+
30+
fn valid_test(dirname: &str) {
31+
let (status, stdout, stderr) = run(dirname);
32+
if !status.success() {
33+
panic!(
34+
"test did not succeed as expected\n\
35+
--- stdout:\n\
36+
{}\n\
37+
--- stderr:\n\
38+
{}\n",
39+
stdout, stderr
40+
);
41+
}
42+
}
43+
44+
#[test]
45+
fn valid() {
46+
valid_test("valid/inner");
47+
}
48+
49+
#[test]
50+
fn basic_broken() {
51+
broken_test("basic_broken", "bar.html");
52+
}
53+
54+
#[test]
55+
fn broken_fragment_local() {
56+
broken_test("broken_fragment_local", "#somefrag");
57+
}
58+
59+
#[test]
60+
fn broken_fragment_remote() {
61+
broken_test("broken_fragment_remote/inner", "#somefrag");
62+
}
63+
64+
#[test]
65+
fn broken_redir() {
66+
broken_test("broken_redir", "sometarget");
67+
}
68+
69+
#[test]
70+
fn directory_link() {
71+
broken_test("directory_link", "somedir");
72+
}
73+
74+
#[test]
75+
fn redirect_loop() {
76+
broken_test("redirect_loop", "redir-bad.html");
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="somedir">dir link</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html>
2+
<body>
3+
</body>
4+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a href="redir-bad.html">loop link</a>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="refresh" content="0;URL=redir-bad.html">
5+
</head>
6+
<body>
7+
<p>Redirecting to <a href="redir-bad.html">redir-bad.html</a>...</p>
8+
<script>location.replace("redir-bad.html" + location.search + location.hash);</script>
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
4+
<h2 id="barfrag">Bar</h2>
5+
6+
</body>
7+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<body>
3+
<a href="#localfrag">test local frag</a>
4+
<a href="../outer.html">remote link</a>
5+
<a href="../outer.html#somefrag">remote link with fragment</a>
6+
<a href="bar.html">this book</a>
7+
<a href="bar.html#barfrag">this book with fragment</a>
8+
<a href="https://example.com/doesnotexist">external links not validated</a>
9+
<a href="redir.html#redirfrag">Redirect</a>
10+
11+
<h2 id="localfrag">Local</h2>
12+
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="refresh" content="0;URL=xxx">
5+
</head>
6+
<body>
7+
<p>Redirecting to <a href="xxx">xxx</a>...</p>
8+
<script>location.replace("xxx" + location.search + location.hash);</script>
9+
These files are skipped, but probably shouldn't be.
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h2 id="redirfrag">Redir</h2>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="refresh" content="0;URL=redir-target.html">
5+
</head>
6+
<body>
7+
<p>Redirecting to <a href="redir-target.html">redir-target.html</a>...</p>
8+
<script>location.replace("redir-target.html" + location.search + location.hash);</script>
9+
</body>
10+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<a id="somefrag"></a>
4+
</body>
5+
</html>

0 commit comments

Comments
 (0)