From b0553d49744038b860358ca27729759eb41a09c3 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 May 2024 15:18:25 -0500 Subject: [PATCH] refactor: Transition to assert::Error --- crates/snapbox/src/assert/mod.rs | 2 +- crates/snapbox/src/cmd.rs | 26 ++++++++++++++------------ crates/snapbox/src/data/mod.rs | 14 +++++++------- crates/snapbox/src/harness.rs | 10 +++++++--- crates/snapbox/src/path.rs | 14 +++++++------- crates/snapbox/src/substitutions.rs | 8 ++++---- crates/trycmd/src/lib.rs | 2 +- crates/trycmd/src/schema.rs | 2 +- 8 files changed, 42 insertions(+), 36 deletions(-) diff --git a/crates/snapbox/src/assert/mod.rs b/crates/snapbox/src/assert/mod.rs index 13dbca4f..af1d9949 100644 --- a/crates/snapbox/src/assert/mod.rs +++ b/crates/snapbox/src/assert/mod.rs @@ -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( diff --git a/crates/snapbox/src/cmd.rs b/crates/snapbox/src/cmd.rs index be9cf839..e5091d04 100644 --- a/crates/snapbox/src/cmd.rs +++ b/crates/snapbox/src/cmd.rs @@ -929,7 +929,7 @@ pub(crate) mod examples { pub fn compile_example<'a>( target_name: &str, args: impl IntoIterator, - ) -> crate::Result { + ) -> crate::assert::Result { crate::debug!("Compiling example {}", target_name); let messages = escargot::CargoBuild::new() .current_target() @@ -937,12 +937,12 @@ pub(crate) mod examples { .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?; @@ -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 ))) @@ -971,7 +971,9 @@ pub(crate) mod examples { #[cfg(feature = "examples")] pub fn compile_examples<'a>( args: impl IntoIterator, - ) -> crate::Result)>> { + ) -> crate::assert::Result< + impl Iterator)>, + > { crate::debug!("Compiling examples"); let mut examples = std::collections::BTreeMap::new(); @@ -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?; @@ -1000,7 +1002,7 @@ pub(crate) mod examples { #[allow(clippy::type_complexity)] fn decode_example_message<'m>( message: &'m escargot::format::Message, - ) -> Option)>> { + ) -> Option)>> { match message { escargot::format::Message::CompilerMessage(msg) => { let level = msg.message.level; @@ -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 diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 885d82f8..c1656a16 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -158,7 +158,7 @@ impl Data { Self::with_inner(DataInner::Json(raw.into())) } - fn error(raw: impl Into, intended: DataFormat) -> Self { + fn error(raw: impl Into, intended: DataFormat) -> Self { Self::with_inner(DataInner::Error(DataError { error: raw.into(), intended, @@ -204,7 +204,7 @@ impl Data { pub fn try_read_from( path: &std::path::Path, data_format: Option, - ) -> crate::Result { + ) -> crate::assert::Result { let data = std::fs::read(path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; let data = Self::binary(data); @@ -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() @@ -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) @@ -272,7 +272,7 @@ impl Data { } } - pub fn to_bytes(&self) -> crate::Result> { + pub fn to_bytes(&self) -> crate::assert::Result> { match &self.inner { DataInner::Error(err) => Err(err.error.clone()), DataInner::Binary(data) => Ok(data.clone()), @@ -296,7 +296,7 @@ impl Data { } } - fn try_is(self, format: DataFormat) -> crate::Result { + fn try_is(self, format: DataFormat) -> crate::assert::Result { let original = self.format(); let source = self.source; let inner = match (self.inner, format) { @@ -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, } diff --git a/crates/snapbox/src/harness.rs b/crates/snapbox/src/harness.rs index fc9efb64..0ade60f7 100644 --- a/crates/snapbox/src/harness.rs +++ b/crates/snapbox/src/harness.rs @@ -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 => { @@ -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(()) } @@ -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), diff --git a/crates/snapbox/src/path.rs b/crates/snapbox/src/path.rs index 57f177ca..c327086d 100644 --- a/crates/snapbox/src/path.rs +++ b/crates/snapbox/src/path.rs @@ -37,7 +37,7 @@ impl PathFixture { } #[cfg(feature = "path")] - pub fn mutable_temp() -> crate::Result { + pub fn mutable_temp() -> crate::assert::Result { let temp = tempfile::tempdir().map_err(|e| e.to_string())?; // We need to get the `/private` prefix on Mac so variable substitutions work // correctly @@ -47,7 +47,7 @@ impl PathFixture { } #[cfg(feature = "path")] - pub fn mutable_at(target: &std::path::Path) -> crate::Result { + pub fn mutable_at(target: &std::path::Path) -> crate::assert::Result { let _ = std::fs::remove_dir_all(target); std::fs::create_dir_all(target) .map_err(|e| format!("Failed to create {}: {}", target.display(), e))?; @@ -55,7 +55,7 @@ impl PathFixture { } #[cfg(feature = "path")] - pub fn with_template(self, template_root: &std::path::Path) -> crate::Result { + pub fn with_template(self, template_root: &std::path::Path) -> crate::assert::Result { match &self.0 { PathFixtureInner::None | PathFixtureInner::Immutable(_) => { return Err("Sandboxing is disabled".into()); @@ -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, @@ -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 @@ -513,7 +513,7 @@ impl Iterator for Walk { pub fn copy_template( source: impl AsRef, dest: impl AsRef, -) -> crate::Result<()> { +) -> crate::assert::Result<()> { let source = source.as_ref(); let dest = dest.as_ref(); let source = canonicalize(source) @@ -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))?; diff --git a/crates/snapbox/src/substitutions.rs b/crates/snapbox/src/substitutions.rs index 3310c275..06bbd3f9 100644 --- a/crates/snapbox/src/substitutions.rs +++ b/crates/snapbox/src/substitutions.rs @@ -36,7 +36,7 @@ impl Substitutions { &mut self, key: &'static str, value: impl Into>, - ) -> crate::Result<()> { + ) -> crate::assert::Result<()> { let key = validate_key(key)?; let value = value.into(); if value.is_empty() { @@ -56,14 +56,14 @@ impl Substitutions { pub fn extend( &mut self, vars: impl IntoIterator>)>, - ) -> 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(()) @@ -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()); } diff --git a/crates/trycmd/src/lib.rs b/crates/trycmd/src/lib.rs index 70d9848a..8efd3a81 100644 --- a/crates/trycmd/src/lib.rs +++ b/crates/trycmd/src/lib.rs @@ -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}; diff --git a/crates/trycmd/src/schema.rs b/crates/trycmd/src/schema.rs index 7ad44f58..81dd0186 100644 --- a/crates/trycmd/src/schema.rs +++ b/crates/trycmd/src/schema.rs @@ -446,7 +446,7 @@ fn overwrite_trycmd_status( step: &Step, stdout_line_nums: &mut std::ops::Range, normalized: &mut String, -) -> Result<(), snapbox::Error> { +) -> Result<(), crate::Error> { let status = match exit { Some(status) => status, _ => {