|
44 | 44 | use crate::cross_compile::try_alternate; |
45 | 45 | use crate::paths; |
46 | 46 | use crate::{diff, rustc_host}; |
47 | | -use anyhow::{bail, Context, Result}; |
48 | | -use serde_json::Value; |
| 47 | +use anyhow::{bail, Result}; |
49 | 48 | use std::fmt; |
50 | 49 | use std::path::Path; |
51 | 50 | use std::str; |
@@ -654,159 +653,6 @@ pub(crate) fn match_with_without( |
654 | 653 | } |
655 | 654 | } |
656 | 655 |
|
657 | | -/// Checks that the given string of JSON objects match the given set of |
658 | | -/// expected JSON objects. |
659 | | -/// |
660 | | -/// See [`crate::Execs::with_json`] for more details. |
661 | | -pub(crate) fn match_json(expected: &str, actual: &str, cwd: Option<&Path>) -> Result<()> { |
662 | | - let (exp_objs, act_objs) = collect_json_objects(expected, actual)?; |
663 | | - if exp_objs.len() != act_objs.len() { |
664 | | - bail!( |
665 | | - "expected {} json lines, got {}, stdout:\n{}", |
666 | | - exp_objs.len(), |
667 | | - act_objs.len(), |
668 | | - actual |
669 | | - ); |
670 | | - } |
671 | | - for (exp_obj, act_obj) in exp_objs.iter().zip(act_objs) { |
672 | | - find_json_mismatch(exp_obj, &act_obj, cwd)?; |
673 | | - } |
674 | | - Ok(()) |
675 | | -} |
676 | | - |
677 | | -/// Checks that the given string of JSON objects match the given set of |
678 | | -/// expected JSON objects, ignoring their order. |
679 | | -/// |
680 | | -/// See [`crate::Execs::with_json_contains_unordered`] for more details and |
681 | | -/// cautions when using. |
682 | | -pub(crate) fn match_json_contains_unordered( |
683 | | - expected: &str, |
684 | | - actual: &str, |
685 | | - cwd: Option<&Path>, |
686 | | -) -> Result<()> { |
687 | | - let (exp_objs, mut act_objs) = collect_json_objects(expected, actual)?; |
688 | | - for exp_obj in exp_objs { |
689 | | - match act_objs |
690 | | - .iter() |
691 | | - .position(|act_obj| find_json_mismatch(&exp_obj, act_obj, cwd).is_ok()) |
692 | | - { |
693 | | - Some(index) => act_objs.remove(index), |
694 | | - None => { |
695 | | - bail!( |
696 | | - "Did not find expected JSON:\n\ |
697 | | - {}\n\ |
698 | | - Remaining available output:\n\ |
699 | | - {}\n", |
700 | | - serde_json::to_string_pretty(&exp_obj).unwrap(), |
701 | | - itertools::join( |
702 | | - act_objs.iter().map(|o| serde_json::to_string(o).unwrap()), |
703 | | - "\n" |
704 | | - ) |
705 | | - ); |
706 | | - } |
707 | | - }; |
708 | | - } |
709 | | - Ok(()) |
710 | | -} |
711 | | - |
712 | | -fn collect_json_objects( |
713 | | - expected: &str, |
714 | | - actual: &str, |
715 | | -) -> Result<(Vec<serde_json::Value>, Vec<serde_json::Value>)> { |
716 | | - let expected_objs: Vec<_> = expected |
717 | | - .split("\n\n") |
718 | | - .map(|expect| { |
719 | | - expect |
720 | | - .parse() |
721 | | - .with_context(|| format!("failed to parse expected JSON object:\n{}", expect)) |
722 | | - }) |
723 | | - .collect::<Result<_>>()?; |
724 | | - let actual_objs: Vec<_> = actual |
725 | | - .lines() |
726 | | - .filter(|line| line.starts_with('{')) |
727 | | - .map(|line| { |
728 | | - line.parse() |
729 | | - .with_context(|| format!("failed to parse JSON object:\n{}", line)) |
730 | | - }) |
731 | | - .collect::<Result<_>>()?; |
732 | | - Ok((expected_objs, actual_objs)) |
733 | | -} |
734 | | - |
735 | | -/// Compares JSON object for approximate equality. |
736 | | -/// You can use `[..]` wildcard in strings (useful for OS-dependent things such |
737 | | -/// as paths). You can use a `"{...}"` string literal as a wildcard for |
738 | | -/// arbitrary nested JSON (useful for parts of object emitted by other programs |
739 | | -/// (e.g., rustc) rather than Cargo itself). |
740 | | -pub(crate) fn find_json_mismatch( |
741 | | - expected: &Value, |
742 | | - actual: &Value, |
743 | | - cwd: Option<&Path>, |
744 | | -) -> Result<()> { |
745 | | - match find_json_mismatch_r(expected, actual, cwd) { |
746 | | - Some((expected_part, actual_part)) => bail!( |
747 | | - "JSON mismatch\nExpected:\n{}\nWas:\n{}\nExpected part:\n{}\nActual part:\n{}\n", |
748 | | - serde_json::to_string_pretty(expected).unwrap(), |
749 | | - serde_json::to_string_pretty(&actual).unwrap(), |
750 | | - serde_json::to_string_pretty(expected_part).unwrap(), |
751 | | - serde_json::to_string_pretty(actual_part).unwrap(), |
752 | | - ), |
753 | | - None => Ok(()), |
754 | | - } |
755 | | -} |
756 | | - |
757 | | -fn find_json_mismatch_r<'a>( |
758 | | - expected: &'a Value, |
759 | | - actual: &'a Value, |
760 | | - cwd: Option<&Path>, |
761 | | -) -> Option<(&'a Value, &'a Value)> { |
762 | | - use serde_json::Value::*; |
763 | | - match (expected, actual) { |
764 | | - (&Number(ref l), &Number(ref r)) if l == r => None, |
765 | | - (&Bool(l), &Bool(r)) if l == r => None, |
766 | | - (&String(ref l), _) if l == "{...}" => None, |
767 | | - (&String(ref l), &String(ref r)) => { |
768 | | - if match_exact(l, r, "", "", cwd).is_err() { |
769 | | - Some((expected, actual)) |
770 | | - } else { |
771 | | - None |
772 | | - } |
773 | | - } |
774 | | - (&Array(ref l), &Array(ref r)) => { |
775 | | - if l.len() != r.len() { |
776 | | - return Some((expected, actual)); |
777 | | - } |
778 | | - |
779 | | - l.iter() |
780 | | - .zip(r.iter()) |
781 | | - .filter_map(|(l, r)| find_json_mismatch_r(l, r, cwd)) |
782 | | - .next() |
783 | | - } |
784 | | - (&Object(ref l), &Object(ref r)) => { |
785 | | - let mut expected_entries = l.iter(); |
786 | | - let mut actual_entries = r.iter(); |
787 | | - |
788 | | - loop { |
789 | | - match (expected_entries.next(), actual_entries.next()) { |
790 | | - (None, None) => return None, |
791 | | - (Some((expected_key, expected_value)), Some((actual_key, actual_value))) |
792 | | - if expected_key == actual_key => |
793 | | - { |
794 | | - if let mismatch @ Some(_) = |
795 | | - find_json_mismatch_r(expected_value, actual_value, cwd) |
796 | | - { |
797 | | - return mismatch; |
798 | | - } |
799 | | - } |
800 | | - _ => return Some((expected, actual)), |
801 | | - } |
802 | | - } |
803 | | - } |
804 | | - (&Null, &Null) => None, |
805 | | - // Magic string literal `"{...}"` acts as wildcard for any sub-JSON. |
806 | | - _ => Some((expected, actual)), |
807 | | - } |
808 | | -} |
809 | | - |
810 | 656 | /// A single line string that supports `[..]` wildcard matching. |
811 | 657 | pub(crate) struct WildStr<'a> { |
812 | 658 | has_meta: bool, |
|
0 commit comments