Skip to content

Conversation

@max-sixty
Copy link
Collaborator

@max-sixty max-sixty commented Jun 15, 2024

(requires some thought / discussion)

This would allow us to handle ending newlines.

We'd need to do some work

  • to check newlines
  • to handle the transition when some files won't have these ending items, to only check those that do

(...possibly work we should do before we merge this?)

(requires some thought / discussion)

This would allow us te handle ending newlines.

We'd need to do some work
- to check newlines
- to handle the transition when some files won't have these ending items, to only check those that do

(...possibly work we should do before we merge this?)
@max-sixty max-sixty mentioned this pull request Jun 17, 2024
@max-sixty max-sixty requested a review from mitsuhiko July 7, 2024 19:18
@mitsuhiko
Copy link
Owner

We have the same problem with leading whitespace too. I think it needs addressing but I'm not convinced that this is the right way to do it.

@max-sixty
Copy link
Collaborator Author

I think it needs addressing but I'm not convinced that this is the right way to do it.

No prob ofc

We have the same problem with leading whitespace too.

Right — just tbc, if we have delimiters at the start & end, then it becomes possible to check for the number of newlines. IIUC, we previously gave up and just .trim everything because we didn't have a delimiter at the end.

(Note that this PR doesn't check newlines, it just adds the delimiters so that we can later check them...)

@mitsuhiko
Copy link
Owner

I'm thinking the right solution here would be that if

  • more than one trailing newline exist
  • or other trailing spaces
  • or any leading spaces

We add a unicode character to mark the beginning or end of it. I was thinking of inserting at the end and at the beginning or something similar. The diffing tool already also adds special unicode characters to indicate some significant whitespace to clarify what's going on.

@max-sixty
Copy link
Collaborator Author

more than one trailing newline exist

How about discriminating between zero and one trailing newline?


To ensure we're on the same page — at least for file snapshots — I think this is only a problem because of tools which change ending newlines (for example lots of repos have lints which enforce exactly one ending newline). Otherwise, we can just put the value after the header and we're done. Or am I missing something?

If that's the case, why do we need to handle the beginning?

@mitsuhiko
Copy link
Owner

So to be specific on the challenge: the leading whitespace is a problem in inline cases because of whitespace removal for indentation handling. If the leading whitespace is meaningful you lose the meaning in the assertions. The trailing one has similar challenges, but the newline specific case is indeed just for file snapshots due to editor settings.

@max-sixty
Copy link
Collaborator Author

the leading whitespace is a problem in inline cases because of whitespace removal for indentation handling

I would claim newlines in inline snapshots are a very tractable problem: #457 (comment) — inline snapshots are either short and contain zero newlines, or long and always have exactly one newline trimmed at the start & end...

(I agree indentation isn't possible to control like this, though also that's a less frequent issue)

max-sixty added a commit that referenced this pull request Sep 10, 2025
This is an example of what I've been suggesting at
#506 (+ some linked issues):
- ~Stacked on #528 so that would
need to merge first~
- Enforces the correct number of newlines at the start of snapshots. The
start is easier because we don't need to worry about editor issues in
trailing newlines (and don't need to worry about how `Lines` deals
ambiguously with trailing new lines)
- Works for both inline & file snapshots
- Adds a `matches_legacy` method on snapshot contents — this
encapsulates the older formats we still support and warns about them

~It's not ready to merge yet, because we don't seem to actually support
`--force-update-snapshots` on inline values, which is somewhat required.
But I'm publishing for feedback & to put some weight on #528 in case we
don't want to pursue.~

---------

Co-authored-by: Claude <no-reply@anthropic.com>
max-sixty and others added 6 commits September 10, 2025 14:50
Co-authored-by: Claude <no-reply@anthropic.com>
- Add closing --- marker when writing new snapshots
- Maintain backward compatibility with old snapshots without the marker
- Handle edge case where old snapshot content naturally ends with ---
- Add comprehensive functional tests for all scenarios
- Test all macro types (snapshot, yaml, json, debug) get the marker
- Binary snapshots correctly don't get the text closing marker
Replace backslashes with forward slashes in CARGO_MANIFEST_DIR path
to avoid TOML parsing errors on Windows
max-sixty and others added 3 commits September 10, 2025 21:23
The standard test framework already handles Windows path replacement
correctly via the $PROJECT_PATH placeholder.
Only test debug snapshots which don't need serde or special cargo
features. This makes the test simpler and more consistent with other
tests in the file.
Co-authored-by: Claude <no-reply@anthropic.com>
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.

2 participants