Skip to content

Conversation

@max-sixty
Copy link
Collaborator

This is a version of #468 that doesn't break compatibility — the change in snapshot (removing the leading --- in inline snapshots) is optional, and snapshots still pass if they have it. Otherwise it has the same benefits of #468

(There is a tiny corner case that if a snapshot is intended to test whether there's a leading ---, we could get a false match, but this is a very small possibility — much much smaller than removing the line endings we've done)

As part of that, this adds as_str_exact & matches_fully methods to SnapshotContents, which allows us to unify our normalization of snapshots contents, and allow detecting snapshots that pass but don't completely match; for example if we want to restore checking for line endings.

This is the code for mitsuhiko#456 (comment), as mentioned in mitsuhiko#466.

There's a very small change in yaml inline snapshots — shown here in the tests. In return, it makes the macros simpler & more maintainable.
@max-sixty max-sixty requested a review from mitsuhiko July 15, 2024 06:23
@max-sixty max-sixty force-pushed the unify-file-inline-compat branch from 0a479a4 to 3e6bdee Compare August 2, 2024 23:19
@mitsuhiko
Copy link
Owner

One thing that is a bit weird I guess is that the leading --- is just a YAML thing, but we're now removing it regardless of the snapshot type. But given how the code is structured there is not a lot of alternative I think.

@mitsuhiko mitsuhiko merged commit 4bea0fb into mitsuhiko:master Aug 31, 2024
@max-sixty max-sixty deleted the unify-file-inline-compat branch August 31, 2024 21:10
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>
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