Skip to content

feat: allow empty lines in snapshot#1042

Open
George-Ogden wants to merge 3 commits intosyrupy-project:mainfrom
George-Ogden:optional-indentation
Open

feat: allow empty lines in snapshot#1042
George-Ogden wants to merge 3 commits intosyrupy-project:mainfrom
George-Ogden:optional-indentation

Conversation

@George-Ogden
Copy link

@George-Ogden George-Ogden commented Nov 29, 2025

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:

# Outputs README-like text

Including empty newlines above and below

<footer>Followed by a footer</footer>

This is saved in a snapshot as:

  """
  # Outputs README-like text
  
  Including empty newlines above and below
  
  <footer>Followed by a footer</footer>
  """

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

  • This PR has sufficient documentation.
  • This PR has sufficient test coverage.
  • This PR title satisfies semantic convention.

Additional Comments

No additional comments.

@codecov
Copy link

codecov bot commented Nov 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@           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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Author

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.ambr snapshot files have meaningful trailing whitespace

2 participants