From 22d7ece91d625ed2e673d0f44402f224b25c5335 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 26 Nov 2024 15:40:02 +0100 Subject: [PATCH] [red-knot] Fix unit tests in release mode (#14604) ## Summary This is about the easiest patch that I can think of. It has a drawback in that there is no real guarantee this won't happen again. I think this might be acceptable, given that all of this is a temporary thing. And we also add a new CI job to prevent regressions like this in the future. For the record though, I'm listing alternative approaches I thought of: - We could get rid of the debug/release distinction and just add `@Todo` type metadata everywhere. This has possible affects on runtime. The main reason I didn't follow through with this is that the size of `Type` increases. We would either have to adapt the `assert_eq_size!` test or get rid of it. Even if we add messages everywhere and get rid of the file-and-line-variant in the enum, it's not enough to get back to the current release-mode size of `Type`. - We could generally discard `@Todo` meta information when using it in tests. I think this would be a huge drawback. I like that we can have the actual messages in the mdtest. And make sure we get the expected `@Todo` type, not just any `@Todo`. It's also helpful when debugging tests. closes #14594 ## Test Plan ```rs cargo nextest run --release ``` (cherry picked from commit 0e71c9e3bbd78fad878fa4d09126a7b305d06fe2) [Strip out unnecessary CI changes -- Eli] Signed-off-by: Eli Schwartz --- crates/red_knot_python_semantic/src/types/infer.rs | 12 ++++++++++-- crates/red_knot_test/src/matcher.rs | 13 +++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 853abbe26..25a4f80aa 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -5970,7 +5970,11 @@ mod tests { "src/a.py", &["foo", ""], "x", - "@Todo(async iterables/iterators)", + if cfg!(debug_assertions) { + "@Todo(async iterables/iterators)" + } else { + "@Todo" + }, ); Ok(()) @@ -6000,7 +6004,11 @@ mod tests { "src/a.py", &["foo", ""], "x", - "@Todo(async iterables/iterators)", + if cfg!(debug_assertions) { + "@Todo(async iterables/iterators)" + } else { + "@Todo" + }, ); Ok(()) diff --git a/crates/red_knot_test/src/matcher.rs b/crates/red_knot_test/src/matcher.rs index 6d8b0488c..5c8b7328e 100644 --- a/crates/red_knot_test/src/matcher.rs +++ b/crates/red_knot_test/src/matcher.rs @@ -180,6 +180,16 @@ where } } +/// Discard `@Todo`-type metadata from expected types, which is not available +/// when running in release mode. +#[cfg(not(debug_assertions))] +fn discard_todo_metadata(ty: &str) -> std::borrow::Cow<'_, str> { + static TODO_METADATA_REGEX: std::sync::LazyLock = + std::sync::LazyLock::new(|| regex::Regex::new(r"@Todo\([^)]*\)").unwrap()); + + TODO_METADATA_REGEX.replace_all(ty, "@Todo") +} + struct Matcher { line_index: LineIndex, source: SourceText, @@ -276,6 +286,9 @@ impl Matcher { } } Assertion::Revealed(expected_type) => { + #[cfg(not(debug_assertions))] + let expected_type = discard_todo_metadata(&expected_type); + let mut matched_revealed_type = None; let mut matched_undefined_reveal = None; let expected_reveal_type_message = format!("Revealed type is `{expected_type}`"); -- 2.45.2