Skip to content

Commit e3768b9

Browse files
committed
make individual errors in a failed test more clearly visible and more consistent
1 parent f05e9d1 commit e3768b9

File tree

2 files changed

+103
-85
lines changed

2 files changed

+103
-85
lines changed

src/status_emitter.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717
};
1818
use std::{
1919
collections::HashMap,
20-
fmt::{Debug, Write as _},
20+
fmt::{Debug, Display, Write as _},
2121
io::Write as _,
2222
num::NonZeroUsize,
2323
panic::RefUnwindSafe,
@@ -243,17 +243,14 @@ impl TestStatus for TextTest {
243243
stderr: &'a [u8],
244244
stdout: &'a [u8],
245245
) -> Box<dyn Debug + 'a> {
246+
let mut path = self.path.display().to_string();
247+
if !self.revision.is_empty() {
248+
write!(path, " (revision `{}`)", self.revision).unwrap();
249+
}
250+
let text = format!("{} {path}", "FAILED TEST:".red());
251+
246252
println!();
247-
let path = self.path.display().to_string();
248-
print!("{}", path.underline().bold());
249-
let revision = if self.revision.is_empty() {
250-
String::new()
251-
} else {
252-
format!(" (revision `{}`)", self.revision)
253-
};
254-
print!("{revision}");
255-
print!(" {}", "FAILED:".red().bold());
256-
println!();
253+
println!("{}", text.bold().underline());
257254
println!("command: {cmd:?}");
258255
println!();
259256

@@ -264,10 +261,10 @@ impl TestStatus for TextTest {
264261
}
265262
impl<'a> Drop for Guard<'a> {
266263
fn drop(&mut self) {
267-
println!("full stderr:");
264+
println!("{}", "full stderr:".bold());
268265
std::io::stdout().write_all(self.stderr).unwrap();
269266
println!();
270-
println!("full stdout:");
267+
println!("{}", "full stdout:".bold());
271268
std::io::stdout().write_all(self.stdout).unwrap();
272269
println!();
273270
println!();
@@ -405,16 +402,27 @@ impl StatusEmitter for Text {
405402
}
406403

407404
fn print_error(error: &Error, path: &Path) {
405+
/// Every error starts with a header like that, to make them all easy to find.
406+
/// It is made to look like the headers printed for spanned errors.
407+
fn print_error_header(msg: impl Display) {
408+
let text = format!("{} {msg}", "error:".red());
409+
println!("{}", text.bold());
410+
}
411+
408412
match error {
409413
Error::ExitStatus {
410414
mode,
411415
status,
412416
expected,
413417
} => {
414-
println!("{mode} test got {status}, but expected {expected}")
418+
// `status` prints as `exit status: N`.
419+
print_error_header(format_args!(
420+
"{mode} test got {status}, but expected {expected}"
421+
))
415422
}
416423
Error::Command { kind, status } => {
417-
println!("{kind} failed with {status}");
424+
// `status` prints as `exit status: N`.
425+
print_error_header(format_args!("{kind} failed with {status}"));
418426
}
419427
Error::PatternNotFound {
420428
pattern,
@@ -432,6 +440,7 @@ fn print_error(error: &Error, path: &Path) {
432440
format!("`/{r}/` does not match diagnostics {line}",)
433441
}
434442
};
443+
// This will print a suitable error header.
435444
create_error(
436445
msg,
437446
&[(
@@ -442,7 +451,7 @@ fn print_error(error: &Error, path: &Path) {
442451
);
443452
}
444453
Error::NoPatternsFound => {
445-
println!("{}", "no error patterns found in fail test".red());
454+
print_error_header("no error patterns found in fail test");
446455
}
447456
Error::PatternFoundInPassTest { mode, span } => {
448457
let annot = [("expected because of this annotation", Some(*span))];
@@ -451,6 +460,7 @@ fn print_error(error: &Error, path: &Path) {
451460
if let Some(mode) = mode {
452461
lines.push((&annot, mode.line_start))
453462
}
463+
// This will print a suitable error header.
454464
create_error("error pattern found in pass test", &lines, path);
455465
}
456466
Error::OutputDiffers {
@@ -459,7 +469,7 @@ fn print_error(error: &Error, path: &Path) {
459469
expected,
460470
bless_command,
461471
} => {
462-
println!("{}", "actual output differed from expected".underline());
472+
print_error_header("actual output differed from expected");
463473
println!(
464474
"Execute `{}` to update `{}` to the actual output",
465475
bless_command,
@@ -483,6 +493,7 @@ fn print_error(error: &Error, path: &Path) {
483493
.iter()
484494
.map(|msg| (format!("{:?}: {}", msg.level, msg.message), msg.line_col))
485495
.collect::<Vec<_>>();
496+
// This will print a suitable error header.
486497
create_error(
487498
format!("There were {} unmatched diagnostics", msgs.len()),
488499
&[(
@@ -495,10 +506,10 @@ fn print_error(error: &Error, path: &Path) {
495506
path,
496507
);
497508
} else {
498-
println!(
499-
"There were {} unmatched diagnostics that occurred outside the testfile and had no pattern",
509+
print_error_header(format_args!(
510+
"there were {} unmatched diagnostics that occurred outside the testfile and had no pattern",
500511
msgs.len(),
501-
);
512+
));
502513
for Message {
503514
level,
504515
message,
@@ -510,10 +521,12 @@ fn print_error(error: &Error, path: &Path) {
510521
}
511522
}
512523
Error::InvalidComment { msg, span } => {
524+
// This will print a suitable error header.
513525
create_error(msg, &[(&[("", Some(*span))], span.line_start)], path)
514526
}
515527
Error::MultipleRevisionsWithResults { kind, lines } => {
516528
let title = format!("multiple {kind} found");
529+
// This will print a suitable error header.
517530
create_error(
518531
title,
519532
&lines
@@ -524,23 +537,28 @@ fn print_error(error: &Error, path: &Path) {
524537
)
525538
}
526539
Error::Bug(msg) => {
527-
println!("A bug in `ui_test` occurred: {msg}");
540+
print_error_header("a bug in `ui_test` occurred");
541+
println!("{msg}");
528542
}
529543
Error::Aux {
530544
path: aux_path,
531545
errors,
532546
line,
533547
} => {
534-
println!("Aux build from {}:{line} failed", path.display());
548+
print_error_header(format_args!(
549+
"aux build from {}:{line} failed",
550+
path.display()
551+
));
535552
for error in errors {
536553
print_error(error, aux_path);
537554
}
538555
}
539556
Error::Rustfix(error) => {
540-
println!(
541-
"failed to apply suggestions for {} with rustfix: {error}",
557+
print_error_header(format_args!(
558+
"failed to apply suggestions for {} with rustfix",
542559
path.display()
543-
);
560+
));
561+
println!("{error}");
544562
println!("Add //@no-rustfix to the test file to ignore rustfix suggestions");
545563
}
546564
}

0 commit comments

Comments
 (0)