Skip to content

Commit 3e293f6

Browse files
authored
Merge pull request #294 from epage/assert
fix(assert)!: Organize all Assert logic together
2 parents 5a4da90 + 41bfb95 commit 3e293f6

File tree

11 files changed

+66
-55
lines changed

11 files changed

+66
-55
lines changed
File renamed without changes.

crates/snapbox/src/error.rs renamed to crates/snapbox/src/assert/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub type Result<T, E = Error> = std::result::Result<T, E>;
2+
13
#[derive(Clone, Debug)]
24
pub struct Error {
35
inner: String,

crates/snapbox/src/assert.rs renamed to crates/snapbox/src/assert/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
mod action;
2+
mod error;
3+
14
#[cfg(feature = "color")]
25
use anstream::panic;
36
#[cfg(feature = "color")]
@@ -6,9 +9,13 @@ use anstream::stderr;
69
use std::io::stderr;
710

811
use crate::filters::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};
9-
use crate::Action;
1012
use crate::IntoData;
1113

14+
pub use action::Action;
15+
pub use action::DEFAULT_ACTION_ENV;
16+
pub use error::Error;
17+
pub use error::Result;
18+
1219
/// Snapshot assertion against a file's contents
1320
///
1421
/// Useful for one-off assertions with the snapshot stored in a file
@@ -75,7 +82,7 @@ impl Assert {
7582
expected: crate::Data,
7683
actual: crate::Data,
7784
actual_name: Option<&dyn std::fmt::Display>,
78-
) -> Result<(), crate::Error> {
85+
) -> Result<()> {
7986
if expected.source().is_none() && actual.source().is_some() {
8087
panic!("received `(actual, expected)`, expected `(expected, actual)`");
8188
}
@@ -122,7 +129,7 @@ impl Assert {
122129
expected: crate::Data,
123130
actual: crate::Data,
124131
actual_name: Option<&dyn std::fmt::Display>,
125-
) -> Result<(), crate::Error> {
132+
) -> Result<()> {
126133
let result = self.try_verify(&expected, &actual, actual_name);
127134
let Err(err) = result else {
128135
return Ok(());
@@ -149,7 +156,7 @@ impl Assert {
149156
} else {
150157
crate::report::Styled::new(String::new(), Default::default())
151158
};
152-
Err(crate::Error::new(format_args!("{err}{message}")))
159+
Err(Error::new(format_args!("{err}{message}")))
153160
}
154161
Action::Overwrite => {
155162
use std::io::Write;
@@ -159,7 +166,7 @@ impl Assert {
159166
actual.write_to(source).unwrap();
160167
Ok(())
161168
} else {
162-
Err(crate::Error::new(format_args!("{err}")))
169+
Err(Error::new(format_args!("{err}")))
163170
}
164171
}
165172
}
@@ -170,7 +177,7 @@ impl Assert {
170177
expected: &crate::Data,
171178
actual: &crate::Data,
172179
actual_name: Option<&dyn std::fmt::Display>,
173-
) -> crate::Result<()> {
180+
) -> Result<()> {
174181
if expected != actual {
175182
let mut buf = String::new();
176183
crate::report::write_diff(

crates/snapbox/src/cmd.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Command {
2323
stdin: None,
2424
timeout: None,
2525
_stderr_to_stdout: false,
26-
config: crate::Assert::new().action_env(crate::DEFAULT_ACTION_ENV),
26+
config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV),
2727
}
2828
}
2929

@@ -34,7 +34,7 @@ impl Command {
3434
stdin: None,
3535
timeout: None,
3636
_stderr_to_stdout: false,
37-
config: crate::Assert::new().action_env(crate::DEFAULT_ACTION_ENV),
37+
config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV),
3838
}
3939
}
4040

@@ -456,7 +456,7 @@ impl OutputAssert {
456456
pub fn new(output: std::process::Output) -> Self {
457457
Self {
458458
output,
459-
config: crate::Assert::new().action_env(crate::DEFAULT_ACTION_ENV),
459+
config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV),
460460
}
461461
}
462462

@@ -871,20 +871,20 @@ pub(crate) mod examples {
871871
pub fn compile_example<'a>(
872872
target_name: &str,
873873
args: impl IntoIterator<Item = &'a str>,
874-
) -> Result<std::path::PathBuf, crate::Error> {
874+
) -> Result<std::path::PathBuf, crate::assert::Error> {
875875
crate::debug!("Compiling example {}", target_name);
876876
let messages = escargot::CargoBuild::new()
877877
.current_target()
878878
.current_release()
879879
.example(target_name)
880880
.args(args)
881881
.exec()
882-
.map_err(|e| crate::Error::new(e.to_string()))?;
882+
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
883883
for message in messages {
884-
let message = message.map_err(|e| crate::Error::new(e.to_string()))?;
884+
let message = message.map_err(|e| crate::assert::Error::new(e.to_string()))?;
885885
let message = message
886886
.decode()
887-
.map_err(|e| crate::Error::new(e.to_string()))?;
887+
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
888888
crate::debug!("Message: {:?}", message);
889889
if let Some(bin) = decode_example_message(&message) {
890890
let (name, bin) = bin?;
@@ -893,7 +893,7 @@ pub(crate) mod examples {
893893
}
894894
}
895895

896-
Err(crate::Error::new(format!(
896+
Err(crate::assert::Error::new(format!(
897897
"Unknown error building example {}",
898898
target_name
899899
)))
@@ -914,8 +914,8 @@ pub(crate) mod examples {
914914
pub fn compile_examples<'a>(
915915
args: impl IntoIterator<Item = &'a str>,
916916
) -> Result<
917-
impl Iterator<Item = (String, Result<std::path::PathBuf, crate::Error>)>,
918-
crate::Error,
917+
impl Iterator<Item = (String, Result<std::path::PathBuf, crate::assert::Error>)>,
918+
crate::assert::Error,
919919
> {
920920
crate::debug!("Compiling examples");
921921
let mut examples = std::collections::BTreeMap::new();
@@ -926,12 +926,12 @@ pub(crate) mod examples {
926926
.examples()
927927
.args(args)
928928
.exec()
929-
.map_err(|e| crate::Error::new(e.to_string()))?;
929+
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
930930
for message in messages {
931-
let message = message.map_err(|e| crate::Error::new(e.to_string()))?;
931+
let message = message.map_err(|e| crate::assert::Error::new(e.to_string()))?;
932932
let message = message
933933
.decode()
934-
.map_err(|e| crate::Error::new(e.to_string()))?;
934+
.map_err(|e| crate::assert::Error::new(e.to_string()))?;
935935
crate::debug!("Message: {:?}", message);
936936
if let Some(bin) = decode_example_message(&message) {
937937
let (name, bin) = bin?;
@@ -945,7 +945,9 @@ pub(crate) mod examples {
945945
#[allow(clippy::type_complexity)]
946946
fn decode_example_message<'m>(
947947
message: &'m escargot::format::Message,
948-
) -> Option<Result<(&'m str, Result<std::path::PathBuf, crate::Error>), crate::Error>> {
948+
) -> Option<
949+
Result<(&'m str, Result<std::path::PathBuf, crate::assert::Error>), crate::assert::Error>,
950+
> {
949951
match message {
950952
escargot::format::Message::CompilerMessage(msg) => {
951953
let level = msg.message.level;
@@ -959,10 +961,10 @@ pub(crate) mod examples {
959961
.unwrap_or_else(|| msg.message.message.as_ref())
960962
.to_owned();
961963
if is_example_target(&msg.target) {
962-
let bin = Err(crate::Error::new(output));
964+
let bin = Err(crate::assert::Error::new(output));
963965
Some(Ok((msg.target.name.as_ref(), bin)))
964966
} else {
965-
Some(Err(crate::Error::new(output)))
967+
Some(Err(crate::assert::Error::new(output)))
966968
}
967969
} else {
968970
None

crates/snapbox/src/data/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl Data {
269269
Self::with_inner(DataInner::JsonLines(serde_json::Value::Array(raw.into())))
270270
}
271271

272-
fn error(raw: impl Into<crate::Error>, intended: DataFormat) -> Self {
272+
fn error(raw: impl Into<crate::assert::Error>, intended: DataFormat) -> Self {
273273
Self::with_inner(DataInner::Error(DataError {
274274
error: raw.into(),
275275
intended,
@@ -322,7 +322,7 @@ impl Data {
322322
pub fn try_read_from(
323323
path: &std::path::Path,
324324
data_format: Option<DataFormat>,
325-
) -> Result<Self, crate::Error> {
325+
) -> Result<Self, crate::assert::Error> {
326326
let data =
327327
std::fs::read(path).map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
328328
let data = Self::binary(data);
@@ -346,7 +346,7 @@ impl Data {
346346
}
347347

348348
/// Overwrite a snapshot
349-
pub fn write_to(&self, source: &DataSource) -> Result<(), crate::Error> {
349+
pub fn write_to(&self, source: &DataSource) -> Result<(), crate::assert::Error> {
350350
match &source.inner {
351351
source::DataSourceInner::Path(p) => self.write_to_path(p),
352352
source::DataSourceInner::Inline(p) => runtime::get()
@@ -356,7 +356,7 @@ impl Data {
356356
}
357357

358358
/// Overwrite a snapshot
359-
pub fn write_to_path(&self, path: &std::path::Path) -> Result<(), crate::Error> {
359+
pub fn write_to_path(&self, path: &std::path::Path) -> Result<(), crate::assert::Error> {
360360
if let Some(parent) = path.parent() {
361361
std::fs::create_dir_all(parent).map_err(|e| {
362362
format!("Failed to create parent dir for {}: {}", path.display(), e)
@@ -384,7 +384,7 @@ impl Data {
384384
}
385385
}
386386

387-
pub fn to_bytes(&self) -> Result<Vec<u8>, crate::Error> {
387+
pub fn to_bytes(&self) -> Result<Vec<u8>, crate::assert::Error> {
388388
match &self.inner {
389389
DataInner::Error(err) => Err(err.error.clone()),
390390
DataInner::Binary(data) => Ok(data.clone()),
@@ -408,7 +408,7 @@ impl Data {
408408
}
409409
}
410410

411-
fn try_is(self, format: DataFormat) -> Result<Self, crate::Error> {
411+
fn try_is(self, format: DataFormat) -> Result<Self, crate::assert::Error> {
412412
let original = self.format();
413413
let source = self.source;
414414
let filters = self.filters;
@@ -650,7 +650,7 @@ impl PartialEq for Data {
650650

651651
#[derive(Clone, Debug, PartialEq, Eq)]
652652
pub(crate) struct DataError {
653-
error: crate::Error,
653+
error: crate::assert::Error,
654654
intended: DataFormat,
655655
}
656656

crates/snapbox/src/filters/redactions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Redactions {
4949
&mut self,
5050
placeholder: &'static str,
5151
value: impl Into<RedactedValue>,
52-
) -> Result<(), crate::Error> {
52+
) -> Result<(), crate::assert::Error> {
5353
let placeholder = validate_placeholder(placeholder)?;
5454
let value = value.into();
5555
if let Some(inner) = value.inner {
@@ -66,14 +66,14 @@ impl Redactions {
6666
pub fn extend(
6767
&mut self,
6868
vars: impl IntoIterator<Item = (&'static str, impl Into<RedactedValue>)>,
69-
) -> Result<(), crate::Error> {
69+
) -> Result<(), crate::assert::Error> {
7070
for (placeholder, value) in vars {
7171
self.insert(placeholder, value)?;
7272
}
7373
Ok(())
7474
}
7575

76-
pub fn remove(&mut self, placeholder: &'static str) -> Result<(), crate::Error> {
76+
pub fn remove(&mut self, placeholder: &'static str) -> Result<(), crate::assert::Error> {
7777
let placeholder = validate_placeholder(placeholder)?;
7878
self.vars.remove(placeholder);
7979
Ok(())
@@ -291,7 +291,7 @@ fn replace_many<'a>(
291291
}
292292
}
293293

294-
fn validate_placeholder(placeholder: &'static str) -> Result<&'static str, crate::Error> {
294+
fn validate_placeholder(placeholder: &'static str) -> Result<&'static str, crate::assert::Error> {
295295
if !placeholder.starts_with('[') || !placeholder.ends_with(']') {
296296
return Err(format!("Key `{}` is not enclosed in []", placeholder).into());
297297
}

crates/snapbox/src/harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! }
3636
//! ```
3737
38-
use crate::Action;
38+
use crate::assert::Action;
3939
use crate::Data;
4040

4141
use libtest_mimic::Trial;
@@ -81,7 +81,7 @@ where
8181
overrides: None,
8282
setup,
8383
test,
84-
config: crate::Assert::new().action_env(crate::DEFAULT_ACTION_ENV),
84+
config: crate::Assert::new().action_env(crate::assert::DEFAULT_ACTION_ENV),
8585
test_output: Default::default(),
8686
test_error: Default::default(),
8787
}

crates/snapbox/src/lib.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@
9494
9595
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
9696

97-
mod action;
98-
mod assert;
99-
mod error;
10097
mod macros;
10198

99+
pub mod assert;
102100
pub mod cmd;
103101
pub mod data;
104102
pub mod filters;
@@ -109,20 +107,16 @@ pub mod utils;
109107
#[cfg(feature = "harness")]
110108
pub mod harness;
111109

112-
pub use action::Action;
113-
pub use action::DEFAULT_ACTION_ENV;
114110
pub use assert::Assert;
115111
pub use data::Data;
116112
pub use data::IntoData;
117113
#[cfg(feature = "json")]
118114
pub use data::IntoJson;
119115
pub use data::ToDebug;
120-
pub use error::Error;
121116
pub use filters::Redactions;
117+
#[doc(hidden)]
122118
pub use snapbox_macros::debug;
123119

124-
pub type Result<T, E = Error> = std::result::Result<T, E>;
125-
126120
/// Easier access to common traits
127121
pub mod prelude {
128122
pub use crate::IntoData;
@@ -159,7 +153,7 @@ pub mod prelude {
159153
#[track_caller]
160154
pub fn assert_eq(expected: impl IntoData, actual: impl IntoData) {
161155
Assert::new()
162-
.action_env(DEFAULT_ACTION_ENV)
156+
.action_env(assert::DEFAULT_ACTION_ENV)
163157
.eq(expected, actual);
164158
}
165159

@@ -179,7 +173,7 @@ pub fn assert_subset_eq(
179173
actual_root: impl Into<std::path::PathBuf>,
180174
) {
181175
Assert::new()
182-
.action_env(DEFAULT_ACTION_ENV)
176+
.action_env(assert::DEFAULT_ACTION_ENV)
183177
.subset_eq(expected_root, actual_root);
184178
}
185179

@@ -206,6 +200,6 @@ pub fn assert_subset_matches(
206200
actual_root: impl Into<std::path::PathBuf>,
207201
) {
208202
Assert::new()
209-
.action_env(DEFAULT_ACTION_ENV)
203+
.action_env(assert::DEFAULT_ACTION_ENV)
210204
.subset_matches(pattern_root, actual_root);
211205
}

0 commit comments

Comments
 (0)