diff options
Diffstat (limited to 'vendor/snapbox/src')
-rw-r--r-- | vendor/snapbox/src/bin/snap-fixture.rs | 3 | ||||
-rw-r--r-- | vendor/snapbox/src/cmd.rs | 2 | ||||
-rw-r--r-- | vendor/snapbox/src/data.rs | 270 | ||||
-rw-r--r-- | vendor/snapbox/src/lib.rs | 2 | ||||
-rw-r--r-- | vendor/snapbox/src/path.rs | 6 | ||||
-rw-r--r-- | vendor/snapbox/src/substitutions.rs | 2 |
6 files changed, 257 insertions, 28 deletions
diff --git a/vendor/snapbox/src/bin/snap-fixture.rs b/vendor/snapbox/src/bin/snap-fixture.rs index 6e13448a7..7e21fc647 100644 --- a/vendor/snapbox/src/bin/snap-fixture.rs +++ b/vendor/snapbox/src/bin/snap-fixture.rs @@ -43,7 +43,8 @@ fn run() -> Result<(), Box<dyn Error>> { let code = env::var("exit") .ok() .map(|v| v.parse::<i32>()) - .map_or(Ok(None), |r| r.map(Some))? + .map(|r| r.map(Some)) + .unwrap_or(Ok(None))? .unwrap_or(0); process::exit(code); } diff --git a/vendor/snapbox/src/cmd.rs b/vendor/snapbox/src/cmd.rs index 8529852f9..3b2a19f70 100644 --- a/vendor/snapbox/src/cmd.rs +++ b/vendor/snapbox/src/cmd.rs @@ -1013,7 +1013,7 @@ pub use snapbox_macros::cargo_bin; pub fn cargo_bin(name: &str) -> std::path::PathBuf { let file_name = format!("{}{}", name, std::env::consts::EXE_SUFFIX); let target_dir = target_dir(); - target_dir.join(&file_name) + target_dir.join(file_name) } // Adapted from diff --git a/vendor/snapbox/src/data.rs b/vendor/snapbox/src/data.rs index ab4432c1e..b892ffd1e 100644 --- a/vendor/snapbox/src/data.rs +++ b/vendor/snapbox/src/data.rs @@ -14,20 +14,15 @@ enum DataInner { Json(serde_json::Value), } -#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Copy, Hash, Default)] pub enum DataFormat { Binary, + #[default] Text, #[cfg(feature = "json")] Json, } -impl Default for DataFormat { - fn default() -> Self { - DataFormat::Text - } -} - impl Data { /// Mark the data as binary (no post-processing) pub fn binary(raw: impl Into<Vec<u8>>) -> Self { @@ -63,24 +58,24 @@ impl Data { let data = match data_format { Some(df) => match df { DataFormat::Binary => { - let data = std::fs::read(&path) + let data = std::fs::read(path) .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; Self::binary(data) } DataFormat::Text => { - let data = std::fs::read_to_string(&path) + let data = std::fs::read_to_string(path) .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; Self::text(data) } #[cfg(feature = "json")] DataFormat::Json => { - let data = std::fs::read_to_string(&path) + let data = std::fs::read_to_string(path) .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; Self::json(serde_json::from_str::<serde_json::Value>(&data).unwrap()) } }, None => { - let data = std::fs::read(&path) + let data = std::fs::read(path) .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?; let data = Self::binary(data); match path @@ -334,11 +329,14 @@ fn normalize_value(value: &mut serde_json::Value, op: fn(&str) -> String) { *str = op(str); } serde_json::Value::Array(arr) => { - arr.iter_mut().for_each(|value| normalize_value(value, op)); + for value in arr.iter_mut() { + normalize_value(value, op) + } } serde_json::Value::Object(obj) => { - obj.iter_mut() - .for_each(|(_, value)| normalize_value(value, op)); + for (_, value) in obj.iter_mut() { + normalize_value(value, op) + } } _ => {} } @@ -360,15 +358,44 @@ fn normalize_value_matches( *act = substitutions.normalize(act, exp); } (Array(act), Array(exp)) => { - act.iter_mut() - .zip(exp) - .for_each(|(a, e)| normalize_value_matches(a, e, substitutions)); + let wildcard = String("{...}".to_string()); + let mut sections = exp.split(|e| e == &wildcard).peekable(); + let mut processed = 0; + while let Some(expected_subset) = sections.next() { + // Process all values in the current section + if !expected_subset.is_empty() { + let actual_subset = &mut act[processed..processed + expected_subset.len()]; + for (a, e) in actual_subset.iter_mut().zip(expected_subset) { + normalize_value_matches(a, e, substitutions); + } + processed += expected_subset.len(); + } + + if let Some(next_section) = sections.peek() { + // If the next section has nothing in it, replace from processed to end with + // a single "{...}" + if next_section.is_empty() { + act.splice(processed.., vec![wildcard.clone()]); + processed += 1; + } else { + let first = next_section.first().unwrap(); + // Replace everything up until the value we are looking for with + // a single "{...}". + if let Some(index) = act.iter().position(|v| v == first) { + act.splice(processed..index, vec![wildcard.clone()]); + processed += 1; + } else { + // If we cannot find the value we are looking for return early + break; + } + } + } + } } (Object(act), Object(exp)) => { - act.iter_mut() - .zip(exp) - .filter(|(a, e)| a.0 == e.0) - .for_each(|(a, e)| normalize_value_matches(a.1, e.1, substitutions)); + for (a, e) in act.iter_mut().zip(exp).filter(|(a, e)| a.0 == e.0) { + normalize_value_matches(a.1, e.1, substitutions) + } } (_, _) => {} } @@ -709,4 +736,205 @@ mod test { assert_ne!(exp, act); } } + + #[test] + #[cfg(feature = "json")] + fn json_normalize_wildcard_object_first() { + let exp = json!({ + "people": [ + "{...}", + { + "name": "three", + "nickname": "3", + } + ] + }); + let expected = Data::json(exp); + let actual = json!({ + "people": [ + { + "name": "one", + "nickname": "1", + }, + { + "name": "two", + "nickname": "2", + }, + { + "name": "three", + "nickname": "3", + } + ] + }); + let actual = Data::json(actual).normalize(NormalizeMatches { + substitutions: &Default::default(), + pattern: &expected, + }); + if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) { + assert_eq!(exp, act); + } + } + + #[test] + #[cfg(feature = "json")] + fn json_normalize_wildcard_array_first() { + let exp = json!([ + "{...}", + { + "name": "three", + "nickname": "3", + } + ]); + let expected = Data::json(exp); + let actual = json!([ + { + "name": "one", + "nickname": "1", + }, + { + "name": "two", + "nickname": "2", + }, + { + "name": "three", + "nickname": "3", + } + ]); + let actual = Data::json(actual).normalize(NormalizeMatches { + substitutions: &Default::default(), + pattern: &expected, + }); + if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) { + assert_eq!(exp, act); + } + } + + #[test] + #[cfg(feature = "json")] + fn json_normalize_wildcard_array_first_last() { + let exp = json!([ + "{...}", + { + "name": "two", + "nickname": "2", + }, + "{...}" + ]); + let expected = Data::json(exp); + let actual = json!([ + { + "name": "one", + "nickname": "1", + }, + { + "name": "two", + "nickname": "2", + }, + { + "name": "three", + "nickname": "3", + }, + { + "name": "four", + "nickname": "4", + } + ]); + let actual = Data::json(actual).normalize(NormalizeMatches { + substitutions: &Default::default(), + pattern: &expected, + }); + if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) { + assert_eq!(exp, act); + } + } + + #[test] + #[cfg(feature = "json")] + fn json_normalize_wildcard_array_middle_last() { + let exp = json!([ + { + "name": "one", + "nickname": "1", + }, + "{...}", + { + "name": "three", + "nickname": "3", + }, + "{...}" + ]); + let expected = Data::json(exp); + let actual = json!([ + { + "name": "one", + "nickname": "1", + }, + { + "name": "two", + "nickname": "2", + }, + { + "name": "three", + "nickname": "3", + }, + { + "name": "four", + "nickname": "4", + }, + { + "name": "five", + "nickname": "5", + } + ]); + let actual = Data::json(actual).normalize(NormalizeMatches { + substitutions: &Default::default(), + pattern: &expected, + }); + if let (DataInner::Json(exp), DataInner::Json(act)) = (expected.inner, actual.inner) { + assert_eq!(exp, act); + } + } + + #[test] + #[cfg(feature = "json")] + fn json_normalize_wildcard_array_middle_last_early_return() { + let exp = json!([ + { + "name": "one", + "nickname": "1", + }, + "{...}", + { + "name": "three", + "nickname": "3", + }, + "{...}" + ]); + let expected = Data::json(exp); + let actual = json!([ + { + "name": "one", + "nickname": "1", + }, + { + "name": "two", + "nickname": "2", + }, + { + "name": "four", + "nickname": "4", + }, + { + "name": "five", + "nickname": "5", + } + ]); + let actual_normalized = Data::json(actual.clone()).normalize(NormalizeMatches { + substitutions: &Default::default(), + pattern: &expected, + }); + if let DataInner::Json(act) = actual_normalized.inner { + assert_eq!(act, actual); + } + } } diff --git a/vendor/snapbox/src/lib.rs b/vendor/snapbox/src/lib.rs index 61419fd5e..7084c15d2 100644 --- a/vendor/snapbox/src/lib.rs +++ b/vendor/snapbox/src/lib.rs @@ -128,7 +128,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>; /// ```rust /// let output = "something"; /// let expected = "something"; -/// snapbox::assert_matches(expected, output); +/// snapbox::assert_eq(expected, output); /// ``` #[track_caller] pub fn assert_eq(expected: impl Into<crate::Data>, actual: impl Into<crate::Data>) { diff --git a/vendor/snapbox/src/path.rs b/vendor/snapbox/src/path.rs index 9cd1e3f94..523b03822 100644 --- a/vendor/snapbox/src/path.rs +++ b/vendor/snapbox/src/path.rs @@ -40,8 +40,8 @@ impl PathFixture { #[cfg(feature = "path")] pub fn mutable_at(target: &std::path::Path) -> Result<Self, crate::Error> { - let _ = std::fs::remove_dir_all(&target); - std::fs::create_dir_all(&target) + 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()))) } @@ -577,7 +577,7 @@ fn copy_stats( dest: &std::path::Path, ) -> Result<(), std::io::Error> { let src_mtime = filetime::FileTime::from_last_modification_time(source_meta); - filetime::set_file_mtime(&dest, src_mtime)?; + filetime::set_file_mtime(dest, src_mtime)?; Ok(()) } diff --git a/vendor/snapbox/src/substitutions.rs b/vendor/snapbox/src/substitutions.rs index 9c228172b..d0057436c 100644 --- a/vendor/snapbox/src/substitutions.rs +++ b/vendor/snapbox/src/substitutions.rs @@ -184,7 +184,7 @@ fn normalize(input: &str, pattern: &str, substitutions: &Substitutions) -> Strin if let Some(future_pattern_index) = pattern_lines[next_pattern_index..] .iter() .enumerate() - .find(|(_, l)| **l == future_input_line || is_line_elide(**l)) + .find(|(_, l)| **l == future_input_line || is_line_elide(l)) .map(|(i, _)| next_pattern_index + i) { normalized.extend( |