Skip to content

Commit c3f19a8

Browse files
committed
refactor(test): Migrate validate_upload to snapbox for json comparisons
1 parent 01a47f3 commit c3f19a8

File tree

2 files changed

+3
-82
lines changed

2 files changed

+3
-82
lines changed

crates/cargo-test-support/src/compare.rs

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use crate::cross_compile::try_alternate;
4545
use crate::paths;
4646
use crate::{diff, rustc_host};
4747
use anyhow::{bail, Result};
48-
use serde_json::Value;
4948
use std::fmt;
5049
use std::path::Path;
5150
use std::str;
@@ -654,81 +653,6 @@ pub(crate) fn match_with_without(
654653
}
655654
}
656655

657-
/// Compares JSON object for approximate equality.
658-
/// You can use `[..]` wildcard in strings (useful for OS-dependent things such
659-
/// as paths). You can use a `"{...}"` string literal as a wildcard for
660-
/// arbitrary nested JSON (useful for parts of object emitted by other programs
661-
/// (e.g., rustc) rather than Cargo itself).
662-
pub(crate) fn find_json_mismatch(
663-
expected: &Value,
664-
actual: &Value,
665-
cwd: Option<&Path>,
666-
) -> Result<()> {
667-
match find_json_mismatch_r(expected, actual, cwd) {
668-
Some((expected_part, actual_part)) => bail!(
669-
"JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n",
670-
serde_json::to_string_pretty(expected).unwrap(),
671-
serde_json::to_string_pretty(&actual).unwrap(),
672-
serde_json::to_string_pretty(expected_part).unwrap(),
673-
serde_json::to_string_pretty(actual_part).unwrap(),
674-
),
675-
None => Ok(()),
676-
}
677-
}
678-
679-
fn find_json_mismatch_r<'a>(
680-
expected: &'a Value,
681-
actual: &'a Value,
682-
cwd: Option<&Path>,
683-
) -> Option<(&'a Value, &'a Value)> {
684-
use serde_json::Value::*;
685-
match (expected, actual) {
686-
(&Number(ref l), &Number(ref r)) if l == r => None,
687-
(&Bool(l), &Bool(r)) if l == r => None,
688-
(&String(ref l), _) if l == "{...}" => None,
689-
(&String(ref l), &String(ref r)) => {
690-
if match_exact(l, r, "", "", cwd).is_err() {
691-
Some((expected, actual))
692-
} else {
693-
None
694-
}
695-
}
696-
(&Array(ref l), &Array(ref r)) => {
697-
if l.len() != r.len() {
698-
return Some((expected, actual));
699-
}
700-
701-
l.iter()
702-
.zip(r.iter())
703-
.filter_map(|(l, r)| find_json_mismatch_r(l, r, cwd))
704-
.next()
705-
}
706-
(&Object(ref l), &Object(ref r)) => {
707-
let mut expected_entries = l.iter();
708-
let mut actual_entries = r.iter();
709-
710-
loop {
711-
match (expected_entries.next(), actual_entries.next()) {
712-
(None, None) => return None,
713-
(Some((expected_key, expected_value)), Some((actual_key, actual_value)))
714-
if expected_key == actual_key =>
715-
{
716-
if let mismatch @ Some(_) =
717-
find_json_mismatch_r(expected_value, actual_value, cwd)
718-
{
719-
return mismatch;
720-
}
721-
}
722-
_ => return Some((expected, actual)),
723-
}
724-
}
725-
}
726-
(&Null, &Null) => None,
727-
// Magic string literal `"{...}"` acts as wildcard for any sub-JSON.
728-
_ => Some((expected, actual)),
729-
}
730-
}
731-
732656
/// A single line string that supports `[..]` wildcard matching.
733657
pub(crate) struct WildStr<'a> {
734658
has_meta: bool,

crates/cargo-test-support/src/publish.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@
5757
//! );
5858
//! ```
5959
60-
use crate::compare::{assert_match_exact, find_json_mismatch};
60+
use crate::compare::assert_match_exact;
6161
use crate::registry::{self, alt_api_path, FeatureMap};
6262
use flate2::read::GzDecoder;
63+
use snapbox::prelude::*;
6364
use std::collections::{HashMap, HashSet};
6465
use std::fs;
6566
use std::fs::File;
@@ -133,12 +134,8 @@ fn _validate_upload(
133134
let json_sz = read_le_u32(&mut f).expect("read json length");
134135
let mut json_bytes = vec![0; json_sz as usize];
135136
f.read_exact(&mut json_bytes).expect("read JSON data");
136-
let actual_json = serde_json::from_slice(&json_bytes).expect("uploaded JSON should be valid");
137-
let expected_json = serde_json::from_str(expected_json).expect("expected JSON does not parse");
138137

139-
if let Err(e) = find_json_mismatch(&expected_json, &actual_json, None) {
140-
panic!("{}", e);
141-
}
138+
snapbox::assert_data_eq!(json_bytes, expected_json.is_json());
142139

143140
// 32-bit little-endian integer of length of crate file.
144141
let crate_sz = read_le_u32(&mut f).expect("read crate length");

0 commit comments

Comments
 (0)