From 2ceda2fdc8fe449a8ec99775baa5e84da6f11fba Mon Sep 17 00:00:00 2001 From: Ed Page Date: Sat, 21 Sep 2024 20:06:13 -0500 Subject: [PATCH] fix(test): Remove unused, deprecated with_json --- crates/cargo-test-support/src/compare.rs | 80 +----------------------- crates/cargo-test-support/src/lib.rs | 64 ------------------- 2 files changed, 1 insertion(+), 143 deletions(-) diff --git a/crates/cargo-test-support/src/compare.rs b/crates/cargo-test-support/src/compare.rs index e822bb3822c7..0b2ca2a21653 100644 --- a/crates/cargo-test-support/src/compare.rs +++ b/crates/cargo-test-support/src/compare.rs @@ -44,7 +44,7 @@ use crate::cross_compile::try_alternate; use crate::paths; use crate::{diff, rustc_host}; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use serde_json::Value; use std::fmt; use std::path::Path; @@ -654,84 +654,6 @@ pub(crate) fn match_with_without( } } -/// Checks that the given string of JSON objects match the given set of -/// expected JSON objects. -/// -/// See [`crate::Execs::with_json`] for more details. -pub(crate) fn match_json(expected: &str, actual: &str, cwd: Option<&Path>) -> Result<()> { - let (exp_objs, act_objs) = collect_json_objects(expected, actual)?; - if exp_objs.len() != act_objs.len() { - bail!( - "expected {} json lines, got {}, stdout:\n{}", - exp_objs.len(), - act_objs.len(), - actual - ); - } - for (exp_obj, act_obj) in exp_objs.iter().zip(act_objs) { - find_json_mismatch(exp_obj, &act_obj, cwd)?; - } - Ok(()) -} - -/// Checks that the given string of JSON objects match the given set of -/// expected JSON objects, ignoring their order. -/// -/// See [`crate::Execs::with_json_contains_unordered`] for more details and -/// cautions when using. -pub(crate) fn match_json_contains_unordered( - expected: &str, - actual: &str, - cwd: Option<&Path>, -) -> Result<()> { - let (exp_objs, mut act_objs) = collect_json_objects(expected, actual)?; - for exp_obj in exp_objs { - match act_objs - .iter() - .position(|act_obj| find_json_mismatch(&exp_obj, act_obj, cwd).is_ok()) - { - Some(index) => act_objs.remove(index), - None => { - bail!( - "Did not find expected JSON:\n\ - {}\n\ - Remaining available output:\n\ - {}\n", - serde_json::to_string_pretty(&exp_obj).unwrap(), - itertools::join( - act_objs.iter().map(|o| serde_json::to_string(o).unwrap()), - "\n" - ) - ); - } - }; - } - Ok(()) -} - -fn collect_json_objects( - expected: &str, - actual: &str, -) -> Result<(Vec, Vec)> { - let expected_objs: Vec<_> = expected - .split("\n\n") - .map(|expect| { - expect - .parse() - .with_context(|| format!("failed to parse expected JSON object:\n{}", expect)) - }) - .collect::>()?; - let actual_objs: Vec<_> = actual - .lines() - .filter(|line| line.starts_with('{')) - .map(|line| { - line.parse() - .with_context(|| format!("failed to parse JSON object:\n{}", line)) - }) - .collect::>()?; - Ok((expected_objs, actual_objs)) -} - /// Compares JSON object for approximate equality. /// You can use `[..]` wildcard in strings (useful for OS-dependent things such /// as paths). You can use a `"{...}"` string literal as a wildcard for diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index 4c7829974f38..950f9818f4c6 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -658,8 +658,6 @@ pub struct Execs { expect_stdout_unordered: Vec, expect_stderr_unordered: Vec, expect_stderr_with_without: Vec<(Vec, Vec)>, - expect_json: Option, - expect_json_contains_unordered: Option, stream_output: bool, assert: snapbox::Assert, } @@ -843,56 +841,6 @@ impl Execs { self.expect_stderr_with_without.push((with, without)); self } - - /// Verifies the JSON output matches the given JSON. - /// - /// This is typically used when testing cargo commands that emit JSON. - /// Each separate JSON object should be separated by a blank line. - /// Example: - /// - /// ```rust,ignore - /// assert_that( - /// p.cargo("metadata"), - /// execs().with_json(r#" - /// {"example": "abc"} - /// - /// {"example": "def"} - /// "#) - /// ); - /// ``` - /// - /// - Objects should match in the order given. - /// - The order of arrays is ignored. - /// - Strings support patterns described in [`compare`]. - /// - Use `"{...}"` to match any object. - #[deprecated( - note = "replaced with `Execs::with_stdout_data(expected.is_json().against_jsonlines())`" - )] - pub fn with_json(&mut self, expected: &str) -> &mut Self { - self.expect_json = Some(expected.to_string()); - self - } - - /// Verifies JSON output contains the given objects (in any order) somewhere - /// in its output. - /// - /// CAUTION: Be very careful when using this. Make sure every object is - /// unique (not a subset of one another). Also avoid using objects that - /// could possibly match multiple output lines unless you're very sure of - /// what you are doing. - /// - /// See `with_json` for more detail. - #[deprecated] - pub fn with_json_contains_unordered(&mut self, expected: &str) -> &mut Self { - match &mut self.expect_json_contains_unordered { - None => self.expect_json_contains_unordered = Some(expected.to_string()), - Some(e) => { - e.push_str("\n\n"); - e.push_str(expected); - } - } - self - } } /// # Configure the process @@ -1071,8 +1019,6 @@ impl Execs { && self.expect_stdout_unordered.is_empty() && self.expect_stderr_unordered.is_empty() && self.expect_stderr_with_without.is_empty() - && self.expect_json.is_none() - && self.expect_json_contains_unordered.is_none() { panic!( "`with_status()` is used, but no output is checked.\n\ @@ -1202,14 +1148,6 @@ impl Execs { for (with, without) in self.expect_stderr_with_without.iter() { compare::match_with_without(stderr, with, without, cwd)?; } - - if let Some(ref expect_json) = self.expect_json { - compare::match_json(expect_json, stdout, cwd)?; - } - - if let Some(ref expected) = self.expect_json_contains_unordered { - compare::match_json_contains_unordered(expected, stdout, cwd)?; - } Ok(()) } } @@ -1241,8 +1179,6 @@ pub fn execs() -> Execs { expect_stdout_unordered: Vec::new(), expect_stderr_unordered: Vec::new(), expect_stderr_with_without: Vec::new(), - expect_json: None, - expect_json_contains_unordered: None, stream_output: false, assert: compare::assert_e2e(), }