Skip to content

Commit

Permalink
Clean up run_test to only return sorted list of failures with adjuste…
Browse files Browse the repository at this point in the history
…d line numbers
  • Loading branch information
pilleye committed Oct 21, 2024
1 parent 5fbecdc commit c2172a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
27 changes: 14 additions & 13 deletions crates/red_knot_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ruff_db::system::{DbWithTestSystem, SystemPathBuf};
use ruff_source_file::OneIndexed;
use std::path::PathBuf;

type Failures = Vec<(AbsoluteLineNumberPath, matcher::FailuresByLine)>;
type Failures = Vec<matcher::FailuresByLine>;

mod assertion;
mod db;
Expand All @@ -32,16 +32,12 @@ pub fn run(path: &PathBuf, title: &str) {
for test in suite.tests() {
if let Err(failures) = run_test(&test) {
any_failures = true;
println!("\n{}\n", test.name().bold().underline());
println!("\n{}", test.name().bold().underline());

for (contextual_path, by_line) in failures {
println!("{}", contextual_path.path.as_str().bold());
for by_line in failures {
for (line_number, failures) in by_line.iter() {
for failure in failures {
let absolute_line_number = contextual_path
.starting_line_number
.saturating_add(line_number.try_into().unwrap());
let line_info = format!("{title}:{absolute_line_number}").cyan();
let line_info = format!("{title}:{line_number}").cyan();

println!(" {line_info} {failure}");
}
Expand Down Expand Up @@ -82,7 +78,8 @@ fn run_test(test: &parser::MarkdownTest) -> Result<(), Failures> {
});
}

let mut failures = vec![];
let mut failures = Vec::with_capacity(paths.len());
paths.sort_by(|a, b| a.starting_line_number.cmp(&b.starting_line_number));

for contextual_path in paths {
let file = system_path_to_file(&db, contextual_path.path.clone()).unwrap();
Expand All @@ -97,14 +94,18 @@ fn run_test(test: &parser::MarkdownTest) -> Result<(), Failures> {
parsed.errors()
);

matcher::match_file(&db, file, check_types(&db, file)).unwrap_or_else(|line_failures| {
failures.push((contextual_path, line_failures));
});
match matcher::match_file(&db, file, check_types(&db, file)) {
Ok(()) => {}
Err(line_failures) => {
failures.push(line_failures.offset_errors(contextual_path.starting_line_number));
}
}
}

if failures.is_empty() {
Ok(())
} else {
failures.sort_by(|(a, _), (b, _)| a.path.cmp(&b.path));
failures.shrink_to_fit();
Err(failures)
}
}
14 changes: 14 additions & 0 deletions crates/red_knot_test/src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl FailuresByLine {
fn is_empty(&self) -> bool {
self.lines.is_empty()
}

pub(crate) fn offset_errors(self, offset: OneIndexed) -> Self {
Self {
failures: self.failures,
lines: self
.lines
.into_iter()
.map(|line_failures| LineFailures {
line_number: line_failures.line_number.saturating_add(offset.get()),
range: line_failures.range,
})
.collect(),
}
}
}

#[derive(Debug)]
Expand Down

0 comments on commit c2172a1

Please sign in to comment.