mirror of
https://github.com/rust-lang/cargo.git
synced 2025-10-01 11:30:39 +00:00
fix(test): Remove unused, deprecated with_json
This commit is contained in:
parent
c18765a152
commit
01a47f3c4d
@ -44,7 +44,7 @@
|
|||||||
use crate::cross_compile::try_alternate;
|
use crate::cross_compile::try_alternate;
|
||||||
use crate::paths;
|
use crate::paths;
|
||||||
use crate::{diff, rustc_host};
|
use crate::{diff, rustc_host};
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Result};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::path::Path;
|
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<serde_json::Value>, Vec<serde_json::Value>)> {
|
|
||||||
let expected_objs: Vec<_> = expected
|
|
||||||
.split("\n\n")
|
|
||||||
.map(|expect| {
|
|
||||||
expect
|
|
||||||
.parse()
|
|
||||||
.with_context(|| format!("failed to parse expected JSON object:\n{}", expect))
|
|
||||||
})
|
|
||||||
.collect::<Result<_>>()?;
|
|
||||||
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::<Result<_>>()?;
|
|
||||||
Ok((expected_objs, actual_objs))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compares JSON object for approximate equality.
|
/// Compares JSON object for approximate equality.
|
||||||
/// You can use `[..]` wildcard in strings (useful for OS-dependent things such
|
/// You can use `[..]` wildcard in strings (useful for OS-dependent things such
|
||||||
/// as paths). You can use a `"{...}"` string literal as a wildcard for
|
/// as paths). You can use a `"{...}"` string literal as a wildcard for
|
||||||
|
@ -655,8 +655,6 @@ pub struct Execs {
|
|||||||
expect_stdout_unordered: Vec<String>,
|
expect_stdout_unordered: Vec<String>,
|
||||||
expect_stderr_unordered: Vec<String>,
|
expect_stderr_unordered: Vec<String>,
|
||||||
expect_stderr_with_without: Vec<(Vec<String>, Vec<String>)>,
|
expect_stderr_with_without: Vec<(Vec<String>, Vec<String>)>,
|
||||||
expect_json: Option<String>,
|
|
||||||
expect_json_contains_unordered: Option<String>,
|
|
||||||
stream_output: bool,
|
stream_output: bool,
|
||||||
assert: snapbox::Assert,
|
assert: snapbox::Assert,
|
||||||
}
|
}
|
||||||
@ -824,56 +822,6 @@ impl Execs {
|
|||||||
self.expect_stderr_with_without.push((with, without));
|
self.expect_stderr_with_without.push((with, without));
|
||||||
self
|
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
|
/// # Configure the process
|
||||||
@ -1050,8 +998,6 @@ impl Execs {
|
|||||||
&& self.expect_stdout_unordered.is_empty()
|
&& self.expect_stdout_unordered.is_empty()
|
||||||
&& self.expect_stderr_unordered.is_empty()
|
&& self.expect_stderr_unordered.is_empty()
|
||||||
&& self.expect_stderr_with_without.is_empty()
|
&& self.expect_stderr_with_without.is_empty()
|
||||||
&& self.expect_json.is_none()
|
|
||||||
&& self.expect_json_contains_unordered.is_none()
|
|
||||||
{
|
{
|
||||||
panic!(
|
panic!(
|
||||||
"`with_status()` is used, but no output is checked.\n\
|
"`with_status()` is used, but no output is checked.\n\
|
||||||
@ -1175,14 +1121,6 @@ impl Execs {
|
|||||||
for (with, without) in self.expect_stderr_with_without.iter() {
|
for (with, without) in self.expect_stderr_with_without.iter() {
|
||||||
compare::match_with_without(stderr, with, without, cwd)?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1212,8 +1150,6 @@ pub fn execs() -> Execs {
|
|||||||
expect_stdout_unordered: Vec::new(),
|
expect_stdout_unordered: Vec::new(),
|
||||||
expect_stderr_unordered: Vec::new(),
|
expect_stderr_unordered: Vec::new(),
|
||||||
expect_stderr_with_without: Vec::new(),
|
expect_stderr_with_without: Vec::new(),
|
||||||
expect_json: None,
|
|
||||||
expect_json_contains_unordered: None,
|
|
||||||
stream_output: false,
|
stream_output: false,
|
||||||
assert: compare::assert_e2e(),
|
assert: compare::assert_e2e(),
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user