Skip to content

Commit e435e32

Browse files
authored
Rollup merge of #86156 - ehuss:linkchecker-fixes, r=Mark-Simulacrum
Fix a bug in the linkchecker There was a small typo in the linkchecker (in #85652) that caused it to report a `#` fragment link error pointing to the wrong file (it was displaying the path to the source file, not the target of the link). This also includes a few other changes: - Fixes the tests due to some changes in the redirect handling in #84703. - Adds the tests to rustbuild to run whenever the linkchecker itself is run. - Updates the tests to validate more of the output (so that a mistake like this would have been caught). Closes #86144
2 parents e7a4f1e + 1e9f0b3 commit e435e32

File tree

8 files changed

+70
-8
lines changed

8 files changed

+70
-8
lines changed

src/bootstrap/builder/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -613,9 +613,14 @@ mod dist {
613613
// Note that the stages here are +1 than what they actually are because
614614
// Rustdoc::run swaps out the compiler with stage minus 1 if --stage is
615615
// not 0.
616+
//
617+
// The stage 0 copy is the one downloaded for bootstrapping. It is
618+
// (currently) needed to run "cargo test" on the linkchecker, and
619+
// should be relatively "free".
616620
assert_eq!(
617621
first(builder.cache.all::<tool::Rustdoc>()),
618622
&[
623+
tool::Rustdoc { compiler: Compiler { host: a, stage: 0 } },
619624
tool::Rustdoc { compiler: Compiler { host: a, stage: 1 } },
620625
tool::Rustdoc { compiler: Compiler { host: a, stage: 2 } },
621626
]

src/bootstrap/test.rs

+17
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,25 @@ You can skip linkcheck with --exclude src/tools/linkchecker"
124124

125125
builder.info(&format!("Linkcheck ({})", host));
126126

127+
// Test the linkchecker itself.
128+
let bootstrap_host = builder.config.build;
129+
let compiler = builder.compiler(0, bootstrap_host);
130+
let cargo = tool::prepare_tool_cargo(
131+
builder,
132+
compiler,
133+
Mode::ToolBootstrap,
134+
bootstrap_host,
135+
"test",
136+
"src/tools/linkchecker",
137+
SourceType::InTree,
138+
&[],
139+
);
140+
try_run(builder, &mut cargo.into());
141+
142+
// Build all the default documentation.
127143
builder.default_doc(&[]);
128144

145+
// Run the linkchecker.
129146
let _time = util::timeit(&builder);
130147
try_run(
131148
builder,

src/tools/linkchecker/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl Checker {
347347
} else {
348348
report.errors += 1;
349349
print!("{}:{}: broken link fragment ", pretty_path, i + 1);
350-
println!("`#{}` pointing to `{}`", fragment, pretty_path);
350+
println!("`#{}` pointing to `{}`", fragment, target_pretty_path);
351351
};
352352
}
353353
});

src/tools/linkchecker/tests/broken_redir/redir-bad.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta http-equiv="refresh" content="0;URL=sometarget">
5+
<title>Redirection</title>
56
</head>
67
<body>
78
<p>Redirecting to <a href="sometarget">sometarget</a>...</p>

src/tools/linkchecker/tests/checks.rs

+43-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn run(dirname: &str) -> (ExitStatus, String, String) {
1515
fn broken_test(dirname: &str, expected: &str) {
1616
let (status, stdout, stderr) = run(dirname);
1717
assert!(!status.success());
18-
if !stdout.contains(expected) {
18+
if !contains(expected, &stdout) {
1919
panic!(
2020
"stdout did not contain expected text: {}\n\
2121
--- stdout:\n\
@@ -27,6 +27,25 @@ fn broken_test(dirname: &str, expected: &str) {
2727
}
2828
}
2929

30+
fn contains(expected: &str, actual: &str) -> bool {
31+
// Normalize for Windows paths.
32+
let actual = actual.replace('\\', "/");
33+
actual.lines().any(|mut line| {
34+
for (i, part) in expected.split("[..]").enumerate() {
35+
match line.find(part) {
36+
Some(j) => {
37+
if i == 0 && j != 0 {
38+
return false;
39+
}
40+
line = &line[j + part.len()..];
41+
}
42+
None => return false,
43+
}
44+
}
45+
line.is_empty() || expected.ends_with("[..]")
46+
})
47+
}
48+
3049
fn valid_test(dirname: &str) {
3150
let (status, stdout, stderr) = run(dirname);
3251
if !status.success() {
@@ -48,30 +67,47 @@ fn valid() {
4867

4968
#[test]
5069
fn basic_broken() {
51-
broken_test("basic_broken", "bar.html");
70+
broken_test("basic_broken", "foo.html:3: broken link - `bar.html`");
5271
}
5372

5473
#[test]
5574
fn broken_fragment_local() {
56-
broken_test("broken_fragment_local", "#somefrag");
75+
broken_test(
76+
"broken_fragment_local",
77+
"foo.html:3: broken link fragment `#somefrag` pointing to `foo.html`",
78+
);
5779
}
5880

5981
#[test]
6082
fn broken_fragment_remote() {
61-
broken_test("broken_fragment_remote/inner", "#somefrag");
83+
broken_test(
84+
"broken_fragment_remote/inner",
85+
"foo.html:3: broken link fragment `#somefrag` pointing to \
86+
`[..]/broken_fragment_remote/bar.html`",
87+
);
6288
}
6389

6490
#[test]
6591
fn broken_redir() {
66-
broken_test("broken_redir", "sometarget");
92+
broken_test(
93+
"broken_redir",
94+
"foo.html:3: broken redirect from `redir-bad.html` to `sometarget`",
95+
);
6796
}
6897

6998
#[test]
7099
fn directory_link() {
71-
broken_test("directory_link", "somedir");
100+
broken_test(
101+
"directory_link",
102+
"foo.html:3: directory link to `somedir` (directory links should use index.html instead)",
103+
);
72104
}
73105

74106
#[test]
75107
fn redirect_loop() {
76-
broken_test("redirect_loop", "redir-bad.html");
108+
broken_test(
109+
"redirect_loop",
110+
"foo.html:3: redirect from `redir-bad.html` to `[..]redirect_loop/redir-bad.html` \
111+
which is also a redirect (not supported)",
112+
);
77113
}

src/tools/linkchecker/tests/redirect_loop/redir-bad.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta http-equiv="refresh" content="0;URL=redir-bad.html">
5+
<title>Redirection</title>
56
</head>
67
<body>
78
<p>Redirecting to <a href="redir-bad.html">redir-bad.html</a>...</p>

src/tools/linkchecker/tests/valid/inner/redir-bad.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta http-equiv="refresh" content="0;URL=xxx">
5+
<title>Redirection</title>
56
</head>
67
<body>
78
<p>Redirecting to <a href="xxx">xxx</a>...</p>

src/tools/linkchecker/tests/valid/inner/redir.html

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta http-equiv="refresh" content="0;URL=redir-target.html">
5+
<title>Redirection</title>
56
</head>
67
<body>
78
<p>Redirecting to <a href="redir-target.html">redir-target.html</a>...</p>

0 commit comments

Comments
 (0)