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

support prettier yaml with newlines #372

Open
xxchan opened this issue May 14, 2023 · 8 comments
Open

support prettier yaml with newlines #372

xxchan opened this issue May 14, 2023 · 8 comments

Comments

@xxchan
Copy link

xxchan commented May 14, 2023

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 #264 (due to it's breaking changes?), but serde-yaml 0.9 also makes the formatting prettier dtolnay/serde-yaml#226, so that's a little unfortunate :(

@xxchan
Copy link
Author

xxchan commented May 14, 2023

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

@max-sixty
Copy link
Sponsor Contributor

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 Add a to_string_pretty method dtolnay/serde-yaml#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

@xxchan
Copy link
Author

xxchan commented May 17, 2023

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

@mitsuhiko
Copy link
Owner

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.

@nyurik
Copy link

nyurik commented Oct 3, 2023

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!

@Yatekii
Copy link

Yatekii commented Feb 23, 2024

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

@mitsuhiko
Copy link
Owner

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 mitsuhiko mentioned this issue Mar 26, 2024
@mitsuhiko
Copy link
Owner

Turns out serde_yaml is now officially deprecated.

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

No branches or pull requests

5 participants