Skip to content

Commit

Permalink
refactor: Transition to assert::Error
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed May 15, 2024
1 parent 8b432b6 commit b0553d4
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Assert {
expected: &crate::Data,
actual: &crate::Data,
actual_name: Option<&dyn std::fmt::Display>,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
if expected != actual {
let mut buf = String::new();
crate::report::write_diff(
Expand Down
26 changes: 14 additions & 12 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,20 +929,20 @@ pub(crate) mod examples {
pub fn compile_example<'a>(
target_name: &str,
args: impl IntoIterator<Item = &'a str>,
) -> crate::Result<std::path::PathBuf> {
) -> crate::assert::Result<std::path::PathBuf> {
crate::debug!("Compiling example {}", target_name);
let messages = escargot::CargoBuild::new()
.current_target()
.current_release()
.example(target_name)
.args(args)
.exec()
.map_err(|e| crate::Error::new(e.to_string()))?;
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
for message in messages {
let message = message.map_err(|e| crate::Error::new(e.to_string()))?;
let message = message.map_err(|e| crate::assert::Error::new(e.to_string()))?;
let message = message
.decode()
.map_err(|e| crate::Error::new(e.to_string()))?;
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
crate::debug!("Message: {:?}", message);
if let Some(bin) = decode_example_message(&message) {
let (name, bin) = bin?;
Expand All @@ -951,7 +951,7 @@ pub(crate) mod examples {
}
}

Err(crate::Error::new(format!(
Err(crate::assert::Error::new(format!(
"Unknown error building example {}",
target_name
)))
Expand All @@ -971,7 +971,9 @@ pub(crate) mod examples {
#[cfg(feature = "examples")]
pub fn compile_examples<'a>(
args: impl IntoIterator<Item = &'a str>,
) -> crate::Result<impl Iterator<Item = (String, crate::Result<std::path::PathBuf>)>> {
) -> crate::assert::Result<
impl Iterator<Item = (String, crate::assert::Result<std::path::PathBuf>)>,
> {
crate::debug!("Compiling examples");
let mut examples = std::collections::BTreeMap::new();

Expand All @@ -981,12 +983,12 @@ pub(crate) mod examples {
.examples()
.args(args)
.exec()
.map_err(|e| crate::Error::new(e.to_string()))?;
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
for message in messages {
let message = message.map_err(|e| crate::Error::new(e.to_string()))?;
let message = message.map_err(|e| crate::assert::Error::new(e.to_string()))?;
let message = message
.decode()
.map_err(|e| crate::Error::new(e.to_string()))?;
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
crate::debug!("Message: {:?}", message);
if let Some(bin) = decode_example_message(&message) {
let (name, bin) = bin?;
Expand All @@ -1000,7 +1002,7 @@ pub(crate) mod examples {
#[allow(clippy::type_complexity)]
fn decode_example_message<'m>(
message: &'m escargot::format::Message,
) -> Option<crate::Result<(&'m str, crate::Result<std::path::PathBuf>)>> {
) -> Option<crate::assert::Result<(&'m str, crate::assert::Result<std::path::PathBuf>)>> {
match message {
escargot::format::Message::CompilerMessage(msg) => {
let level = msg.message.level;
Expand All @@ -1014,10 +1016,10 @@ pub(crate) mod examples {
.unwrap_or_else(|| msg.message.message.as_ref())
.to_owned();
if is_example_target(&msg.target) {
let bin = Err(crate::Error::new(output));
let bin = Err(crate::assert::Error::new(output));
Some(Ok((msg.target.name.as_ref(), bin)))
} else {
Some(Err(crate::Error::new(output)))
Some(Err(crate::assert::Error::new(output)))
}
} else {
None
Expand Down
14 changes: 7 additions & 7 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl Data {
Self::with_inner(DataInner::Json(raw.into()))
}

fn error(raw: impl Into<crate::Error>, intended: DataFormat) -> Self {
fn error(raw: impl Into<crate::assert::Error>, intended: DataFormat) -> Self {
Self::with_inner(DataInner::Error(DataError {
error: raw.into(),
intended,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Data {
pub fn try_read_from(
path: &std::path::Path,
data_format: Option<DataFormat>,
) -> crate::Result<Self> {
) -> crate::assert::Result<Self> {
let data =
std::fs::read(path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
let data = Self::binary(data);
Expand All @@ -228,7 +228,7 @@ impl Data {
}

/// Overwrite a snapshot
pub fn write_to(&self, source: &DataSource) -> crate::Result<()> {
pub fn write_to(&self, source: &DataSource) -> crate::assert::Result<()> {
match &source.inner {
source::DataSourceInner::Path(p) => self.write_to_path(p),
source::DataSourceInner::Inline(p) => runtime::get()
Expand All @@ -238,7 +238,7 @@ impl Data {
}

/// Overwrite a snapshot
pub fn write_to_path(&self, path: &std::path::Path) -> crate::Result<()> {
pub fn write_to_path(&self, path: &std::path::Path) -> crate::assert::Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| {
format!("Failed to create parent dir for {}: {}", path.display(), e)
Expand Down Expand Up @@ -272,7 +272,7 @@ impl Data {
}
}

pub fn to_bytes(&self) -> crate::Result<Vec<u8>> {
pub fn to_bytes(&self) -> crate::assert::Result<Vec<u8>> {
match &self.inner {
DataInner::Error(err) => Err(err.error.clone()),
DataInner::Binary(data) => Ok(data.clone()),
Expand All @@ -296,7 +296,7 @@ impl Data {
}
}

fn try_is(self, format: DataFormat) -> crate::Result<Self> {
fn try_is(self, format: DataFormat) -> crate::assert::Result<Self> {
let original = self.format();
let source = self.source;
let inner = match (self.inner, format) {
Expand Down Expand Up @@ -491,7 +491,7 @@ impl PartialEq for Data {

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

Expand Down
10 changes: 7 additions & 3 deletions crates/snapbox/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ impl Verifier {
self
}

fn verify(&self, expected_path: &std::path::Path, actual: crate::Data) -> crate::Result<()> {
fn verify(
&self,
expected_path: &std::path::Path,
actual: crate::Data,
) -> crate::assert::Result<()> {
match self.action {
Action::Skip => Ok(()),
Action::Ignore => {
Expand All @@ -179,7 +183,7 @@ impl Verifier {
&self,
expected_path: &std::path::Path,
actual: crate::Data,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
actual.write_to_path(expected_path)?;
Ok(())
}
Expand All @@ -188,7 +192,7 @@ impl Verifier {
&self,
expected_path: &std::path::Path,
actual: crate::Data,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
let expected = FilterNewlines.filter(crate::Data::read_from(
expected_path,
Some(DataFormat::Text),
Expand Down
14 changes: 7 additions & 7 deletions crates/snapbox/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PathFixture {
}

#[cfg(feature = "path")]
pub fn mutable_temp() -> crate::Result<Self> {
pub fn mutable_temp() -> crate::assert::Result<Self> {
let temp = tempfile::tempdir().map_err(|e| e.to_string())?;
// We need to get the `/private` prefix on Mac so variable substitutions work
// correctly
Expand All @@ -47,15 +47,15 @@ impl PathFixture {
}

#[cfg(feature = "path")]
pub fn mutable_at(target: &std::path::Path) -> crate::Result<Self> {
pub fn mutable_at(target: &std::path::Path) -> crate::assert::Result<Self> {
let _ = std::fs::remove_dir_all(target);
std::fs::create_dir_all(target)
.map_err(|e| format!("Failed to create {}: {}", target.display(), e))?;
Ok(Self(PathFixtureInner::MutablePath(target.to_owned())))
}

#[cfg(feature = "path")]
pub fn with_template(self, template_root: &std::path::Path) -> crate::Result<Self> {
pub fn with_template(self, template_root: &std::path::Path) -> crate::assert::Result<Self> {
match &self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => {
return Err("Sandboxing is disabled".into());
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Default for PathFixture {

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PathDiff {
Failure(crate::Error),
Failure(crate::assert::Error),
TypeMismatch {
expected_path: std::path::PathBuf,
actual_path: std::path::PathBuf,
Expand Down Expand Up @@ -373,7 +373,7 @@ impl PathDiff {
Ok(())
}

pub fn overwrite(&self) -> crate::Result<()> {
pub fn overwrite(&self) -> crate::assert::Result<()> {
match self {
// Not passing the error up because users most likely want to treat a processing error
// differently than an overwrite error
Expand Down Expand Up @@ -513,7 +513,7 @@ impl Iterator for Walk {
pub fn copy_template(
source: impl AsRef<std::path::Path>,
dest: impl AsRef<std::path::Path>,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
let source = source.as_ref();
let dest = dest.as_ref();
let source = canonicalize(source)
Expand All @@ -535,7 +535,7 @@ pub fn copy_template(
}

/// Copy a file system entry, without recursing
fn shallow_copy(source: &std::path::Path, dest: &std::path::Path) -> crate::Result<()> {
fn shallow_copy(source: &std::path::Path, dest: &std::path::Path) -> crate::assert::Result<()> {
let meta = source
.symlink_metadata()
.map_err(|e| format!("Failed to read metadata from {}: {}", source.display(), e))?;
Expand Down
8 changes: 4 additions & 4 deletions crates/snapbox/src/substitutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Substitutions {
&mut self,
key: &'static str,
value: impl Into<Cow<'static, str>>,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
let key = validate_key(key)?;
let value = value.into();
if value.is_empty() {
Expand All @@ -56,14 +56,14 @@ impl Substitutions {
pub fn extend(
&mut self,
vars: impl IntoIterator<Item = (&'static str, impl Into<Cow<'static, str>>)>,
) -> crate::Result<()> {
) -> crate::assert::Result<()> {
for (key, value) in vars {
self.insert(key, value)?;
}
Ok(())
}

pub fn remove(&mut self, key: &'static str) -> crate::Result<()> {
pub fn remove(&mut self, key: &'static str) -> crate::assert::Result<()> {
let key = validate_key(key)?;
self.vars.remove(key);
Ok(())
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Substitutions {
}
}

fn validate_key(key: &'static str) -> crate::Result<&'static str> {
fn validate_key(key: &'static str) -> crate::assert::Result<&'static str> {
if !key.starts_with('[') || !key.ends_with(']') {
return Err(format!("Key `{}` is not enclosed in []", key).into());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trycmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ mod runner;
mod spec;

pub use cases::TestCases;
pub use snapbox::Error;
pub use snapbox::assert::Error;

pub(crate) use registry::BinRegistry;
pub(crate) use runner::{Case, Mode, Runner};
Expand Down
2 changes: 1 addition & 1 deletion crates/trycmd/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn overwrite_trycmd_status(
step: &Step,
stdout_line_nums: &mut std::ops::Range<usize>,
normalized: &mut String,
) -> Result<(), snapbox::Error> {
) -> Result<(), crate::Error> {
let status = match exit {
Some(status) => status,
_ => {
Expand Down

0 comments on commit b0553d4

Please sign in to comment.