Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Error out when an mdtest code block is unterminated #14965

Merged
merged 5 commits into from
Dec 14, 2024
Merged
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions crates/red_knot_test/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,14 @@ static HEADER_RE: LazyLock<Regex> =
/// Matches a code block fenced by triple backticks, possibly with language and `key=val`
/// configuration items following the opening backticks (in the "tag string" of the code block).
static CODE_RE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^```(?<lang>(?-u:\w)+)?(?<config>(?: +\S+)*)\s*\n(?<code>(?:.|\n)*?)\n?```\s*\n?")
.unwrap()
Regex::new(
r"(?x)
^```(?<lang>(?-u:\w)+)?(?<config>(?:\x20+\S+)*)\s*\n
(?<code>(?:.|\n)*?)\n?
(?<end>```|\z)
",
)
.unwrap()
});

#[derive(Debug)]
Expand Down Expand Up @@ -328,6 +334,13 @@ impl<'s> Parser<'s> {
// We never pop the implicit root section.
let section = self.stack.top();

if captures.name("end").unwrap().is_empty() {
return Err(anyhow::anyhow!(
"Unterminated code block at index {}.",
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved
captures.get(0).unwrap().start()
));
}

let mut config: FxHashMap<&'s str, &'s str> = FxHashMap::default();

if let Some(config_match) = captures.name("config") {
Expand Down Expand Up @@ -664,6 +677,18 @@ mod tests {
assert_eq!(file.code, "x = 10");
}

#[test]
fn unterminated_code_block() {
let source = dedent(
"
```
x = 1
",
);
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Unterminated code block at index 0.");
InSyncWithFoo marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn no_header_inside_test() {
let source = dedent(
Expand Down
Loading