Skip to content

Commit

Permalink
fix(snap): Don't panic on error
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 22, 2024
1 parent ab6da39 commit f77a6f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ 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, data_format.unwrap_or_default()).with_path(path),
Err(err) => Self::error(err, data_format.unwrap_or_else(|| DataFormat::from(path)))
.with_path(path),
}
}

Expand Down
20 changes: 12 additions & 8 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ impl Normalize for NormalizeMatches<'_> {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = self
.substitutions
.normalize(&text, &self.pattern.render().unwrap());
Data::text(lines)
if let Some(pattern) = self.pattern.render() {
let lines = self.substitutions.normalize(&text, &pattern);
Data::text(lines)
} else {
DataInner::Text(text).into()
}
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
Expand All @@ -94,10 +96,12 @@ impl Normalize for NormalizeMatches<'_> {
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = self
.substitutions
.normalize(&text, &self.pattern.render().unwrap());
DataInner::TermSvg(lines).into()
if let Some(pattern) = self.pattern.render() {
let lines = self.substitutions.normalize(&text, &pattern);
DataInner::TermSvg(lines).into()
} else {
DataInner::TermSvg(text).into()
}
}
};
new.source = data.source;
Expand Down

0 comments on commit f77a6f5

Please sign in to comment.