From 968a976c3977b9e78e22b1cf32af6e770d69f699 Mon Sep 17 00:00:00 2001 From: ndkazu Date: Fri, 29 Nov 2024 01:19:27 +0900 Subject: [PATCH] Code refactoring based on latest reviews --- .../tests/chain_spec_builder_tests.rs | 221 +++++++++--------- 1 file changed, 115 insertions(+), 106 deletions(-) diff --git a/docs/sdk/src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs b/docs/sdk/src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs index fefbd63cdf8a..94f1489a66c2 100644 --- a/docs/sdk/src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs +++ b/docs/sdk/src/reference_docs/chain_spec_runtime/tests/chain_spec_builder_tests.rs @@ -10,37 +10,15 @@ fn wasm_file_path() -> &'static str { const CHAIN_SPEC_BUILDER_PATH: &str = "../../../../../target/release/chain-spec-builder"; macro_rules! bash( - ( chain-spec-builder $x:tt, $($a:tt)* ) => {{ + ( chain-spec-builder $($a:tt)* ) => {{ let path = get_chain_spec_builder_path(); - let x: serde_json::Value = json!($x); - let output = spawn_with_output!( + spawn_with_output!( $path $($a)* ) .expect("a process running. qed") .wait_with_output() - .expect("to get output. qed."); - let output0: serde_json::Value = serde_json::from_slice(&output.as_bytes()).unwrap(); - assert_eq!(output0, x, "Output did not match expected"); - }} -); + .expect("to get output. qed.") -macro_rules! bash2( - ( chain-spec-builder $x:tt, $($a:tt)* ) => {{ - let path = get_chain_spec_builder_path(); - let x: serde_json::Value = json!($x); - let output = spawn_with_output!( - $path $($a)* - ) - .expect("a process running. qed") - .wait_with_output() - .expect("to get output. qed."); - let mut output0: serde_json::Value = serde_json::from_slice(&output.as_bytes()).unwrap(); - //remove code field for better readability - if let Some(code) = output0["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code") - { - *code = Value::String("0x123".to_string()); - } - assert_eq!(output0, x, "Output did not match expected"); }} ); @@ -52,10 +30,20 @@ fn get_chain_spec_builder_path() -> &'static str { CHAIN_SPEC_BUILDER_PATH } -#[docify::export] -fn cmd_list_presets(path: &str) { +#[docify::export_content] +fn cmd_list_presets(path: &str) -> String { bash!( - chain-spec-builder { + chain-spec-builder list-presets -r $path + ) +} + +#[test] +fn list_presets() { + let output: serde_json::Value = + serde_json::from_slice(cmd_list_presets(wasm_file_path()).as_bytes()).unwrap(); + assert_eq!( + output, + json!({ "presets":[ "preset_1", "preset_2", @@ -63,19 +51,25 @@ fn cmd_list_presets(path: &str) { "preset_4", "preset_invalid" ] - }, list-presets -r $path + }), + "Output did not match expected" ); } -#[test] -fn list_presets() { - cmd_list_presets(wasm_file_path()); -} - #[docify::export] -fn cmd_get_preset(path: &str) { +fn cmd_get_preset(path: &str) -> String { bash!( - chain-spec-builder { + chain-spec-builder display-preset -r $path -p preset_2 + ) +} + +#[test] +fn get_preset() { + let output: serde_json::Value = + serde_json::from_slice(cmd_get_preset(wasm_file_path()).as_bytes()).unwrap(); + assert_eq!( + output, + json!({ "bar": { "initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL", }, @@ -87,93 +81,108 @@ fn cmd_get_preset(path: &str) { }, "someInteger": 200 }, - }, display-preset -r $path -p preset_2 + }), + "Output did not match expected" ); } -#[test] -fn get_preset() { - cmd_get_preset(wasm_file_path()); +#[docify::export] +fn cmd_generate_chain_spec(path: &str) -> String { + bash!( + chain-spec-builder -c /dev/stdout create -r $path named-preset preset_2 + ) } -#[docify::export] -fn cmd_generate_chain_spec(path: &str) { - bash2!( - chain-spec-builder { - "name": "Custom", - "id": "custom", - "chainType": "Live", - "bootNodes": [], - "telemetryEndpoints": null, - "protocolId": null, - "properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" }, - "codeSubstitutes": {}, - "genesis": { - "runtimeGenesis": { - "code": "0x123", - "patch": { - "bar": { - "initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL" +#[test] +fn generate_chain_spec() { + let mut output: serde_json::Value = + serde_json::from_slice(cmd_generate_chain_spec(wasm_file_path()).as_bytes()).unwrap(); + if let Some(code) = output["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code") + { + *code = Value::String("0x123".to_string()); + } + assert_eq!( + output, + json!({ + "name": "Custom", + "id": "custom", + "chainType": "Live", + "bootNodes": [], + "telemetryEndpoints": null, + "protocolId": null, + "properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" }, + "codeSubstitutes": {}, + "genesis": { + "runtimeGenesis": { + "code": "0x123", + "patch": { + "bar": { + "initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL" + }, + "foo": { + "someEnum": { + "Data2": { + "values": "0x0c10" + } }, - "foo": { - "someEnum": { - "Data2": { - "values": "0x0c10" - } - }, - "someInteger": 200 - } + "someInteger": 200 } } } - }, -c /dev/stdout create -r $path named-preset preset_2 + } + }), + "Output did not match expected" ); } -#[test] -fn generate_chain_spec() { - cmd_generate_chain_spec(wasm_file_path()); -} - #[docify::export] -fn cmd_generate_para_chain_spec(path: &str) { - bash2!( - chain-spec-builder { - "name": "Custom", - "id": "custom", - "chainType": "Live", - "bootNodes": [], - "telemetryEndpoints": null, - "protocolId": null, - "relay_chain": "polkadot", - "para_id": 1000, - "properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" }, - "codeSubstitutes": {}, - "genesis": { - "runtimeGenesis": { - "code": "0x123", - "patch": { - "bar": { - "initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL" - }, - "foo": { - "someEnum": { - "Data2": { - "values": "0x0c10" - } - }, - "someInteger": 200 - } - } - } - }} - , -c /dev/stdout create -c polkadot -p 1000 -r $path named-preset preset_2 - ); +fn cmd_generate_para_chain_spec(path: &str) -> String { + bash!( + chain-spec-builder -c /dev/stdout create -c polkadot -p 1000 -r $path named-preset preset_2 + ) } #[test] fn generate_para_chain_spec() { - cmd_generate_para_chain_spec(wasm_file_path()); + let mut output: serde_json::Value = + serde_json::from_slice(cmd_generate_para_chain_spec(wasm_file_path()).as_bytes()).unwrap(); + if let Some(code) = output["genesis"]["runtimeGenesis"].as_object_mut().unwrap().get_mut("code") + { + *code = Value::String("0x123".to_string()); + } + assert_eq!( + output, + json!({ + "name": "Custom", + "id": "custom", + "chainType": "Live", + "bootNodes": [], + "telemetryEndpoints": null, + "protocolId": null, + "relay_chain": "polkadot", + "para_id": 1000, + "properties": { "tokenDecimals": 12, "tokenSymbol": "UNIT" }, + "codeSubstitutes": {}, + "genesis": { + "runtimeGenesis": { + "code": "0x123", + "patch": { + "bar": { + "initialAccount": "5CiPPseXPECbkjWCa6MnjNokrgYjMqmKndv2rSnekmSK2DjL" + }, + "foo": { + "someEnum": { + "Data2": { + "values": "0x0c10" + } + }, + "someInteger": 200 + } + } + } + }}), + "Output did not match expected" + ); } #[test]