-
Notifications
You must be signed in to change notification settings - Fork 630
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
Replace tests that assert on token output with auto-updatable samples #1649
Conversation
This extracts the code samples from each test that used to do `assert list(lexer.get_tokens(fragment)) == tokens`, puts each of them into their own file, and gives the ability to *automatically* update such files if there are changes to the code affecting them. To perform such an update in-place, run `pytest tests/test_lexers.py --update-goldens`. The functionality comes from the 'pytest-golden' plugin [1]. When testing *without* the update flag, this just asserts that the token list does not change from the stored list. So, the tests still serve the same purpose as before (and you can see the diffs both from pytest and in code review), just that they don't need to be manually updated. One only needs to provide the interesting code sample. [1]: https://github.com/oprypin/pytest-golden
Hi, that's ... a rather huge change! Before I get into the details of this, this doesn't come with a formatter for it, unless I'm missing something. Right now use can use In general I think the idea is good, but I'm not entirely sure if that's not a bit too big of a change to take right now. I'd also like to have @birkenfeld chime in. |
Haha I see, looks like I didn't realize that people normally use You're saying it doesn't come with a formatter, but a formatter would be used manually and its output would be pasted into the test file. Instead, this one does the updating itself (with one universal command), so it's not needed. |
Can you please re-run this on the latest master, looks like there's a bunch of conflicts here. Regarding the formatter, I'm not sure I'm following: Today, when someone wants to test a piece of text, they run it through |
This initial translation is partly manual and not trivial. Not worth resolving conflicts in case it's going to sit here for a while again.
They add a new file that looks like input: |
SOURCE CODE then run If the example file already exists but needs an update, just the latter part is necessary. |
Well, I do want to merge it immediately, so it's not going to sit there for a while :) I just didn't want to merge ahead of a release, as we had a few PRs in flight.
I see. Can you please add a few lines to the docs about this? |
Oh sorry then, let me do that. Has @birkenfeld checked it out? I acknowledge that this one can be controversial :D BTW, I do recommend trying it out for yourself locally. There are various ways (
And yes, I'll think of a doc as well. |
Yes, @birkenfeld is on board. Please let me know when you're ready, I want to merge this within a day of you being done so we can cleanly cut over to the new system going forward. Thanks for your help here! |
Thank you as well! I will take a detailed look over everything today and then let you know. |
To chime in in more detail here, after having reviewed the patch more closely now: I'm in favor of the general concept, however I'm hesitant about two things:
We actually already have a facility for this in the test suite; the test_examplefiles can store its output and check against that (but it's not as nice, and disabled in the code). In the course (or as a followup) of this change, I'd also like to convert the examplefiles test to this method. So as a TLDR: excellent idea, but the implementation needs to be simplified. |
Hehe, maybe it's true. Having gone through several iterations of this in development, I lost the context that it is in fact just 2 string literals. But to be fair, another feature of YAML is being used: the comments. # comment
input: |
sample
# comment
tokens: |
Aaa aaa
Bbb bbb |
|
As to the file format, it could be as simple as splitting it into two files, |
2b90301
to
3a649d2
Compare
As of now, did the work purely to avoid any external deps, by re-implementing them here. If you want a particular file format, let me know. |
Pushed with new file format, one which has no invalid states.
3rd and 4th examples should never occur in practice. And of course, writing the files will be in the canonical form. |
Ok sure, we can squash when merging. |
Very nice, thanks a lot! That should hopefully reduce the entry barrier a bit going forward. |
I see that not everything is 100% smooth. E.g. the fact that 3f78a50 was needed. If you have any pains with this, please poke me any time :) |
This extracts the code samples from each test that used to do
assert list(lexer.get_tokens(fragment)) == tokens
, puts each of them into their own file, and gives the ability to automatically update such files if there are changes to the code affecting them.To perform such an update in-place, run
pytest tests/test_lexers.py --update-goldens
.The functionality comes from the 'pytest-golden' plugin (disclaimer: written by me!)When testing without the update flag, this just asserts that the token list does not change from the stored list.
So, the tests still serve the same purpose as before (and you can see the diffs both from pytest and in code review), just that they don't need to be manually updated. One only needs to provide the interesting code sample.
Note to reviewers:
tests/test_lexers.py is the newly added file, which is the entry point for these tests.
The conversion work was mostly mechanical, but I'll make sure to look through all of it again if the overall idea is liked.