From 3ba612b70951b770d2e4e23311a5a02de557e545 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 14 Feb 2024 11:12:29 -0600 Subject: [PATCH 1/3] fix(snap)!: Move most data types to data mod --- crates/snapbox/src/data/mod.rs | 6 +++--- crates/snapbox/src/lib.rs | 5 +---- src/runner.rs | 12 +++++++----- src/schema.rs | 6 +++--- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index b2123c81..8c36b733 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -35,11 +35,11 @@ macro_rules! file { }}; [_ : $type:ident] => {{ let stem = ::std::path::Path::new(::std::file!()).file_stem().unwrap(); - let ext = $crate::DataFormat:: $type.ext(); + let ext = $crate::data::DataFormat:: $type.ext(); let rel_path = ::std::format!("snapshots/{}-{}.{ext}", stem.to_str().unwrap(), line!()); let mut path = $crate::current_dir!(); path.push(rel_path); - $crate::Data::read_from(&path, Some($crate::DataFormat:: $type)) + $crate::Data::read_from(&path, Some($crate::data::DataFormat:: $type)) }}; [$path:literal] => {{ let mut path = $crate::current_dir!(); @@ -49,7 +49,7 @@ macro_rules! file { [$path:literal : $type:ident] => {{ let mut path = $crate::current_dir!(); path.push($path); - $crate::Data::read_from(&path, Some($crate::DataFormat:: $type)) + $crate::Data::read_from(&path, Some($crate::data::DataFormat:: $type)) }}; } diff --git a/crates/snapbox/src/lib.rs b/crates/snapbox/src/lib.rs index a83b5779..5c25422e 100644 --- a/crates/snapbox/src/lib.rs +++ b/crates/snapbox/src/lib.rs @@ -95,12 +95,12 @@ mod action; mod assert; -mod data; mod error; mod macros; mod substitutions; pub mod cmd; +pub mod data; pub mod path; pub mod report; pub mod utils; @@ -112,9 +112,6 @@ pub use action::Action; pub use action::DEFAULT_ACTION_ENV; pub use assert::Assert; pub use data::Data; -pub use data::DataFormat; -pub use data::DataSource; -pub use data::{Normalize, NormalizeMatches, NormalizeNewlines, NormalizePaths}; pub use error::Error; pub use snapbox_macros::debug; pub use substitutions::Substitutions; diff --git a/src/runner.rs b/src/runner.rs index 23c4572c..68fb3515 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -12,8 +12,8 @@ use std::eprintln; use std::io::stderr; use rayon::prelude::*; +use snapbox::data::{DataFormat, NormalizeNewlines, NormalizePaths}; use snapbox::path::FileType; -use snapbox::{DataFormat, NormalizeNewlines, NormalizePaths}; #[derive(Debug)] pub(crate) struct Runner { @@ -441,10 +441,12 @@ impl Case { } if let Some(expected_content) = expected_content { - stream.content = stream.content.normalize(snapbox::NormalizeMatches::new( - substitutions, - expected_content, - )); + stream.content = stream + .content + .normalize(snapbox::data::NormalizeMatches::new( + substitutions, + expected_content, + )); if stream.content != *expected_content { stream.status = StreamStatus::Expected(expected_content.clone()); diff --git a/src/schema.rs b/src/schema.rs index d15d5e14..e2886313 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -2,7 +2,7 @@ //! //! [`OneShot`] is the top-level item in the `cmd.toml` files. -use snapbox::{NormalizeNewlines, NormalizePaths}; +use snapbox::data::{NormalizeNewlines, NormalizePaths}; use std::collections::BTreeMap; use std::collections::VecDeque; @@ -21,8 +21,8 @@ impl TryCmd { let one_shot = OneShot::parse_toml(&raw)?; let mut sequence: Self = one_shot.into(); let is_binary = match sequence.steps[0].binary { - true => snapbox::DataFormat::Binary, - false => snapbox::DataFormat::Text, + true => snapbox::data::DataFormat::Binary, + false => snapbox::data::DataFormat::Text, }; if sequence.steps[0].stdin.is_none() { From febb9f6eee2ec78dcce7e77d821c432b5fa547a9 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 14 Feb 2024 12:33:08 -0600 Subject: [PATCH 2/3] fix(snap): Simplify Data::coerce_tos name --- crates/snapbox/src/assert.rs | 4 ++-- crates/snapbox/src/data/mod.rs | 10 +++++----- crates/snapbox/src/data/tests.rs | 26 +++++++++++++------------- crates/snapbox/src/path.rs | 4 ++-- src/runner.rs | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/crates/snapbox/src/assert.rs b/crates/snapbox/src/assert.rs index e3b05a04..2736ed8f 100644 --- a/crates/snapbox/src/assert.rs +++ b/crates/snapbox/src/assert.rs @@ -129,7 +129,7 @@ impl Assert { // On `expected` being an error, make a best guess let format = expected.format(); - actual = actual.try_coerce(format).normalize(NormalizeNewlines); + actual = actual.coerce_to(format).normalize(NormalizeNewlines); (expected, actual) } @@ -142,7 +142,7 @@ impl Assert { let expected = expected.normalize(NormalizeNewlines); // On `expected` being an error, make a best guess let format = expected.format(); - actual = actual.try_coerce(format); + actual = actual.coerce_to(format); if self.normalize_paths { actual = actual.normalize(NormalizePaths); diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 8c36b733..f96007d6 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -156,8 +156,8 @@ impl Data { .unwrap_or_default() { #[cfg(feature = "json")] - "json" => data.try_coerce(DataFormat::Json), - _ => data.try_coerce(DataFormat::Text), + "json" => data.coerce_to(DataFormat::Json), + _ => data.coerce_to(DataFormat::Text), } } }; @@ -220,7 +220,7 @@ impl Data { } } - pub fn try_coerce(self, format: DataFormat) -> Self { + pub fn coerce_to(self, format: DataFormat) -> Self { let mut data = match (self.inner, format) { (DataInner::Error(inner), _) => Self::error(inner), (inner, DataFormat::Error) => Self { @@ -237,11 +237,11 @@ impl Data { } else { match String::from_utf8(inner) { Ok(str) => { - let coerced = Self::text(str).try_coerce(format); + let coerced = Self::text(str).coerce_to(format); // if the Text cannot be coerced into the correct format // reset it back to Binary if coerced.format() != format { - coerced.try_coerce(DataFormat::Binary) + coerced.coerce_to(DataFormat::Binary) } else { coerced } diff --git a/crates/snapbox/src/data/tests.rs b/crates/snapbox/src/data/tests.rs index bd53707e..e05879cf 100644 --- a/crates/snapbox/src/data/tests.rs +++ b/crates/snapbox/src/data/tests.rs @@ -28,7 +28,7 @@ fn json_to_bytes_render() { fn binary_to_text() { let binary = String::from("test").into_bytes(); let d = Data::binary(binary); - let text = d.try_coerce(DataFormat::Text); + let text = d.coerce_to(DataFormat::Text); assert_eq!(DataFormat::Text, text.format()) } @@ -36,7 +36,7 @@ fn binary_to_text() { fn binary_to_text_not_utf8() { let binary = b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00".to_vec(); let d = Data::binary(binary); - let d = d.try_coerce(DataFormat::Text); + let d = d.coerce_to(DataFormat::Text); assert_ne!(DataFormat::Text, d.format()); assert_eq!(DataFormat::Binary, d.format()); } @@ -47,7 +47,7 @@ fn binary_to_json() { let value = json!({"name": "John\\Doe\r\n"}); let binary = serde_json::to_vec_pretty(&value).unwrap(); let d = Data::binary(binary); - let json = d.try_coerce(DataFormat::Json); + let json = d.coerce_to(DataFormat::Json); assert_eq!(DataFormat::Json, json.format()); } @@ -56,7 +56,7 @@ fn binary_to_json() { fn binary_to_json_not_utf8() { let binary = b"\xFF\xE0\x00\x10\x4A\x46\x49\x46\x00".to_vec(); let d = Data::binary(binary); - let d = d.try_coerce(DataFormat::Json); + let d = d.coerce_to(DataFormat::Json); assert_ne!(DataFormat::Json, d.format()); assert_eq!(DataFormat::Binary, d.format()); } @@ -66,7 +66,7 @@ fn binary_to_json_not_utf8() { fn binary_to_json_not_json() { let binary = String::from("test").into_bytes(); let d = Data::binary(binary); - let d = d.try_coerce(DataFormat::Json); + let d = d.coerce_to(DataFormat::Json); assert_ne!(DataFormat::Json, d.format()); assert_eq!(DataFormat::Binary, d.format()); } @@ -75,7 +75,7 @@ fn binary_to_json_not_json() { fn text_to_binary() { let text = String::from("test"); let d = Data::text(text); - let binary = d.try_coerce(DataFormat::Binary); + let binary = d.coerce_to(DataFormat::Binary); assert_eq!(DataFormat::Binary, binary.format()); } @@ -85,7 +85,7 @@ fn text_to_json() { let value = json!({"name": "John\\Doe\r\n"}); let text = serde_json::to_string_pretty(&value).unwrap(); let d = Data::text(text); - let json = d.try_coerce(DataFormat::Json); + let json = d.coerce_to(DataFormat::Json); assert_eq!(DataFormat::Json, json.format()); } @@ -94,7 +94,7 @@ fn text_to_json() { fn text_to_json_not_json() { let text = String::from("test"); let d = Data::text(text); - let json = d.try_coerce(DataFormat::Json); + let json = d.coerce_to(DataFormat::Json); assert_eq!(DataFormat::Text, json.format()); } @@ -103,7 +103,7 @@ fn text_to_json_not_json() { fn json_to_binary() { let value = json!({"name": "John\\Doe\r\n"}); let d = Data::json(value); - let binary = d.try_coerce(DataFormat::Binary); + let binary = d.coerce_to(DataFormat::Binary); assert_eq!(DataFormat::Binary, binary.format()); } @@ -112,7 +112,7 @@ fn json_to_binary() { fn json_to_text() { let value = json!({"name": "John\\Doe\r\n"}); let d = Data::json(value); - let text = d.try_coerce(DataFormat::Text); + let text = d.coerce_to(DataFormat::Text); assert_eq!(DataFormat::Text, text.format()); } @@ -124,7 +124,7 @@ fn json_to_text() { fn text_to_bin_coerce_equals_to_bytes() { let text = String::from("test"); let d = Data::text(text); - let binary = d.clone().try_coerce(DataFormat::Binary); + let binary = d.clone().coerce_to(DataFormat::Binary); assert_eq!(Data::binary(d.to_bytes().unwrap()), binary); } @@ -133,7 +133,7 @@ fn text_to_bin_coerce_equals_to_bytes() { fn json_to_bin_coerce_equals_to_bytes() { let json = json!({"name": "John\\Doe\r\n"}); let d = Data::json(json); - let binary = d.clone().try_coerce(DataFormat::Binary); + let binary = d.clone().coerce_to(DataFormat::Binary); assert_eq!(Data::binary(d.to_bytes().unwrap()), binary); } @@ -142,7 +142,7 @@ fn json_to_bin_coerce_equals_to_bytes() { fn json_to_text_coerce_equals_render() { let json = json!({"name": "John\\Doe\r\n"}); let d = Data::json(json); - let text = d.clone().try_coerce(DataFormat::Text); + let text = d.clone().coerce_to(DataFormat::Text); assert_eq!(Data::text(d.render().unwrap()), text); } diff --git a/crates/snapbox/src/path.rs b/crates/snapbox/src/path.rs index a317a60c..13c804c0 100644 --- a/crates/snapbox/src/path.rs +++ b/crates/snapbox/src/path.rs @@ -189,7 +189,7 @@ impl PathDiff { crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines); actual = actual - .try_coerce(expected.format()) + .coerce_to(expected.format()) .normalize(NormalizeNewlines); if expected != actual { @@ -266,7 +266,7 @@ impl PathDiff { crate::Data::read_from(&expected_path, None).normalize(NormalizeNewlines); actual = actual - .try_coerce(expected.format()) + .coerce_to(expected.format()) .normalize(NormalizePaths) .normalize(NormalizeNewlines) .normalize(NormalizeMatches::new(substitutions, &expected)); diff --git a/src/runner.rs b/src/runner.rs index 68fb3515..d175cca4 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -736,7 +736,7 @@ struct Stream { impl Stream { fn make_text(mut self) -> Self { - let content = self.content.try_coerce(DataFormat::Text); + let content = self.content.coerce_to(DataFormat::Text); if content.format() != DataFormat::Text { self.status = StreamStatus::Failure("Unable to convert underlying Data to Text".into()); } From fafd6875dce71f0b28d837354fc0da2a1810d7d0 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 14 Feb 2024 12:39:20 -0600 Subject: [PATCH 3/3] feat(snap): Simplify DataSource creation --- crates/snapbox/src/data/mod.rs | 59 +++++++++++++------------------ crates/snapbox/src/data/source.rs | 12 +++++++ 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index f96007d6..0644fba9 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -74,33 +74,21 @@ enum DataInner { impl Data { /// Mark the data as binary (no post-processing) pub fn binary(raw: impl Into>) -> Self { - Self { - inner: DataInner::Binary(raw.into()), - source: None, - } + DataInner::Binary(raw.into()).into() } /// Mark the data as text (post-processing) pub fn text(raw: impl Into) -> Self { - Self { - inner: DataInner::Text(raw.into()), - source: None, - } + DataInner::Text(raw.into()).into() } #[cfg(feature = "json")] pub fn json(raw: impl Into) -> Self { - Self { - inner: DataInner::Json(raw.into()), - source: None, - } + DataInner::Json(raw.into()).into() } fn error(raw: impl Into) -> Self { - Self { - inner: DataInner::Error(raw.into()), - source: None, - } + DataInner::Error(raw.into()).into() } /// Empty test data @@ -108,11 +96,15 @@ impl Data { Self::text("") } - fn with_path(mut self, path: impl Into) -> Self { - self.source = Some(DataSource::path(path)); + fn with_source(mut self, source: impl Into) -> Self { + self.source = Some(source.into()); self } + fn with_path(self, path: impl Into) -> Self { + self.with_source(path.into()) + } + /// Load test data from a file pub fn read_from(path: &std::path::Path, data_format: Option) -> Self { match Self::try_read_from(path, data_format) { @@ -223,10 +215,7 @@ impl Data { pub fn coerce_to(self, format: DataFormat) -> Self { let mut data = match (self.inner, format) { (DataInner::Error(inner), _) => Self::error(inner), - (inner, DataFormat::Error) => Self { - inner, - source: None, - }, + (inner, DataFormat::Error) => inner.into(), (DataInner::Binary(inner), DataFormat::Binary) => Self::binary(inner), (DataInner::Text(inner), DataFormat::Text) => Self::text(inner), #[cfg(feature = "json")] @@ -260,21 +249,14 @@ impl Data { Err(_) => Self::text(inner), } } - (inner, DataFormat::Binary) => Self::binary( - Self { - inner, - source: None, - } - .to_bytes() - .expect("error case handled"), - ), + (inner, DataFormat::Binary) => { + let remake: Self = inner.into(); + Self::binary(remake.to_bytes().expect("error case handled")) + } // This variant is already covered unless structured data is enabled #[cfg(feature = "structured-data")] (inner, DataFormat::Text) => { - let remake = Self { - inner, - source: None, - }; + let remake: Self = inner.into(); if let Some(str) = remake.render() { Self::text(str) } else { @@ -298,6 +280,15 @@ impl Data { } } +impl From for Data { + fn from(inner: DataInner) -> Self { + Data { + inner, + source: None, + } + } +} + impl std::fmt::Display for Data { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.inner { diff --git a/crates/snapbox/src/data/source.rs b/crates/snapbox/src/data/source.rs index 1d57518b..8d02e02a 100644 --- a/crates/snapbox/src/data/source.rs +++ b/crates/snapbox/src/data/source.rs @@ -26,6 +26,18 @@ impl DataSource { } } +impl From<&'_ std::path::Path> for DataSource { + fn from(value: &'_ std::path::Path) -> Self { + Self::path(value) + } +} + +impl From for DataSource { + fn from(value: std::path::PathBuf) -> Self { + Self::path(value) + } +} + impl std::fmt::Display for DataSource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.inner {