Skip to content

Commit 6ec439d

Browse files
authored
Rollup merge of rust-lang#144908 - GuillaumeGomez:fix-doctest-output-json, r=fmease
Fix doctest output json Fixes rust-lang#144798. Hopefully it will work with the new changes in `libtest` without needing to do both at once. This PR moves the `rustdoc` merged doctest extra information directly into `libtest` to ensure they share the same rendering to prevent the bug uncovered in rust-lang#144798. cc `@lolbinary` (as you reviewed the first PR) And since we're making changes to `libtest`: r? `@Amanieu`
2 parents ccb06f6 + 4a22063 commit 6ec439d

File tree

3 files changed

+95
-17
lines changed

3 files changed

+95
-17
lines changed

src/librustdoc/doctest.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::process::{self, Command, Stdio};
1212
use std::sync::atomic::{AtomicUsize, Ordering};
1313
use std::sync::{Arc, Mutex};
1414
use std::time::{Duration, Instant};
15-
use std::{fmt, panic, str};
15+
use std::{panic, str};
1616

1717
pub(crate) use make::{BuildDocTestBuilder, DocTestBuilder};
1818
pub(crate) use markdown::test as test_markdown;
@@ -60,24 +60,15 @@ impl MergedDoctestTimes {
6060
self.added_compilation_times += 1;
6161
}
6262

63-
fn display_times(&self) {
63+
/// Returns `(total_time, compilation_time)`.
64+
fn times_in_secs(&self) -> Option<(f64, f64)> {
6465
// If no merged doctest was compiled, then there is nothing to display since the numbers
6566
// displayed by `libtest` for standalone tests are already accurate (they include both
6667
// compilation and runtime).
67-
if self.added_compilation_times > 0 {
68-
println!("{self}");
68+
if self.added_compilation_times == 0 {
69+
return None;
6970
}
70-
}
71-
}
72-
73-
impl fmt::Display for MergedDoctestTimes {
74-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75-
write!(
76-
f,
77-
"all doctests ran in {:.2}s; merged doctests compilation took {:.2}s",
78-
self.total_time.elapsed().as_secs_f64(),
79-
self.compilation_time.as_secs_f64(),
80-
)
71+
Some((self.total_time.elapsed().as_secs_f64(), self.compilation_time.as_secs_f64()))
8172
}
8273
}
8374

@@ -400,15 +391,20 @@ pub(crate) fn run_tests(
400391
if ran_edition_tests == 0 || !standalone_tests.is_empty() {
401392
standalone_tests.sort_by(|a, b| a.desc.name.as_slice().cmp(b.desc.name.as_slice()));
402393
test::test_main_with_exit_callback(&test_args, standalone_tests, None, || {
394+
let times = times.times_in_secs();
403395
// We ensure temp dir destructor is called.
404396
std::mem::drop(temp_dir.take());
405-
times.display_times();
397+
if let Some((total_time, compilation_time)) = times {
398+
test::print_merged_doctests_times(&test_args, total_time, compilation_time);
399+
}
406400
});
407401
} else {
408402
// If the first condition branch exited successfully, `test_main_with_exit_callback` will
409403
// not exit the process. So to prevent displaying the times twice, we put it behind an
410404
// `else` condition.
411-
times.display_times();
405+
if let Some((total_time, compilation_time)) = times.times_in_secs() {
406+
test::print_merged_doctests_times(&test_args, total_time, compilation_time);
407+
}
412408
}
413409
// We ensure temp dir destructor is called.
414410
std::mem::drop(temp_dir);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! ```
2+
//! let x = 12;
3+
//! ```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Regression test to ensure that the output format is respected for doctests.
2+
//!
3+
//! Regression test for <https://github.com/rust-lang/rust/issues/144798>.
4+
5+
use run_make_support::{rustdoc, serde_json};
6+
7+
fn run_test(edition: &str, format: Option<&str>) -> String {
8+
let mut r = rustdoc();
9+
r.input("file.rs").edition(edition).arg("--test");
10+
if let Some(format) = format {
11+
r.args(&["--test-args", "-Zunstable-options"]).args(&[
12+
"--test-args",
13+
"--format",
14+
"--test-args",
15+
format,
16+
]);
17+
}
18+
r.run().stdout_utf8()
19+
}
20+
21+
fn check_json_output(edition: &str, expected_reports: usize) {
22+
let out = run_test(edition, Some("json"));
23+
let mut found_report = 0;
24+
for (line_nb, line) in out.lines().enumerate() {
25+
match serde_json::from_str::<serde_json::Value>(&line) {
26+
Ok(value) => {
27+
if value.get("type") == Some(&serde_json::json!("report")) {
28+
found_report += 1;
29+
}
30+
}
31+
Err(error) => panic!(
32+
"failed for {edition} edition (json format) at line {}: non-JSON value: {error}\n\
33+
====== output ======\n{out}",
34+
line_nb + 1,
35+
),
36+
}
37+
}
38+
if found_report != expected_reports {
39+
panic!(
40+
"failed for {edition} edition (json format): expected {expected_reports} doctest \
41+
time `report`, found {found_report}\n====== output ======\n{out}",
42+
);
43+
}
44+
}
45+
46+
fn check_non_json_output(edition: &str, expected_reports: usize) {
47+
let out = run_test(edition, None);
48+
let mut found_report = 0;
49+
for (line_nb, line) in out.lines().enumerate() {
50+
if line.starts_with('{') && serde_json::from_str::<serde_json::Value>(&line).is_ok() {
51+
panic!(
52+
"failed for {edition} edition: unexpected json at line {}: `{line}`\n\
53+
====== output ======\n{out}",
54+
line_nb + 1
55+
);
56+
}
57+
if line.starts_with("all doctests ran in")
58+
&& line.contains("; merged doctests compilation took ")
59+
{
60+
found_report += 1;
61+
}
62+
}
63+
if found_report != expected_reports {
64+
panic!(
65+
"failed for {edition} edition: expected {expected_reports} doctest time `report`, \
66+
found {found_report}\n====== output ======\n{out}",
67+
);
68+
}
69+
}
70+
71+
fn main() {
72+
// Only the merged doctests generate the "times report".
73+
check_json_output("2021", 0);
74+
check_json_output("2024", 1);
75+
76+
// Only the merged doctests generate the "times report".
77+
check_non_json_output("2021", 0);
78+
check_non_json_output("2024", 1);
79+
}

0 commit comments

Comments
 (0)