From 59654816b606116031a2014aee49dfaac7127922 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 25 Dec 2024 16:19:09 +0200 Subject: [PATCH] chore(starknet_sequencer_node): fix the conditional print on config tests --- Cargo.lock | 2 +- crates/papyrus_config/Cargo.toml | 1 + crates/papyrus_config/src/lib.rs | 1 + crates/papyrus_config/src/test_utils.rs | 35 +++++++++++++++++++ crates/starknet_sequencer_node/Cargo.toml | 1 - .../src/config/config_test.rs | 13 +++---- 6 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 crates/papyrus_config/src/test_utils.rs diff --git a/Cargo.lock b/Cargo.lock index 46269280ef..0c523e3d0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7345,6 +7345,7 @@ dependencies = [ name = "papyrus_config" version = "0.0.0" dependencies = [ + "assert-json-diff", "assert_matches", "clap", "infra_utils", @@ -10723,7 +10724,6 @@ name = "starknet_sequencer_node" version = "0.0.0" dependencies = [ "anyhow", - "assert-json-diff", "assert_matches", "clap", "colored", diff --git a/crates/papyrus_config/Cargo.toml b/crates/papyrus_config/Cargo.toml index 408b95fb06..fcf23279cc 100644 --- a/crates/papyrus_config/Cargo.toml +++ b/crates/papyrus_config/Cargo.toml @@ -7,6 +7,7 @@ license-file.workspace = true description = "A library for handling node configuration." [dependencies] +assert-json-diff.workspace = true clap = { workspace = true, features = ["env", "string"] } infra_utils.workspace = true itertools.workspace = true diff --git a/crates/papyrus_config/src/lib.rs b/crates/papyrus_config/src/lib.rs index 7a383cef54..f1ffb2fbbd 100644 --- a/crates/papyrus_config/src/lib.rs +++ b/crates/papyrus_config/src/lib.rs @@ -70,6 +70,7 @@ pub mod converters; pub mod dumping; pub mod loading; pub mod presentation; +pub mod test_utils; pub mod validators; /// The privacy level of a config parameter, that received as input from the configs. diff --git a/crates/papyrus_config/src/test_utils.rs b/crates/papyrus_config/src/test_utils.rs new file mode 100644 index 0000000000..98cb379ef4 --- /dev/null +++ b/crates/papyrus_config/src/test_utils.rs @@ -0,0 +1,35 @@ +//! Utils for config test. + +use assert_json_diff::{assert_json_matches_no_panic, CompareMode, Config}; +use serde::Serialize; + +/// Compares two JSON values for an exact match without panicking. +/// See [`assert_json_matches_no_panic`] +pub fn assert_json_exact_matches_no_panic(lhs: &Lhs, rhs: &Rhs) -> Result<(), String> +where + Lhs: Serialize, + Rhs: Serialize, +{ + assert_json_matches_no_panic(lhs, rhs, Config::new(CompareMode::Strict)) +} + +#[macro_export] +/// Compare two JSON values for an exact match. +/// +/// Extends the functionality of [`assert_json_diff::assert_json_eq`] by also adding a customizable +/// error message print. +macro_rules! assert_json_eq { + ($lhs:expr, $rhs:expr, $error_message:expr $(,)?) => {{ + if let Err(error) = $crate::test_utils::assert_json_exact_matches_no_panic(&$lhs, &$rhs) { + let printed_error = format!("\n\n{}\n{}\n\n", $error_message, error); + panic!("{}", printed_error); + } + }}; + + ($lhs:expr, $rhs:expr $(,)?) => {{ + if let Err(error) = $crate::test_utils::assert_json_exact_matches_no_panic(&$lhs, &$rhs) { + let printed_error = format!("\n\n{}\n\n", error); + panic!("{}", printed_error); + } + }}; +} diff --git a/crates/starknet_sequencer_node/Cargo.toml b/crates/starknet_sequencer_node/Cargo.toml index 60a88cba71..4b903f6ea0 100644 --- a/crates/starknet_sequencer_node/Cargo.toml +++ b/crates/starknet_sequencer_node/Cargo.toml @@ -45,7 +45,6 @@ tracing.workspace = true validator.workspace = true [dev-dependencies] -assert-json-diff.workspace = true assert_matches.workspace = true colored.workspace = true infra_utils.workspace = true diff --git a/crates/starknet_sequencer_node/src/config/config_test.rs b/crates/starknet_sequencer_node/src/config/config_test.rs index 3d636e950a..7234ce1a49 100644 --- a/crates/starknet_sequencer_node/src/config/config_test.rs +++ b/crates/starknet_sequencer_node/src/config/config_test.rs @@ -2,13 +2,12 @@ use std::collections::HashSet; use std::env; use std::fs::File; -use assert_json_diff::assert_json_eq; use assert_matches::assert_matches; use colored::Colorize; use infra_utils::path::resolve_project_relative_path; use papyrus_config::dumping::SerializeConfig; use papyrus_config::validators::config_validate; -use papyrus_config::SerializedParam; +use papyrus_config::{assert_json_eq, SerializedParam}; use rstest::rstest; use starknet_batcher::block_builder::BlockBuilderConfig; use starknet_batcher::config::BatcherConfig; @@ -96,17 +95,15 @@ fn test_default_config_file_is_up_to_date() { let from_code: serde_json::Value = serde_json::from_reader(File::open(tmp_file_path).unwrap()).unwrap(); - println!( - "{}", + let error_message = format!( + "{}\n{}", "Default config file doesn't match the default NodeConfig implementation. Please update \ it using the sequencer_dump_config binary." .purple() - .bold() - ); - println!( + .bold(), "Diffs shown below (default config file <<>> dump of SequencerNodeConfig::default())." ); - assert_json_eq!(from_default_config_file, from_code) + assert_json_eq!(from_default_config_file, from_code, error_message); } /// Tests parsing a node config without additional args.