[red-knot] Use colors to improve readability of mdtest output (#13725)

This commit is contained in:
Alex Waygood 2024-10-13 14:20:35 +01:00 committed by GitHub
parent 46bc69d1d4
commit defdc4dd8e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 9 deletions

1
Cargo.lock generated
View file

@ -2134,6 +2134,7 @@ name = "red_knot_test"
version = "0.0.0"
dependencies = [
"anyhow",
"colored",
"once_cell",
"red_knot_python_semantic",
"red_knot_vendored",

View file

@ -20,6 +20,7 @@ ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
anyhow = { workspace = true }
colored = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }

View file

@ -1,3 +1,4 @@
use colored::Colorize;
use parser as test_parser;
use red_knot_python_semantic::types::check_types;
use ruff_db::files::system_path_to_file;
@ -31,13 +32,14 @@ pub fn run(path: &PathBuf, title: &str) {
for test in suite.tests() {
if let Err(failures) = run_test(&test) {
any_failures = true;
println!("{}", test.name());
println!("\n{}\n", test.name().bold().underline());
for (path, by_line) in failures {
println!(" {path}");
for (line, failures) in by_line.iter() {
println!("{}", path.as_str().bold());
for (line_number, failures) in by_line.iter() {
for failure in failures {
println!(" line {line}: {failure}");
let line_info = format!("line {line_number}:").cyan();
println!(" {line_info} {failure}");
}
}
println!();
@ -45,6 +47,8 @@ pub fn run(path: &PathBuf, title: &str) {
}
}
println!("{}\n", "-".repeat(50));
assert!(!any_failures, "Some tests failed.");
}

View file

@ -3,6 +3,7 @@
use crate::assertion::{Assertion, ErrorAssertion, InlineFileAssertions};
use crate::db::Db;
use crate::diagnostic::SortedDiagnostics;
use colored::Colorize;
use red_knot_python_semantic::types::TypeCheckDiagnostic;
use ruff_db::files::File;
use ruff_db::source::{line_index, source_text, SourceText};
@ -155,7 +156,7 @@ trait UnmatchedWithColumn {
impl Unmatched for Assertion<'_> {
fn unmatched(&self) -> String {
format!("unmatched assertion: {self}")
format!("{} {self}", "unmatched assertion:".red())
}
}
@ -165,11 +166,11 @@ fn maybe_add_undefined_reveal_clarification<T: Diagnostic>(
) -> String {
if diagnostic.rule() == "undefined-reveal" {
format!(
"used built-in `reveal_type`: add a `# revealed` assertion on this line \
(original diagnostic: {original})"
"{} add a `# revealed` assertion on this line (original diagnostic: {original})",
"used built-in `reveal_type`:".yellow()
)
} else {
format!("unexpected error: {original}")
format!("{} {original}", "unexpected error:".red())
}
}
@ -232,7 +233,10 @@ impl Matcher {
..
})
) {
failures.push("invalid assertion: no rule or message text".to_string());
failures.push(format!(
"{} no rule or message text",
"invalid assertion:".red()
));
continue;
}
if !self.matches(assertion, &mut unmatched) {
@ -360,6 +364,8 @@ mod tests {
}
fn get_result(source: &str, diagnostics: Vec<TestDiagnostic>) -> Result<(), FailuresByLine> {
colored::control::set_override(false);
let mut db = crate::db::Db::setup(SystemPathBuf::from("/src"));
db.write_file("/src/test.py", source).unwrap();
let file = system_path_to_file(&db, "/src/test.py").unwrap();