Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 40 additions & 30 deletions crates/ty_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use ty_static::EnvVars;
/// Run `path` as a markdown test suite with given `title`.
///
/// Panic on test failure, and print failure details.
#[expect(clippy::print_stdout)]
pub fn run(
absolute_fixture_path: &Utf8Path,
relative_fixture_path: &Utf8Path,
Expand All @@ -55,6 +54,7 @@ pub fn run(

let filter = std::env::var(EnvVars::MDTEST_TEST_FILTER).ok();
let mut any_failures = false;
let mut assertion = String::new();
for test in suite.tests() {
if filter
.as_ref()
Expand All @@ -79,7 +79,7 @@ pub fn run(
any_failures = any_failures || this_test_failed;

if this_test_failed && output_format.is_cli() {
println!("\n{}\n", test.name().bold().underline());
let _ = writeln!(assertion, "\n\n{}\n", test.name().bold().underline());
}

if let Err(failures) = result {
Expand All @@ -95,26 +95,29 @@ pub fn run(
OutputFormat::GitHub => absolute_fixture_path.as_str(),
};

let absolute_line_number = match source_map
.to_absolute_line_number(relative_line_number)
{
Ok(line_number) => line_number,
Err(last_line_number) => {
print!("{}",
output_format.display_error(
file,
last_line_number,
"Found a trailing assertion comment (e.g., `# revealed:` or `# error:`) \
not followed by any statement."
)
);

continue;
}
};
let absolute_line_number =
match source_map.to_absolute_line_number(relative_line_number) {
Ok(line_number) => line_number,
Err(last_line_number) => {
let _ = writeln!(
assertion,
"{}",
output_format.display_error(
file,
last_line_number,
"Found a trailing assertion comment \
(e.g., `# revealed:` or `# error:`) \
not followed by any statement."
)
);

continue;
}
};

for failure in failures {
print!(
let _ = writeln!(
assertion,
"{}",
output_format.display_error(file, absolute_line_number, failure)
);
Expand All @@ -128,31 +131,38 @@ pub fn run(
match output_format {
OutputFormat::Cli => {
let info = relative_fixture_path.to_string().cyan();
println!(" {info} {inconsistency}");
let _ = writeln!(assertion, " {info} {inconsistency}");
}
OutputFormat::GitHub => {
println!("::error file={absolute_fixture_path}::{inconsistency}");
let _ = writeln!(
assertion,
"::error file={absolute_fixture_path}::{inconsistency}"
);
}
}
}
}

if this_test_failed && output_format.is_cli() {
let escaped_test_name = test.name().replace('\'', "\\'");
println!(
"\nTo rerun this specific test, set the environment variable: {}='{escaped_test_name}'",
let _ = writeln!(
assertion,
"\nTo rerun this specific test, \
set the environment variable: {}='{escaped_test_name}'",
EnvVars::MDTEST_TEST_FILTER,
);
println!(
"{}='{escaped_test_name}' cargo test -p ty_python_semantic --test mdtest -- {test_name}",
let _ = writeln!(
assertion,
"{}='{escaped_test_name}' cargo test -p ty_python_semantic \
--test mdtest -- {test_name}",
EnvVars::MDTEST_TEST_FILTER,
);

println!("\n{}\n", "-".repeat(50));
let _ = writeln!(assertion, "\n{}", "-".repeat(50));
}
}

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

Ok(())
}
Expand Down Expand Up @@ -195,14 +205,14 @@ impl OutputFormat {

match format {
OutputFormat::Cli => {
writeln!(
write!(
f,
" {file_line} {failure}",
file_line = format!("{file}:{line}").cyan()
)
}
OutputFormat::GitHub => {
writeln!(f, "::error file={file},line={line}::{failure}")
write!(f, "::error file={file},line={line}::{failure}")
}
}
}
Expand Down
Loading