Skip to content

Commit

Permalink
fix(snap): Ensure we normalize to the intended format
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 22, 2024
1 parent 4bf3ca6 commit 51229ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Assert {
) -> (crate::Data, crate::Data) {
let expected = expected.normalize(NormalizeNewlines);
// On `expected` being an error, make a best guess
let format = expected.format();
let format = expected.intended_format();

actual = actual.coerce_to(format).normalize(NormalizeNewlines);

Expand All @@ -147,7 +147,7 @@ impl Assert {
) -> (crate::Data, crate::Data) {
let expected = expected.normalize(NormalizeNewlines);
// On `expected` being an error, make a best guess
let format = expected.format();
let format = expected.intended_format();
actual = actual.coerce_to(format);

if self.normalize_paths {
Expand Down
53 changes: 45 additions & 8 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub struct Data {

#[derive(Clone, Debug)]
pub(crate) enum DataInner {
Error(crate::Error),
Error(DataError),
Binary(Vec<u8>),
Text(String),
#[cfg(feature = "json")]
Expand All @@ -135,8 +135,12 @@ impl Data {
DataInner::Json(raw.into()).into()
}

fn error(raw: impl Into<crate::Error>) -> Self {
DataInner::Error(raw.into()).into()
fn error(raw: impl Into<crate::Error>, intended: DataFormat) -> Self {
DataError {
error: raw.into().into(),
intended,
}
.into()
}

/// Empty test data
Expand All @@ -157,7 +161,7 @@ impl Data {
pub fn read_from(path: &std::path::Path, data_format: Option<DataFormat>) -> Self {
match Self::try_read_from(path, data_format) {
Ok(data) => data,
Err(err) => Self::error(err).with_path(path),
Err(err) => Self::error(err, data_format.unwrap_or_default()).with_path(path),
}
}

Expand Down Expand Up @@ -250,7 +254,7 @@ impl Data {

pub fn to_bytes(&self) -> Result<Vec<u8>, crate::Error> {
match &self.inner {
DataInner::Error(err) => Err(err.clone()),
DataInner::Error(err) => Err(err.error.clone()),
DataInner::Binary(data) => Ok(data.clone()),
DataInner::Text(data) => Ok(data.clone().into_bytes()),
#[cfg(feature = "json")]
Expand All @@ -268,7 +272,7 @@ impl Data {
pub fn is(self, format: DataFormat) -> Self {
match self.try_is(format) {
Ok(new) => new,
Err(err) => Self::error(err),
Err(err) => Self::error(err, format),
}
}

Expand Down Expand Up @@ -317,7 +321,7 @@ impl Data {
/// This is generally used on `actual` data to make it match `expected`
pub fn coerce_to(self, format: DataFormat) -> Self {
let mut data = match (self.inner, format) {
(DataInner::Error(inner), _) => Self::error(inner),
(DataInner::Error(inner), _) => inner.into(),
(inner, DataFormat::Error) => inner.into(),
(DataInner::Binary(inner), DataFormat::Binary) => Self::binary(inner),
(DataInner::Text(inner), DataFormat::Text) => Self::text(inner),
Expand Down Expand Up @@ -398,6 +402,18 @@ impl Data {
}
}

pub(crate) fn intended_format(&self) -> DataFormat {
match &self.inner {
DataInner::Error(DataError { intended, .. }) => *intended,
DataInner::Binary(_) => DataFormat::Binary,
DataInner::Text(_) => DataFormat::Text,
#[cfg(feature = "json")]
DataInner::Json(_) => DataFormat::Json,
#[cfg(feature = "term-svg")]
DataInner::TermSvg(_) => DataFormat::TermSvg,
}
}

pub(crate) fn relevant(&self) -> Option<&str> {
match &self.inner {
DataInner::Error(_) => None,
Expand All @@ -420,10 +436,19 @@ impl From<DataInner> for Data {
}
}

impl From<DataError> for Data {
fn from(inner: DataError) -> Self {
Data {
inner: DataInner::Error(inner),
source: None,
}
}
}

impl std::fmt::Display for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.inner {
DataInner::Error(err) => err.fmt(f),
DataInner::Error(data) => data.fmt(f),
DataInner::Binary(data) => String::from_utf8_lossy(data).fmt(f),
DataInner::Text(data) => data.fmt(f),
#[cfg(feature = "json")]
Expand Down Expand Up @@ -454,6 +479,18 @@ impl PartialEq for Data {
}
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct DataError {
error: crate::Error,
intended: DataFormat,
}

impl std::fmt::Display for DataError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.error.fmt(f)
}
}

#[cfg(feature = "term-svg")]
fn text_elem(svg: &str) -> Option<&str> {
let open_elem_start_idx = svg.find("<text")?;
Expand Down
6 changes: 3 additions & 3 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct NormalizeNewlines;
impl Normalize for NormalizeNewlines {
fn normalize(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => Data::error(err),
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_lines(&text);
Expand All @@ -36,7 +36,7 @@ pub struct NormalizePaths;
impl Normalize for NormalizePaths {
fn normalize(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => Data::error(err),
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_paths(&text);
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a> NormalizeMatches<'a> {
impl Normalize for NormalizeMatches<'_> {
fn normalize(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => Data::error(err),
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = self
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl PathDiff {
crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines);

actual = actual
.coerce_to(expected.format())
.coerce_to(expected.intended_format())
.normalize(NormalizeNewlines);

if expected != actual {
Expand Down Expand Up @@ -268,7 +268,7 @@ impl PathDiff {
crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines);

actual = actual
.coerce_to(expected.format())
.coerce_to(expected.intended_format())
.normalize(NormalizePaths)
.normalize(NormalizeNewlines)
.normalize(NormalizeMatches::new(substitutions, &expected));
Expand Down

0 comments on commit 51229ce

Please sign in to comment.