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

feat: support updating snapshot tests with codelens/hovering/runnables #18757

Merged
merged 10 commits into from
Jan 1, 2025

Conversation

roife
Copy link
Member

@roife roife commented Dec 25, 2024

Fix #17812

This PR introduces functionality to update snapshot tests.

It detect dependencies of snapshot testing crates first (similar to how FamousDefs). If any dependencies are found, it uses FindUsages to determine whether the fn/impl/mod interacts with snapshot testing APIs.

Currently, the supported crates include expect-test, insta and snapbox.

This PR adds the following configurations:

  • lens.UpdateTest.Enable (default: true).
  • hover.Actions.UpdateTest.Enable (default: true).
  • runnables.AskBeforeUpdateTest (default: true, client-side).

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 25, 2024
@roife
Copy link
Member Author

roife commented Dec 25, 2024

Emmm, I can pass the Prettier check locally, but it fails on CI. 🤔

OK, I’ve fixed it. It turned out to be caused by some settings in my editor...

@roife roife force-pushed the fix-17812 branch 8 times, most recently from 272dd87 to 8a0598d Compare December 25, 2024 12:47
@roife
Copy link
Member Author

roife commented Dec 25, 2024

Here's a demo.
Screen Recording 2024-12-25 at 16 01 40

crates/ide/src/runnables.rs Outdated Show resolved Hide resolved
crates/ide/src/hover/render.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/handlers/request.rs Show resolved Hide resolved
crates/rust-analyzer/src/lsp/to_proto.rs Outdated Show resolved Hide resolved
crates/rust-analyzer/src/lsp/to_proto.rs Outdated Show resolved Hide resolved
self.find_macro("snapbox", &["assert_data_eq", "file", "str"])
}

fn find_macro(&self, crate_name: &str, paths: &[&str]) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

This seems a bit weird to me the way this works, a better idea would be to just check the macro calls of the corresponding body I think and match them up with the expected macros. That is fetch the BodySourceMap for the corresponding body (we might have that via semantics already here, not sure), and iterate the expansions in there to find the macros. Before doing so we should probably first try to resolve all the known testing macros and cache them to just match up the IDs against another

Copy link
Member Author

@roife roife Dec 26, 2024

Choose a reason for hiding this comment

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

I use FindUsage because it allows us to quickly filter out irrelevant macro calls by name. Since we need to search within (potentially large) mod/impl blocks, this approach is more efficient. Traversing BodySourceMap and semantics for filtering may lead to a lot of resolving, which might end up being quite slow.

Resolving the testing macros and caching their IDs is reasonable, but I haven’t figured out where to store them yet. 👀

Copy link
Member

Choose a reason for hiding this comment

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

Resolving the testing macros and caching their IDs is reasonable, but I haven’t figured out where to store them yet

The caching here is good enough to be done at the start of the runnables computation (that is we don't cache across requests)

I use FindUsage because it allows us to quickly filter out irrelevant macro calls by name.

I see, I was thinking of what if the macro call is hidden behind another macro call, but I think that basicalyl breaks all of those frameworks anyways so little point in trrying to do that. I see your approach then. The way this is done here still feels a bit brittle to me though. Will add another comment regarding that in a bit

@roife roife force-pushed the fix-17812 branch 4 times, most recently from c2eea6a to e21d719 Compare December 26, 2024 12:37
@roife
Copy link
Member Author

roife commented Dec 26, 2024

I’ve been struggling to pass the prettier --check ., even after running npm run format locally.

I just realized that I’m using Node 18.20.5 (installed with nvm by default), while the CI is on Node 18.4.0 (See https://github.com/actions/setup-node). The prettier installed via npm install in these two environments use different linting rules. 😧 Therefore, it could lead to a scenario where the code runs successfully locally but fails on CI...

@Veykril
Copy link
Member

Veykril commented Dec 27, 2024

I think it should be fine for us to bump the default node version on our CI then (separate PR)

crates/ide/src/runnables.rs Outdated Show resolved Hide resolved
crates/ide/src/runnables.rs Outdated Show resolved Hide resolved
crates/ide/src/runnables.rs Outdated Show resolved Hide resolved
crates/ide/src/runnables.rs Outdated Show resolved Hide resolved
@Veykril Veykril added this pull request to the merge queue Jan 1, 2025
Merged via the queue into rust-lang:master with commit a612fc9 Jan 1, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Add Update Expect CodeLens for expect-test
3 participants