insta icon indicating copy to clipboard operation
insta copied to clipboard

Enforce starting newlines

Open max-sixty opened this issue 1 year ago • 1 comments

This is an example of what I've been suggesting at https://github.com/mitsuhiko/insta/pull/506 (+ some linked issues):

  • ~Stacked on https://github.com/mitsuhiko/insta/pull/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 avatar Aug 11 '24 23:08 max-sixty

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 avatar Sep 22 '24 02:09 max-sixty

I'm tempted to merge this as there's no downside to enforce starting newlines; and it simplifies the code overall

lmk any objections...

max-sixty avatar Sep 10 '25 07:09 max-sixty

merging but one click to revert if anyone disagrees

max-sixty avatar Sep 10 '25 21:09 max-sixty