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

Implement @snapshot check for htmldocck #91209

Merged
merged 2 commits into from
Dec 4, 2021
Merged

Conversation

camelid
Copy link
Member

@camelid camelid commented Nov 25, 2021

This form of check allows performing snapshot tests (à la src/test/ui)
on rustdoc HTML output, making it easier to create and update tests.

See this Zulip thread for more information about the motivation for
this change.

r? @GuillaumeGomez

@camelid camelid added A-testsuite Area: The testsuite used to check the correctness of rustc T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 25, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 25, 2021
@jyn514
Copy link
Member

jyn514 commented Nov 25, 2021

cc @jsha, I know you had reservations about this

Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

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

This is really cool :) I still want to give Jacob a chance to give their opinion but I think it's very useful.

src/etc/htmldocck.py Outdated Show resolved Hide resolved
@jsha
Copy link
Contributor

jsha commented Nov 27, 2021

@jsha
Copy link
Contributor

jsha commented Nov 28, 2021

Reporting back from the Zulip thread: I'm okay proceeding with this, with the intention to be really aware of how brittle the tests are, and to make a different plan if they are too brittle.

We are starting out with the intention to make sure snapshot tests don't encompass too large an HTML subtree. What do you think of enforcing that in htmldocck? I.e., error out if there are more than N nodes, or more than X bytes in a snapshot?

Also, the error output when this fails should say what the command is to bless the results.

@jyn514
Copy link
Member

jyn514 commented Nov 28, 2021

What do you think of enforcing that in htmldocck?

Tidy seems like a better place to put this, we should be avoiding python where possible imo.

@GuillaumeGomez
Copy link
Member

Except that python has the information whereas we would need a weird check in tidy to enforce it.

@camelid
Copy link
Member Author

camelid commented Nov 28, 2021

We are starting out with the intention to make sure snapshot tests don't encompass too large an HTML subtree. What do you think of enforcing that in htmldocck? I.e., error out if there are more than N nodes, or more than X bytes in a snapshot?

I thought about doing the byte-based check, though making it node-based sounds like a better idea. I do worry that that will make the test brittle in a different way—as soon as the threshold is reached, perhaps by a PR that changes the DOM structure, a bunch of tests will fail and can't be fixed easily. But perhaps it's worth it? What do you think?

I agree with Guillaume that if we implement this limit, it should be in htmldocck for simplicity. At some point it'd be nice to have htmldocck all be in Rust, but I think htmldocck is the best place for now.

@GuillaumeGomez
Copy link
Member

Why not both?

@camelid
Copy link
Member Author

camelid commented Nov 28, 2021

Why not both?

What both?

@GuillaumeGomez
Copy link
Member

Bytes and node depth.

@camelid
Copy link
Member Author

camelid commented Nov 28, 2021

Why? I think doing both would add needless complexity for not much gain. I think the node-based limit is probably a better estimate, since you may want to have a section with a lot of prose but few elements.

@GuillaumeGomez
Copy link
Member

Well, if you have more than X node depth and/or X bytes of data, abort. Don't see what it changes... The node depth should be the most important limit though.

@jsha
Copy link
Contributor

jsha commented Nov 28, 2021

I do worry that that will make the test brittle in a different way—as soon as the threshold is reached, perhaps by a PR that changes the DOM structure, a bunch of tests will fail and can't be fixed easily. But perhaps it's worth it? What do you think?

This is a good point. Maybe we could have the equivalent of a #[allow] directive allowing certain test cases to exceed the limit; we could use the number of such allowances as an indicator of whether snapshots are getting too big on average.

@camelid
Copy link
Member Author

camelid commented Nov 28, 2021

This is a good point. Maybe we could have the equivalent of a #[allow] directive allowing certain test cases to exceed the limit; we could use the number of such allowances as an indicator of whether snapshots are getting too big on average.

Hmm, now this sounds like tidy should be involved, as Joshua suggested. There's already ignore-tidy-filelength etc., so it fits in with tidy's model.

@camelid
Copy link
Member Author

camelid commented Dec 1, 2021

Actually, I think integrating with tidy would be difficult, since it's not immediately obvious what files are the output of snapshot tests.

@jsha is it okay to skip the limit for now? If we run into issues with brittle tests, we can always add a limit later. I'd like this PR to just implement an "MVP" of snapshot tests, and I want to avoid it getting too big.

@jsha
Copy link
Contributor

jsha commented Dec 1, 2021 via email

This form of check allows performing snapshot tests (à la `src/test/ui`)
on rustdoc HTML output, making it easier to create and update tests.

See this Zulip thread [1] for more information about the motivation for
this change.

[1]: https://zulip-archive.rust-lang.org/stream/266220-rustdoc/topic/HTML.20snapshot.20tests.html#262651142
I'd been thinking about implementing snapshot testing for a while, but
This test is what finally made me do it. It really benefits from using
snapshot testing, so it's a good initial place to use `@snapshot`.
@camelid
Copy link
Member Author

camelid commented Dec 1, 2021

I fixed Joshua's review comment and added docs.

@camelid
Copy link
Member Author

camelid commented Dec 1, 2021

This should be ready for a final review now :)

// 'Hello world!'
// @has - '//details[@class="rustdoc-toggle top-doc"]/div[@class="docblock"]/p[2]' \
// 'Goodbye! Hello again!'
// @snapshot S2_top-doc - '//details[@class="rustdoc-toggle top-doc"]/div[@class="docblock"]'
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it make more sense to keep the original order for the argument? So first, the file we're checking and then the snapshot file. It would be incoherent with the rest otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered that, but then it looks a bit like S2_top-doc is a text pattern to match against. Plus, the name of the snapshot is important so you know what file to look at it for the output, so I put it at the front. I'm open to changing it, but I'm not sure if putting the snapshot name at the end would necessarily be clearer.

Copy link
Member

Choose a reason for hiding this comment

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

I tihnk coherence is the most important thing. For me it's super strange that only @snapshot has this order for the args. If others agree with you, I won't oppose though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Joshua gave a thumbs up on my previous message, so I think let's keep it at the beginning for now if that's okay.

Copy link
Member

Choose a reason for hiding this comment

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

Can always be changed later on in any case so it's fine.

@jyn514
Copy link
Member

jyn514 commented Dec 3, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Dec 3, 2021

📌 Commit fe88fcf has been approved by jyn514

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 3, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 4, 2021
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#90538 (Document how recursion is handled for `ty::Ty`)
 - rust-lang#90851 (Add unchecked downcast methods)
 - rust-lang#91209 (Implement ``@snapshot`` check for htmldocck)
 - rust-lang#91385 (Suggest the `pat_param` specifier before `|` on 2021 edition )
 - rust-lang#91478 (Remove incorrect newline from float cast suggestion)
 - rust-lang#91481 (Use let_else in some more places in rustc_lint)
 - rust-lang#91488 (Fix ICE when `yield`ing in function returning `impl Trait`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 420ddd0 into rust-lang:master Dec 4, 2021
@rustbot rustbot added this to the 1.59.0 milestone Dec 4, 2021
@camelid camelid deleted the snapshot branch December 4, 2021 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc C-enhancement Category: An issue proposing an enhancement or a PR with one. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants