From 97ef8323f246ef1ff0fc572cb980bd07d150408c Mon Sep 17 00:00:00 2001 From: Marcin Nowak-Liebiediew Date: Mon, 30 Oct 2023 17:12:11 +0100 Subject: [PATCH] extend unit tests --- .../manifest/custom_canister_type.rs | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/src/dfx-core/src/extension/manifest/custom_canister_type.rs b/src/dfx-core/src/extension/manifest/custom_canister_type.rs index 8501dd9aee..bea1140315 100644 --- a/src/dfx-core/src/extension/manifest/custom_canister_type.rs +++ b/src/dfx-core/src/extension/manifest/custom_canister_type.rs @@ -184,19 +184,21 @@ impl CustomCanisterTypeDeclaration { _ => {} } } + // Removing fields should be done last because of the order of the fields in the map. // It's easier to do in second for loop than to sort Ops beforehand, bacause Op would need to implement PartialOrd, // which is not possible, because serde_json::Number does not implement it. for field_name in remove_fields { final_fields.remove(&field_name); } + // Override custom canister declaration values by the real canister_declaration + // see: https://github.com/dfinity/sdk/pull/3222#issuecomment-1624073606 for (key, value) in values.iter() { if key != "type" && key != "canister_name" { final_fields.insert(key.clone(), value.clone()); } } - Ok(final_fields) } } @@ -233,28 +235,64 @@ mod tests { };} #[test] - fn test_op_replace() {} + fn test_op_replace_basic() { + test_op!( + custom_canister_template = r#" + { + "main": { "replace": { "input": "{{canister_name}}", "search": "frontend_(.*)", "output": "oowee/$1/main.ts" } } + } + "#, + dfx_json_canister_values = r#" + { + "canister_name": "frontend_xyz" + } + "#, + expected = r#" + { + "main": "oowee/xyz/main.ts" + } + "# + ); + } + + #[test] + fn test_op_replace_nested() { + test_op!( + custom_canister_template = r#" + { + "main": { "replace": { "input": "{{main}}", "search": "(.*)", "output": "oowee/$1" } } + } + "#, + dfx_json_canister_values = r#" + { + "main": "src/main.ts" + } + "#, + expected = r#" + { + "main": "oowee/src/main.ts" + } + "# + ); + } #[test] fn test_op_remove() { test_op!( custom_canister_template = r#" { - "type": "{{canister_name}}", "main": "src/main.ts", "main": { "remove": true } } "#, dfx_json_canister_values = r#" { - "canister_name": "something", "main": "oowee.exe" } "#, expected = r#" { - "main": "oowee.exe", - "type": "something" + "main": "oowee.exe" } "# ); @@ -265,19 +303,16 @@ mod tests { test_op!( custom_canister_template = r#" { - "type": "{{canister_name}}", - "main": "src/main.ts" + "type": "{{canister_name}}" } "#, dfx_json_canister_values = r#" { - "type": "custom", "canister_name": "something" } "#, expected = r#" { - "main": "src/main.ts", "type": "something" } "# @@ -328,6 +363,27 @@ mod tests { ); } + #[test] + fn test_overwrite() { + test_op!( + custom_canister_template = r#" + { + "gzip": true + } + "#, + dfx_json_canister_values = r#" + { + "gzip": false + } + "#, + expected = r#" + { + "gzip": false + } + "# + ); + } + #[test] fn test_ops() { test_op!(