Port SnapshotDir tests to use insta snapshots

This commit is contained in:
Lucien Greathouse
2019-09-30 18:33:46 -07:00
parent 1c6788ea45
commit 95f06d56d8
5 changed files with 83 additions and 20 deletions

View File

@@ -25,12 +25,12 @@
//! }
//! ```
//!
//! **The methods in this module can only handle relative paths, since absolute
//! paths are never portable.**
//! For absolute paths, which are only safe to serialize if they're artificial,
//! use `serialize_absolute`.
use std::path::{Component, Path};
use serde::Serializer;
use serde::{ser::SerializeSeq, Serialize, Serializer};
pub fn serialize_option<S, T>(maybe_path: &Option<T>, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -72,3 +72,35 @@ where
serializer.serialize_str(&output)
}
pub fn serialize_absolute<S, T>(path: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
let as_str = path
.as_ref()
.as_os_str()
.to_str()
.expect("Invalid Unicode in file path, cannot serialize");
let replaced = as_str.replace("\\", "/");
serializer.serialize_str(&replaced)
}
#[derive(Serialize)]
struct WithAbsolute<'a>(#[serde(serialize_with = "serialize_absolute")] &'a Path);
pub fn serialize_vec_absolute<S, T>(paths: &Vec<T>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: AsRef<Path>,
{
let mut seq = serializer.serialize_seq(Some(paths.len()))?;
for path in paths {
seq.serialize_element(&WithAbsolute(path.as_ref()))?;
}
seq.end()
}