Skip to content

Commit

Permalink
[red-knot] Use colors to improve readability of mdtest output (#13725)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Oct 13, 2024
1 parent 46bc69d commit defdc4d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/red_knot_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
12 changes: 8 additions & 4 deletions crates/red_knot_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,20 +32,23 @@ 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!();
}
}
}

println!("{}\n", "-".repeat(50));

assert!(!any_failures, "Some tests failed.");
}

Expand Down
16 changes: 11 additions & 5 deletions crates/red_knot_test/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -155,7 +156,7 @@ trait UnmatchedWithColumn {

impl Unmatched for Assertion<'_> {
fn unmatched(&self) -> String {
format!("unmatched assertion: {self}")
format!("{} {self}", "unmatched assertion:".red())
}
}

Expand All @@ -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())
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit defdc4d

Please sign in to comment.