Skip to content
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

Enforce starting newlines #563

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Conversation

max-sixty
Copy link
Collaborator

@max-sixty max-sixty commented Aug 11, 2024

This is an example of what I've been suggesting at #506 (+ some linked issues):

  • Stacked on Unify handling of file & inline snapshots (compat) #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.

max-sixty added a commit to max-sixty/insta that referenced this pull request Aug 12, 2024
This will make diffs in mitsuhiko#563 more reasonable

(Though it was good to have some snapshots that _weren't_ updated, as meta-tests that un-updated snapshots still worked -- possibly we try and add an integration test in that direction...)
max-sixty added a commit that referenced this pull request Aug 12, 2024
This will make diffs in #563 more reasonable

(Though it was good to have some snapshots that _weren't_ updated, as
meta-tests that un-updated snapshots still worked -- possibly we try and
add an integration test in that direction...)
insta/src/serialization.rs Outdated Show resolved Hide resolved
max-sixty added a commit to max-sixty/insta that referenced this pull request Aug 17, 2024
We previously excluded these. But it's somewhat required for mitsuhiko#563, and I don't think there's a big downside -- we only generate pending files when a snapshot doesn't match of `--force-update-snapshots` is passed.
max-sixty added a commit to max-sixty/insta that referenced this pull request Sep 17, 2024
Moving some unambiguous code from mitsuhiko#563 into a new PR so we can keep moving
max-sixty added a commit to max-sixty/insta that referenced this pull request Sep 17, 2024
max-sixty added a commit that referenced this pull request Sep 17, 2024
Moving some unambiguous code from #563 into a new PR so we can keep
moving
max-sixty added a commit that referenced this pull request Sep 17, 2024
Another PR taking some unambiguous code from #563
max-sixty added a commit that referenced this pull request Sep 19, 2024
#581)

This solves the issue in #573 for the moment:
- When `--force-update-snapshots` in passed, we only update inline
snapshots when there's some difference _within_ the string, such as an
additional linebreak at the start or the end
- We no longer update the surrounding hashes. In the existing code, this
happened because we were writing too many inline snapshots, not because
we were actually checking the number of hashes
- Checking the number of hashes isn't easy to do — reason outlined at
#573 (comment)
- The main change in the code is that we store the unnormalized snapshot
and then normalize when we need to. The existing code stored the
normalized version, which meant we couldn't evaluate whether the
unnormalized versions were different

It's a decent number of changes, but will integrate nicely with
#563.

~(FYI we currently don't look at the indentation, but we could adjust
this)~ Now indentation works too
@max-sixty
Copy link
Collaborator Author

One downside (but balanced against lots of upsides!) is that because we insert a newline at the start of a multiline string, a literal string can look a bit odd:

assert_snapshot!("
foo
bar
", @r"

foo
bar
");

Notice that because the original literal has a newline after its ", and we always add a newline to the original value, there are then two newlines after the opening quote of the result.

This is mostly only noticeable in examples: very few generated values start with a newline!

@max-sixty max-sixty mentioned this pull request Oct 6, 2024
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.

1 participant