Skip to content

Commit 92b77b2

Browse files
authored
Rollup merge of rust-lang#144909 - GuillaumeGomez:extend-libtest-for-merged-doctests, r=Amanieu
Add new `test::print_merged_doctests_times` used by rustdoc to display more detailed time information This PR is the first commit of rust-lang#144908. Sadly, it seems like we can't do all of it at once... r? `@Amanieu`
2 parents 6199b5b + 75e20af commit 92b77b2

File tree

7 files changed

+76
-11
lines changed

7 files changed

+76
-11
lines changed

library/test/src/console.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,23 +281,15 @@ fn on_test_event(
281281
Ok(())
282282
}
283283

284-
/// A simple console test runner.
285-
/// Runs provided tests reporting process and results to the stdout.
286-
pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<bool> {
284+
pub(crate) fn get_formatter(opts: &TestOpts, max_name_len: usize) -> Box<dyn OutputFormatter> {
287285
let output = match term::stdout() {
288286
None => OutputLocation::Raw(io::stdout()),
289287
Some(t) => OutputLocation::Pretty(t),
290288
};
291289

292-
let max_name_len = tests
293-
.iter()
294-
.max_by_key(|t| len_if_padded(t))
295-
.map(|t| t.desc.name.as_slice().len())
296-
.unwrap_or(0);
297-
298290
let is_multithreaded = opts.test_threads.unwrap_or_else(get_concurrency) > 1;
299291

300-
let mut out: Box<dyn OutputFormatter> = match opts.format {
292+
match opts.format {
301293
OutputFormat::Pretty => Box::new(PrettyFormatter::new(
302294
output,
303295
opts.use_color(),
@@ -310,7 +302,19 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
310302
}
311303
OutputFormat::Json => Box::new(JsonFormatter::new(output)),
312304
OutputFormat::Junit => Box::new(JunitFormatter::new(output)),
313-
};
305+
}
306+
}
307+
308+
/// A simple console test runner.
309+
/// Runs provided tests reporting process and results to the stdout.
310+
pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<bool> {
311+
let max_name_len = tests
312+
.iter()
313+
.max_by_key(|t| len_if_padded(t))
314+
.map(|t| t.desc.name.as_slice().len())
315+
.unwrap_or(0);
316+
317+
let mut out = get_formatter(opts, max_name_len);
314318
let mut st = ConsoleTestState::new(opts)?;
315319

316320
// Prevent the usage of `Instant` in some cases:

library/test/src/formatters/json.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
215215

216216
Ok(state.failed == 0)
217217
}
218+
219+
fn write_merged_doctests_times(
220+
&mut self,
221+
total_time: f64,
222+
compilation_time: f64,
223+
) -> io::Result<()> {
224+
let newline = "\n";
225+
self.writeln_message(&format!(
226+
r#"{{ "type": "report", "total_time": {total_time}, "compilation_time": {compilation_time} }}{newline}"#,
227+
))
228+
}
218229
}
219230

220231
/// A formatting utility used to print strings with characters in need of escaping.

library/test/src/formatters/junit.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ impl<T: Write> OutputFormatter for JunitFormatter<T> {
182182

183183
Ok(state.failed == 0)
184184
}
185+
186+
fn write_merged_doctests_times(
187+
&mut self,
188+
total_time: f64,
189+
compilation_time: f64,
190+
) -> io::Result<()> {
191+
self.write_message(&format!(
192+
"<report total_time=\"{total_time}\" compilation_time=\"{compilation_time}\"></report>\n",
193+
))
194+
}
185195
}
186196

187197
fn parse_class_name(desc: &TestDesc) -> (String, String) {

library/test/src/formatters/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ pub(crate) trait OutputFormatter {
3333
state: &ConsoleTestState,
3434
) -> io::Result<()>;
3535
fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool>;
36+
fn write_merged_doctests_times(
37+
&mut self,
38+
total_time: f64,
39+
compilation_time: f64,
40+
) -> io::Result<()>;
3641
}
3742

3843
pub(crate) fn write_stderr_delimiter(test_output: &mut Vec<u8>, test_name: &TestName) {

library/test/src/formatters/pretty.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,14 @@ impl<T: Write> OutputFormatter for PrettyFormatter<T> {
303303

304304
Ok(success)
305305
}
306+
307+
fn write_merged_doctests_times(
308+
&mut self,
309+
total_time: f64,
310+
compilation_time: f64,
311+
) -> io::Result<()> {
312+
self.write_plain(format!(
313+
"all doctests ran in {total_time:.2}s; merged doctests compilation took {compilation_time:.2}s\n",
314+
))
315+
}
306316
}

library/test/src/formatters/terse.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,14 @@ impl<T: Write> OutputFormatter for TerseFormatter<T> {
295295

296296
Ok(success)
297297
}
298+
299+
fn write_merged_doctests_times(
300+
&mut self,
301+
total_time: f64,
302+
compilation_time: f64,
303+
) -> io::Result<()> {
304+
self.write_plain(format!(
305+
"all doctests ran in {total_time:.2}s; merged doctests compilation took {compilation_time:.2}s\n",
306+
))
307+
}
298308
}

library/test/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,21 @@ fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn {
244244
}
245245
}
246246

247+
/// Public API used by rustdoc to display the `total` and `compilation` times in the expected
248+
/// format.
249+
pub fn print_merged_doctests_times(args: &[String], total_time: f64, compilation_time: f64) {
250+
let opts = match cli::parse_opts(args) {
251+
Some(Ok(o)) => o,
252+
Some(Err(msg)) => {
253+
eprintln!("error: {msg}");
254+
process::exit(ERROR_EXIT_CODE);
255+
}
256+
None => return,
257+
};
258+
let mut formatter = console::get_formatter(&opts, 0);
259+
formatter.write_merged_doctests_times(total_time, compilation_time).unwrap();
260+
}
261+
247262
/// Invoked when unit tests terminate. Returns `Result::Err` if the test is
248263
/// considered a failure. By default, invokes `report()` and checks for a `0`
249264
/// result.

0 commit comments

Comments
 (0)