Skip to content

Commit 3aefc4a

Browse files
committed
Improve compiletest expected/not found formatting
compiletest, oh compiletest, you are truly one of the tools in this repository. You're the omnipresent gatekeeper, ensuring that every new change works, doesn't break the world, and is nice. We thank you for your work, for your tests, for your test runs, for your features that help writing tests, for all the stability and and good you have caused. Without you, Rust wouldn't exist as it does, without you, nothing would work, without you, we would all go insane from having changes break and having to test them all by hand. Thank you, compiletest. but holy shit i fucking hate your stupid debug output so much i simply cannot take this anymore aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa By changing a few magic lines in this file called "runtest.rs", we can cause compiletest to emit nicer messages. This is widely regarded as a good thing. We stop wasting vertical space, allowing more errors to be displayed at once. Additionally, we add colors, which make it so much more pretty *and* gay, both of which are very good and useful. There's a bit of fuckery needed to get the colors to work. `colored` checks whether stdout is a terminal. We also print to stdout, so that works well. But.... for some stupid reason that I absolutely refuse to even attempt to debug, stdout is *not* a terminal when executing tests *in a terminal*. But stderr is >:). So this just checks whether stderr is a terminal. If you have a use case where you dump compiletest stdout into a place where colors are not supported while having stderr be a terminal, then I'm sorry for you, but you are gonna get colors and you're gonna like it. Stop it with the usual environment variable, which `colored` also respects by default.
1 parent a83cf56 commit 3aefc4a

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/tools/compiletest/src/errors.rs

+12
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ pub struct Error {
5757
pub msg: String,
5858
}
5959

60+
impl Error {
61+
pub fn render_for_expected(&self) -> String {
62+
use colored::Colorize;
63+
format!(
64+
"{: <10}line {: >3}: {}",
65+
self.kind.map(|kind| kind.to_string()).unwrap_or_default().to_uppercase(),
66+
self.line_num,
67+
self.msg.cyan(),
68+
)
69+
}
70+
}
71+
6072
#[derive(PartialEq, Debug)]
6173
enum WhichLine {
6274
ThisLine,

src/tools/compiletest/src/main.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
use std::{env, sync::Arc};
1+
use std::{env, io::IsTerminal, sync::Arc};
22

33
use compiletest::{common::Mode, log_config, parse_config, run_tests};
44

55
fn main() {
66
tracing_subscriber::fmt::init();
77

8+
// colored checks stdout by default, but for some reason only stderr is a terminal.
9+
// compiletest *does* print many things to stdout, but it doesn't really matter.
10+
if std::io::stderr().is_terminal()
11+
&& matches!(std::env::var("NO_COLOR").as_deref(), Err(_) | Ok("0"))
12+
{
13+
colored::control::set_override(true);
14+
}
15+
816
let config = Arc::new(parse_config(env::args().collect()));
917

1018
if config.valgrind_path.is_none() && config.force_valgrind {

src/tools/compiletest/src/runtest.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crate::json;
1717
use crate::read2::{read2_abbreviated, Truncated};
1818
use crate::util::{add_dylib_path, dylib_env_var, logv, PathBufExt};
1919
use crate::ColorConfig;
20+
use colored::Colorize;
2021
use miropt_test_tools::{files_for_miropt_test, MiroptTest, MiroptTestFile};
2122
use regex::{Captures, Regex};
2223
use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
23-
2424
use std::collections::{HashMap, HashSet};
2525
use std::env;
2626
use std::ffi::{OsStr, OsString};
@@ -1493,14 +1493,22 @@ impl<'test> TestCx<'test> {
14931493
unexpected.len(),
14941494
not_found.len()
14951495
));
1496-
println!("status: {}\ncommand: {}", proc_res.status, proc_res.cmdline);
1496+
println!("status: {}\ncommand: {}\n", proc_res.status, proc_res.cmdline);
14971497
if !unexpected.is_empty() {
1498-
println!("unexpected errors (from JSON output): {:#?}\n", unexpected);
1498+
println!("{}", "--- unexpected errors (from JSON output) ---".green());
1499+
for error in &unexpected {
1500+
println!("{}", error.render_for_expected());
1501+
}
1502+
println!("{}", "---".green());
14991503
}
15001504
if !not_found.is_empty() {
1501-
println!("not found errors (from test file): {:#?}\n", not_found);
1505+
println!("{}", "--- not found errors (from test file) ---".red());
1506+
for error in &not_found {
1507+
println!("{}", error.render_for_expected());
1508+
}
1509+
println!("{}", "---\n".red());
15021510
}
1503-
panic!();
1511+
panic!("errors differ from expected");
15041512
}
15051513
}
15061514

0 commit comments

Comments
 (0)