Skip to content

Commit

Permalink
Revert "Output full path"
Browse files Browse the repository at this point in the history
This reverts commit b84415b.
  • Loading branch information
InSyncWithFoo committed Dec 14, 2024
1 parent b84415b commit 4037fc3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 48 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MDTEST_TEST_FILTER: &str = "MDTEST_TEST_FILTER";
#[allow(clippy::print_stdout)]
pub fn run(path: &Utf8Path, long_title: &str, short_title: &str, test_name: &str) {
let source = std::fs::read_to_string(path).unwrap();
let suite = match test_parser::parse(short_title, &source, path) {
let suite = match test_parser::parse(short_title, &source) {
Ok(suite) => suite,
Err(err) => {
panic!("Error parsing `{path}`: {err:?}")
Expand Down
71 changes: 24 additions & 47 deletions crates/red_knot_test/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::sync::LazyLock;

use anyhow::bail;
use camino::Utf8Path;
use memchr::memchr2;
use regex::{Captures, Match, Regex};
use rustc_hash::{FxHashMap, FxHashSet};
Expand All @@ -14,12 +13,8 @@ use ruff_text_size::{TextLen, TextRange, TextSize};
use crate::config::MarkdownTestConfig;

/// Parse the Markdown `source` as a test suite with given `title`.
pub(crate) fn parse<'s>(
title: &'s str,
source: &'s str,
path: &'s Utf8Path,
) -> anyhow::Result<MarkdownTestSuite<'s>> {
let parser = Parser::new(title, source, path);
pub(crate) fn parse<'s>(title: &'s str, source: &'s str) -> anyhow::Result<MarkdownTestSuite<'s>> {
let parser = Parser::new(title, source);
parser.parse()
}

Expand Down Expand Up @@ -216,7 +211,6 @@ struct Parser<'s> {

source: &'s str,
source_len: TextSize,
source_path: &'s Utf8Path,

/// Stack of ancestor sections.
stack: SectionStack,
Expand All @@ -229,7 +223,7 @@ struct Parser<'s> {
}

impl<'s> Parser<'s> {
fn new(title: &'s str, source: &'s str, path: &'s Utf8Path) -> Self {
fn new(title: &'s str, source: &'s str) -> Self {
let mut sections = IndexVec::default();
let root_section_id = sections.push(Section {
title,
Expand All @@ -239,11 +233,10 @@ impl<'s> Parser<'s> {
});
Self {
sections,
source,
files: IndexVec::default(),
cursor: Cursor::new(source),
source,
source_len: source.text_len(),
source_path: path,
stack: SectionStack::new(root_section_id),
current_section_files: None,
current_section_has_config: false,
Expand Down Expand Up @@ -348,10 +341,7 @@ impl<'s> Parser<'s> {
let code_block_start = self.cursor.token_len();
let row = self.source.count_lines(TextRange::up_to(code_block_start)) + 1;

return Err(anyhow::anyhow!(
"Unterminated code block at: {}:{row}:0",
self.source_path
));
return Err(anyhow::anyhow!("Unterminated code block at row {row}."));
}

let mut config: FxHashMap<&'s str, &'s str> = FxHashMap::default();
Expand Down Expand Up @@ -448,18 +438,11 @@ impl<'s> Parser<'s> {

#[cfg(test)]
mod tests {
use crate::parser::MarkdownTestSuite;
use camino::Utf8Path;
use ruff_python_trivia::textwrap::dedent;

fn parse<'s>(title: &'s str, source: &'s str) -> anyhow::Result<MarkdownTestSuite<'s>> {
let path = Utf8Path::new("/home/project/file.md");
super::parse(title, source, path)
}

#[test]
fn empty() {
let mf = parse("file.md", "").unwrap();
let mf = super::parse("file.md", "").unwrap();

assert!(mf.tests().next().is_none());
}
Expand All @@ -473,7 +456,7 @@ mod tests {
```
",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand All @@ -498,7 +481,7 @@ mod tests {
x = 1
```",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand Down Expand Up @@ -532,7 +515,7 @@ mod tests {
```
",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test1, test2] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected two tests");
Expand Down Expand Up @@ -579,7 +562,7 @@ mod tests {
```
",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test1, test2] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected two tests");
Expand Down Expand Up @@ -618,7 +601,7 @@ mod tests {
```
",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand All @@ -642,7 +625,7 @@ mod tests {
```
",
);
let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand All @@ -663,7 +646,7 @@ mod tests {
",
);

let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand All @@ -685,7 +668,7 @@ mod tests {
",
);

let mf = parse("file.md", &source).unwrap();
let mf = super::parse("file.md", &source).unwrap();

let [test] = &mf.tests().collect::<Vec<_>>()[..] else {
panic!("expected one test");
Expand All @@ -705,11 +688,8 @@ mod tests {
x = 1
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"Unterminated code block at: /home/project/file.md:2:0"
);
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Unterminated code block at row 2.");
}

#[test]
Expand All @@ -728,11 +708,8 @@ mod tests {
x = 1
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"Unterminated code block at: /home/project/file.md:10:0"
);
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Unterminated code block at row 10.");
}

#[test]
Expand All @@ -748,7 +725,7 @@ mod tests {
## Two
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"Header 'Two' not valid inside a test case; parent 'One' has code files."
Expand All @@ -764,7 +741,7 @@ mod tests {
```
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Invalid config item `foo`.");
}

Expand All @@ -777,7 +754,7 @@ mod tests {
```
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Invalid config item `foo=bar=baz`.");
}

Expand All @@ -790,7 +767,7 @@ mod tests {
```
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(err.to_string(), "Duplicate config item `foo=baz`.");
}

Expand All @@ -807,7 +784,7 @@ mod tests {
```
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"Test `file.md` has duplicate files named `test.py`. \
Expand All @@ -829,7 +806,7 @@ mod tests {
```
",
);
let err = parse("file.md", &source).expect_err("Should fail to parse");
let err = super::parse("file.md", &source).expect_err("Should fail to parse");
assert_eq!(
err.to_string(),
"Test `file.md` has duplicate files named `foo.py`."
Expand Down

0 comments on commit 4037fc3

Please sign in to comment.