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] Ignore surrounding whitespace when looking for <!-- snapshot-diagnostics --> directives in mdtests #16380

Merged
merged 2 commits into from
Feb 27, 2025

Conversation

AlexWaygood
Copy link
Member

Summary

Something unfortunate that happened while I was working on #16321 was that I initially added an HMTL comment like this:

<!--snapshot-diagnostics -->

...and I failed to notice for quite a while that there where no snapshots being tested for that specific test. (The directive was missing a whitespace between the <!-- opener and the word "snapshot", meaning that mdtest wasn't recognising it as a directive.)

This PR changes the parsing of these directives so that whitespace surrounding the phrase "snapshot-diagnostics" is ignored inside HTML comments.

Test Plan

I added a unit test to the red_knot_test crate. I also manually verified that if you apply this diff, running cargo test -p red_knot_python_semantic creates a new snapshot for you:

diff --git a/crates/red_knot_python_semantic/resources/mdtest/attributes.md b/crates/red_knot_python_semantic/resources/mdtest/attributes.md
index 6920bf18c..cd6171572 100644
--- a/crates/red_knot_python_semantic/resources/mdtest/attributes.md
+++ b/crates/red_knot_python_semantic/resources/mdtest/attributes.md
@@ -125,6 +125,8 @@ C.only_declared = "overwritten on class"
 
 #### Mixed declarations/bindings in class body and `__init__`
 
+<!--snapshot-diagnostics    -->

@AlexWaygood AlexWaygood added testing Related to testing Ruff itself red-knot Multi-file analysis & type inference labels Feb 25, 2025
Copy link
Contributor

github-actions bot commented Feb 25, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


while let Some(first) = self.cursor.bump() {
match first {
'<' => {
self.explicit_path = None;
self.preceding_blank_lines = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, should these have been removed? I think so, because now when this branch is taken, we either successfully parse an HTML comment (in which case, the previous state should probably be retained) or we report an error. It might be worth an added unit test for this? Although I wonder if it's actually impossible to observe. I'm thinking about something where you have

`some/file/path.py`:

<!-- snapshot-diagnostics -->

```py
print("frob")
```

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah, good question. I think you're right, I think removing these is probably best?

I'm not entirely sure how to easily add a useful unit test for this (and doesn't seem worth spending a lot of time on), but happy to add one as a followup if you can think of something 😄

@AlexWaygood AlexWaygood enabled auto-merge (squash) February 27, 2025 13:22
@AlexWaygood AlexWaygood merged commit 040071b into main Feb 27, 2025
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/mdtest-html-whitespace branch February 27, 2025 13:25
dcreager added a commit that referenced this pull request Feb 28, 2025
* main:
  [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422)
  [red-knot] Disallow more invalid type expressions (#16427)
  Bump version to Ruff 0.9.9 (#16434)
  Check `LinterSettings::preview` for version-related syntax errors (#16429)
  Avoid caching files with unsupported syntax errors (#16425)
  Prioritize "bug" label for changelog sections (#16433)
  [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421)
  Fix string-length limit in documentation for PYI054 (#16432)
  Show version-related syntax errors in the playground (#16419)
  Allow passing `ParseOptions` to inline tests (#16357)
  Bump version to 0.9.8 (#16414)
  [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380)
  Notify users for invalid client settings (#16361)
  Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
dcreager added a commit that referenced this pull request Feb 28, 2025
* dcreager/alist-type:
  Remove unneeded shear override
  Update property test CI
  Move alist into red_knot_python_semantic
  [red-knot] Switch to a handwritten parser for mdtest error assertions (#16422)
  [red-knot] Disallow more invalid type expressions (#16427)
  Bump version to Ruff 0.9.9 (#16434)
  Check `LinterSettings::preview` for version-related syntax errors (#16429)
  Avoid caching files with unsupported syntax errors (#16425)
  Prioritize "bug" label for changelog sections (#16433)
  [`flake8-copyright`] Add links to applicable options (`CPY001`) (#16421)
  Fix string-length limit in documentation for PYI054 (#16432)
  Show version-related syntax errors in the playground (#16419)
  Allow passing `ParseOptions` to inline tests (#16357)
  Bump version to 0.9.8 (#16414)
  [red-knot] Ignore surrounding whitespace when looking for `<!-- snapshot-diagnostics -->` directives in mdtests (#16380)
  Notify users for invalid client settings (#16361)
  Avoid indexing the project if `configurationPreference` is `editorOnly` (#16381)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference testing Related to testing Ruff itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants