feat: allow empty lines in snapshot#1042
feat: allow empty lines in snapshot#1042George-Ogden wants to merge 3 commits intosyrupy-project:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@
## main #1042 +/- ##
=======================================
Coverage 97.29% 97.30%
=======================================
Files 21 21
Lines 1666 1669 +3
=======================================
+ Hits 1621 1624 +3
Misses 45 45 🚀 New features to boost your workflow:
|
3d0c999 to
ac1ec15
Compare
ac1ec15 to
51b2617
Compare
| snapshot_data += line[indent_len:] | ||
| elif test_name is not None and line.strip() == "": | ||
| # Remove trailing whitespace (but not newline character) from empty line. | ||
| snapshot_data += textwrap.dedent(line) |
There was a problem hiding this comment.
The previous elif checks if the line starts with the indent, which means L188 will only evaluate to true if we encounter a line with with less leading whitespace than the indentation size (2 chars).
It means these snapshots are equivalent:
# name: test_multiline_repr
⬚⬚Line1
⬚⬚Line2
⬚⬚Line3
# name: test_multiline_repr
⬚⬚Line1
⬚⬚Line2
⬚
⬚⬚Line3
# name: test_multiline_repr
⬚⬚Line1
⬚⬚Line2
⬚⬚
⬚⬚Line3
Having modified snapshots not break the test violates the soundness principle IMO and for that reason alone I'm going to reject this change.
That being said, if the motivation is due to tooling like editorconfig removing trailing whitespace, wouldn't it also remove trailing whitespace at the end of "non-empty" lines which this PR doesn't address?
If your IDE is stripping trailing whitespace, I recommend configuring an exception for amber files. Generally snapshot files shouldn't be modified by hand anyway.
There was a problem hiding this comment.
I see your point about soundness, but I think trailing whitespace is a different issue. While you're right that we could configure IDEs to handle snapshot files specially, it's a bit of a pain, especially for new users who run the test fine the first time, but then see errors afterwards. Trailing whitespace at the end of actual content lines usually matters, but on blank lines it's almost always accidental.
I also think this will affect a very small number of users, and it comes at such a small cost.
Description
This PR allows empty lines in the snapshot file to be treated that way, without affecting other snapshot semantics.
Example
Say I have a test output that looks like this:
This is saved in a snapshot as:
Therefore, there are two lines in this snapshot that include just spaces:
" ".This PR does not affect how the snapshots are saved, but if you trim the empty spaces on the blank lines, such as with a linting tool, the snapshot will load them as empty lines (currently, they are skipped).
Related Issues
Checklist
Additional Comments
No additional comments.