Skip to content

Commit

Permalink
Switch from serde_yaml to rust-yaml for serialization (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Aug 6, 2022
1 parent 2fb4c6a commit d65444c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ csv = ["dep_csv", "serialization"]
json = ["serde_json", "serialization"]
ron = ["dep_ron", "serialization"]
toml = ["dep_toml", "serialization"]
yaml = ["serde_yaml", "serialization"]
yaml = ["serialization"]

# legacy support. This is turned on by default for a single release and turns
# on the assert_json_snapshot! and assert_yaml_snapshot! macros by default.
# This will be phased out. Use the "json" and "yaml" features instead.
# If you already want to benefit of the leaner defaults, turn off the default
# features and opt into what you want manually (you probably want to turn on "colors").
legacy_default_serialization = ["serde_json", "serde_yaml", "serialization"]
legacy_default_serialization = ["serde_json", "serialization"]

[dependencies]
dep_csv = { package = "csv", version = "1.1.4", optional = true }
Expand All @@ -71,7 +71,6 @@ regex = { version = "1.6.0", default-features = false, optional = true, features
yaml-rust = "0.4.5"
serde = { version = "1.0.117", optional = true }
serde_json = { version = "1.0.59", optional = true }
serde_yaml = { version = "0.8.26", optional = true }
linked-hash-map = "0.5.6"

[dev-dependencies]
Expand Down
35 changes: 0 additions & 35 deletions cargo-insta/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn get_cargo_workspace(manifest_dir: &str) -> Arc<PathBuf> {
let docs =
yaml_rust::YamlLoader::load_from_str(std::str::from_utf8(&output.stdout).unwrap())
.unwrap();
let manifest = &docs[0];
let manifest = docs.get(0).expect("Unable to parse cargo manifest");
let workspace_root = PathBuf::from(manifest["workspace_root"].as_str().unwrap());
Arc::new(workspace_root)
};
Expand Down
58 changes: 57 additions & 1 deletion src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn serialize_content(
match format {
// #[cfg(feature = "yaml")]
SerializationFormat::Yaml => {
let serialized = serde_yaml::to_string(&_content).unwrap();
let serialized = _content.as_yaml();
match _location {
SnapshotLocation::Inline => serialized,
SnapshotLocation::File => serialized[4..].to_string(),
Expand Down Expand Up @@ -123,3 +123,59 @@ pub fn serialize_value_redacted<S: Serialize>(
}
serialize_content(content, format, location)
}

#[test]
fn test_yaml_serialization() {
let yaml = serialize_content(
Content::Map(vec![
(
Content::from("env"),
Content::Seq(vec![
Content::from("ENVIRONMENT"),
Content::from("production"),
]),
),
(
Content::from("cmdline"),
Content::Seq(vec![Content::from("my-tool"), Content::from("run")]),
),
]),
SerializationFormat::Yaml,
SnapshotLocation::File,
);
crate::assert_snapshot!(&yaml, @r###"
env:
- ENVIRONMENT
- production
cmdline:
- my-tool
- run
"###);

let inline_yaml = serialize_content(
Content::Map(vec![
(
Content::from("env"),
Content::Seq(vec![
Content::from("ENVIRONMENT"),
Content::from("production"),
]),
),
(
Content::from("cmdline"),
Content::Seq(vec![Content::from("my-tool"), Content::from("run")]),
),
]),
SerializationFormat::Yaml,
SnapshotLocation::Inline,
);
crate::assert_snapshot!(&inline_yaml, @r###"
---
env:
- ENVIRONMENT
- production
cmdline:
- my-tool
- run
"###);
}

0 comments on commit d65444c

Please sign in to comment.