insta icon indicating copy to clipboard operation
insta copied to clipboard

support prettier yaml with newlines

Open xxchan opened this issue 2 years ago • 8 comments

struct A {
    b: String,
    c: String,
}

#[test]
fn t() {
    let a = A {
        b: "aaa\nbbb\n".to_string(),
        c: "ccc".to_string(),
    };
    insta::assert_yaml_snapshot!(a);
}

Instead of

---
source: ...
expression: a
---
b: "aaa\nbbb\n"
c: ccc

I want to have

---
b: |
  aaa
  bbb
c: cccc

P.S. My actual use case is that my data looks like this:

let chunk2 = StreamChunk::from_pretty(
    "  I I
    +  7 6
    -  3 2
    -  1 0
    +  5 7
    -  2 1
    + 11 8",
);

And I'm trying to replace the tests with insta, but without pretty newlines it's not usable :(


P.P.S, I noticed you switched from serde_yaml to rust-yaml for serialization https://github.com/mitsuhiko/insta/pull/264 (due to it's breaking changes?), but serde-yaml 0.9 also makes the formatting prettier https://github.com/dtolnay/serde-yaml/issues/226, so that's a little unfortunate :(

xxchan avatar May 14 '23 14:05 xxchan

My workaround is to call serde_yaml::to_string on my own, and pass a string to assert_snapshot

xxchan avatar May 14 '23 14:05 xxchan

As a user in the same place as you @xxchan:

  • I've tried to get rust to serialize yaml nicely, in particular multiline strings — both within and outside of insta, both with serde_yaml and rust-yaml
  • But I've struggled (though https://github.com/dtolnay/serde-yaml/issues/226 does look better than I remember)
  • I'm not sure there's much insta can do, at least if there is I'd be happy to put a PR in

max-sixty avatar May 14 '23 20:05 max-sixty

Well, one thing I can't workaround is the header part. expr and description both can't prettify newlines. info works, but that requires me to add Serialize :(

image

xxchan avatar May 17 '23 08:05 xxchan

I would be happy to see multi line strings in the generated YAML. Would absolutely accept a patch. Unfortunately we use yaml_rust and it's hard to move off it without breaking all snapshots and that does not itself support pretty formatting.

mitsuhiko avatar May 17 '23 12:05 mitsuhiko

Turns out toml serialization looks a lot better for simple multi-line comparisons. Still hoping for YAML in the longer run, but TOML will have to do for now. Thanks for the awesome tool!

nyurik avatar Oct 03 '23 02:10 nyurik

Considering that yaml_rust is unmaintained since 3 years, would it be worth a consideration again to switch to serde_yaml?

Yatekii avatar Feb 23 '24 11:02 Yatekii

I'm considering bumping all of this up with 2.0 and also move to serde_yaml. The risk here obviously is that serde_yaml gives no guarantees of snapshot stability.

mitsuhiko avatar Mar 03 '24 00:03 mitsuhiko

Turns out serde_yaml is now officially deprecated.

mitsuhiko avatar Mar 26 '24 10:03 mitsuhiko