[red-knot] Add line number to mdtest panic message about language tag mismatch (#16906)

<!--
Thank you for contributing to Ruff! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

Fixes #16898 

## Test Plan

Update test for lang mismatch panic
This commit is contained in:
Matthew Mckee 2025-03-22 12:05:31 +00:00 committed by GitHub
parent e4f5fe8cf7
commit 7b86f54c4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -617,8 +617,9 @@ impl<'s> Parser<'s> {
.extension()
.is_none_or(|extension| extension.eq_ignore_ascii_case(expected_extension))
{
let backtick_start = self.line_index(backtick_offsets.0);
bail!(
"File extension of test file path `{explicit_path}` in test `{test_name}` does not match language specified `{lang}` of code block"
"File extension of test file path `{explicit_path}` in test `{test_name}` does not match language specified `{lang}` of code block at line `{backtick_start}`"
);
}
}
@ -750,6 +751,10 @@ impl<'s> Parser<'s> {
fn offset(&self) -> TextSize {
self.source_len - self.cursor.text_len()
}
fn line_index(&self, char_index: TextSize) -> u32 {
self.source.count_lines(TextRange::up_to(char_index))
}
}
#[cfg(test)]
@ -1221,7 +1226,7 @@ mod tests {
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"File extension of test file path `a.py` in test `Accidental stub` does not match language specified `pyi` of code block"
"File extension of test file path `a.py` in test `Accidental stub` does not match language specified `pyi` of code block at line `5`"
);
}