Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 91505a4

Browse files
Run mergeable doctest as part of standalone doctests if there is only one
1 parent 4358284 commit 91505a4

File tree

6 files changed

+70
-48
lines changed

6 files changed

+70
-48
lines changed

src/librustdoc/doctest.rs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -267,50 +267,53 @@ pub(crate) fn run_tests(
267267
if doctests.is_empty() {
268268
continue;
269269
}
270-
doctests.sort_by(|(_, a), (_, b)| a.name.cmp(&b.name));
270+
// If there is only one mergeable doctest, the cost to run it would be higher than just
271+
// running it alonside standalone doctests.
272+
if doctests.len() > 1 {
273+
doctests.sort_by(|(_, a), (_, b)| a.name.cmp(&b.name));
271274

272-
let mut tests_runner = runner::DocTestRunner::new();
275+
let mut tests_runner = runner::DocTestRunner::new();
273276

274-
let rustdoc_test_options = IndividualTestOptions::new(
275-
&rustdoc_options,
276-
&Some(format!("merged_doctest_{edition}")),
277-
PathBuf::from(format!("doctest_{edition}.rs")),
278-
);
277+
let rustdoc_test_options = IndividualTestOptions::new(
278+
&rustdoc_options,
279+
&Some(format!("merged_doctest_{edition}")),
280+
PathBuf::from(format!("doctest_{edition}.rs")),
281+
);
279282

280-
for (doctest, scraped_test) in &doctests {
281-
tests_runner.add_test(doctest, scraped_test, &target_str);
282-
}
283-
if let Ok(success) = tests_runner.run_merged_tests(
284-
rustdoc_test_options,
285-
edition,
286-
&opts,
287-
&test_args,
288-
rustdoc_options,
289-
) {
290-
ran_edition_tests += 1;
291-
if !success {
292-
nb_errors += 1;
283+
for (doctest, scraped_test) in &doctests {
284+
tests_runner.add_test(doctest, scraped_test, &target_str);
285+
}
286+
if let Ok(success) = tests_runner.run_merged_tests(
287+
rustdoc_test_options,
288+
edition,
289+
&opts,
290+
&test_args,
291+
rustdoc_options,
292+
) {
293+
ran_edition_tests += 1;
294+
if !success {
295+
nb_errors += 1;
296+
}
297+
continue;
293298
}
294-
continue;
295-
} else {
296299
// We failed to compile all compatible tests as one so we push them into the
297300
// `standalone_tests` doctests.
298301
debug!("Failed to compile compatible doctests for edition {} all at once", edition);
299-
for (doctest, scraped_test) in doctests {
300-
doctest.generate_unique_doctest(
301-
&scraped_test.text,
302-
scraped_test.langstr.test_harness,
303-
&opts,
304-
Some(&opts.crate_name),
305-
);
306-
standalone_tests.push(generate_test_desc_and_fn(
307-
doctest,
308-
scraped_test,
309-
opts.clone(),
310-
Arc::clone(&rustdoc_options),
311-
unused_extern_reports.clone(),
312-
));
313-
}
302+
}
303+
for (doctest, scraped_test) in doctests {
304+
doctest.generate_unique_doctest(
305+
&scraped_test.text,
306+
scraped_test.langstr.test_harness,
307+
&opts,
308+
Some(&opts.crate_name),
309+
);
310+
standalone_tests.push(generate_test_desc_and_fn(
311+
doctest,
312+
scraped_test,
313+
opts.clone(),
314+
Arc::clone(&rustdoc_options),
315+
unused_extern_reports.clone(),
316+
));
314317
}
315318
}
316319

tests/rustdoc-ui/2024-doctests-checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ compile-flags: --test --test-args=--test-threads=1 -Zunstable-options --edition 2024
33
//@ normalize-stdout-test: "tests/rustdoc-ui" -> "$$DIR"
44
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
5-
//@ normalize-stdout-test "wrong-ast.rs:\d+:\d+" -> "wrong-ast.rs:$$LINE:$$COL"
5+
//@ normalize-stdout-test ".rs:\d+:\d+" -> ".rs:$$LINE:$$COL"
66

77
/// This one should fail: crate attributes should remain crate attributes
88
/// in standalone doctests.

tests/rustdoc-ui/doctest/failed-doctest-should-panic.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
running 1 test
3-
test $DIR/failed-doctest-should-panic.rs - Foo (line 9) - should panic ... FAILED
3+
test $DIR/failed-doctest-should-panic.rs - Foo (line 9) ... FAILED
44

55
failures:
66

77
---- $DIR/failed-doctest-should-panic.rs - Foo (line 9) stdout ----
8-
note: test did not panic as expected
8+
Test executable succeeded, but it's marked `should_panic`.
99

1010
failures:
1111
$DIR/failed-doctest-should-panic.rs - Foo (line 9)

tests/rustdoc-ui/doctest/wrong-ast-2024.stdout

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11

2-
running 1 test
3-
test $DIR/wrong-ast-2024.rs - three (line 17) - should panic ... ok
4-
5-
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6-
7-
8-
running 2 tests
2+
running 3 tests
93
test $DIR/wrong-ast-2024.rs - one (line 7) ... FAILED
4+
test $DIR/wrong-ast-2024.rs - three (line 17) ... ok
105
test $DIR/wrong-ast-2024.rs - two (line 12) ... FAILED
116

127
failures:
@@ -37,5 +32,5 @@ failures:
3732
$DIR/wrong-ast-2024.rs - one (line 7)
3833
$DIR/wrong-ast-2024.rs - two (line 12)
3934

40-
test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
35+
test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
4136

tests/rustdoc-ui/run-as-standalone.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This test ensures that if there is only one mergeable doctest, then it is
2+
// instead run as part of standalone doctests.
3+
4+
//@ compile-flags:--test --test-args=--test-threads=1 -Zunstable-options --edition 2024
5+
//@ normalize-stdout-test: "tests/rustdoc-ui" -> "$$DIR"
6+
//@ normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
7+
//@ normalize-stdout-test ".rs:\d+:\d+" -> ".rs:$$LINE:$$COL"
8+
//@ check-pass
9+
10+
/// ```
11+
/// let x = 12;
12+
/// ```
13+
///
14+
/// ```compile_fail
15+
/// let y = x;
16+
/// ```
17+
pub fn one() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
running 2 tests
3+
test $DIR/run-as-standalone.rs - one (line 10) ... ok
4+
test $DIR/run-as-standalone.rs - one (line 14) - compile fail ... ok
5+
6+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
7+

0 commit comments

Comments
 (0)