From 80752a9158b3fa4996bb8156a50fdedcfa1771b3 Mon Sep 17 00:00:00 2001 From: davidsemakula Date: Tue, 30 Jan 2024 05:44:13 +0300 Subject: [PATCH 1/2] Improve syntax for ink! e2e `runtime_only` attribute argument Use `#[darling(word)]` to enable writing `#[ink_e2e::test(backend(runtime_only))]` instead of `#[ink_e2e::test(backend(runtime_only()))]` --- Cargo.lock | 22 ++++++------- Cargo.toml | 2 +- crates/e2e/macro/src/codegen.rs | 11 ++----- crates/e2e/macro/src/config.rs | 32 +++++++++++++++---- .../e2e-runtime-only-backend/lib.rs | 4 +-- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 41fca28ba03..8af54ae752a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1301,12 +1301,12 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.3" +version = "0.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" +checksum = "da01daa5f6d41c91358398e8db4dde38e292378da1f28300b59ef4732b879454" dependencies = [ - "darling_core 0.20.3", - "darling_macro 0.20.3", + "darling_core 0.20.4", + "darling_macro 0.20.4", ] [[package]] @@ -1325,9 +1325,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.3" +version = "0.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" +checksum = "f44f6238b948a3c6c3073cdf53bb0c2d5e024ee27e0f35bfe9d556a12395808a" dependencies = [ "fnv", "ident_case", @@ -1350,11 +1350,11 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.3" +version = "0.20.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" +checksum = "0d2d88bd93979b1feb760a6b5c531ac5ba06bd63e74894c377af02faee07b9cd" dependencies = [ - "darling_core 0.20.3", + "darling_core 0.20.4", "quote", "syn 2.0.46", ] @@ -2642,7 +2642,7 @@ dependencies = [ name = "ink_e2e_macro" version = "5.0.0-rc" dependencies = [ - "darling 0.20.3", + "darling 0.20.4", "derive_more", "ink_ir", "proc-macro2", @@ -5858,7 +5858,7 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d5086ce2a90e723083ff19b77f06805d00e732eac3e19c86f6cd643d4255d334" dependencies = [ - "darling 0.20.3", + "darling 0.20.4", "parity-scale-codec", "proc-macro-error", "subxt-codegen", diff --git a/Cargo.toml b/Cargo.toml index 57aa776ec8f..581495d8406 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,7 +38,7 @@ blake2 = { version = "0.10" } cargo_metadata = { version = "0.18.0" } cfg-if = { version = "1.0" } contract-build = { version = "4.0.0-rc.1" } -darling = { version = "0.20.3" } +darling = { version = "0.20.4" } derive_more = { version = "0.99.17", default-features = false } drink = { version = "=0.8.5" } either = { version = "1.5", default-features = false } diff --git a/crates/e2e/macro/src/codegen.rs b/crates/e2e/macro/src/codegen.rs index 16de57a5fef..6ef5dd0e2e2 100644 --- a/crates/e2e/macro/src/codegen.rs +++ b/crates/e2e/macro/src/codegen.rs @@ -71,8 +71,8 @@ impl InkE2ETest { build_full_client(&environment, exec_build_contracts, node_url) } #[cfg(any(test, feature = "drink"))] - Backend::RuntimeOnly { runtime } => { - build_runtime_client(exec_build_contracts, runtime) + Backend::RuntimeOnly(runtime) => { + build_runtime_client(exec_build_contracts, runtime.into()) } }; @@ -154,12 +154,7 @@ fn build_full_client( } #[cfg(any(test, feature = "drink"))] -fn build_runtime_client( - contracts: TokenStream2, - runtime: Option, -) -> TokenStream2 { - let runtime = - runtime.unwrap_or_else(|| syn::parse_quote! { ::ink_e2e::MinimalRuntime }); +fn build_runtime_client(contracts: TokenStream2, runtime: syn::Path) -> TokenStream2 { quote! { let contracts = #contracts; let mut client = ::ink_e2e::DrinkClient::<_, _, #runtime>::new(contracts); diff --git a/crates/e2e/macro/src/config.rs b/crates/e2e/macro/src/config.rs index a9d48d026d2..4663f59b031 100644 --- a/crates/e2e/macro/src/config.rs +++ b/crates/e2e/macro/src/config.rs @@ -26,7 +26,27 @@ pub enum Backend { /// This runs a runtime emulator within `TestExternalities` (using drink! library) in /// the same process as the test. #[cfg(any(test, feature = "drink"))] - RuntimeOnly { runtime: Option }, + RuntimeOnly(RuntimeOnly), +} + +/// The runtime emulator that should be used within `TestExternalities` (using drink! +/// library). +#[cfg(any(test, feature = "drink"))] +#[derive(Clone, Eq, PartialEq, Debug, darling::FromMeta)] +pub enum RuntimeOnly { + #[darling(word)] + Default, + Runtime(syn::Path), +} + +#[cfg(any(test, feature = "drink"))] +impl From for syn::Path { + fn from(value: RuntimeOnly) -> Self { + match value { + RuntimeOnly::Default => syn::parse_quote! { ::ink_e2e::MinimalRuntime }, + RuntimeOnly::Runtime(path) => path, + } + } } /// The End-to-End test configuration. @@ -100,7 +120,7 @@ mod tests { let input = quote! { additional_contracts = "adder/Cargo.toml flipper/Cargo.toml", environment = crate::CustomEnvironment, - backend(runtime_only()), + backend(runtime_only), node_url = "ws://127.0.0.1:8000" }; let config = @@ -115,7 +135,7 @@ mod tests { Some(syn::parse_quote! { crate::CustomEnvironment }) ); - assert_eq!(config.backend(), Backend::RuntimeOnly { runtime: None }); + assert_eq!(config.backend(), Backend::RuntimeOnly(RuntimeOnly::Default)); assert_eq!(config.node_url(), Some(String::from("ws://127.0.0.1:8000"))); std::env::set_var("CONTRACTS_NODE_URL", "ws://127.0.0.1:9000"); @@ -132,9 +152,9 @@ mod tests { assert_eq!( config.backend(), - Backend::RuntimeOnly { - runtime: Some(syn::parse_quote! { ::ink_e2e::MinimalRuntime }) - } + Backend::RuntimeOnly(RuntimeOnly::Runtime( + syn::parse_quote! { ::ink_e2e::MinimalRuntime } + )) ); assert_eq!(config.node_url(), None) } diff --git a/integration-tests/e2e-runtime-only-backend/lib.rs b/integration-tests/e2e-runtime-only-backend/lib.rs index 9f3a66d350e..95dc889628c 100644 --- a/integration-tests/e2e-runtime-only-backend/lib.rs +++ b/integration-tests/e2e-runtime-only-backend/lib.rs @@ -55,7 +55,7 @@ pub mod flipper { /// - flip the flipper /// - get the flipper's value /// - assert that the value is `true` - #[ink_e2e::test(backend(runtime_only()))] + #[ink_e2e::test(backend(runtime_only))] async fn it_works(mut client: Client) -> E2EResult<()> { // given const INITIAL_VALUE: bool = false; @@ -88,7 +88,7 @@ pub mod flipper { /// - transfer some funds to the contract using runtime call /// - get the contract's balance again /// - assert that the contract's balance increased by the transferred amount - #[ink_e2e::test(backend(runtime_only()))] + #[ink_e2e::test(backend(runtime_only))] async fn runtime_call_works() -> E2EResult<()> { // given let mut constructor = FlipperRef::new(false); From 5927d8c87cea8caaa314d4dd92cb94fab9137c81 Mon Sep 17 00:00:00 2001 From: davidsemakula Date: Tue, 30 Jan 2024 06:29:46 +0300 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 231ca36d2b2..91eec98af3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Linter: Publish the linting crates on crates.io - [#2060](https://github.com/paritytech/ink/pull/2060) - [E2E] Added `create_call_builder` for testing existing contracts - [#2075](https://github.com/paritytech/ink/pull/2075) +### Changed +- Improve syntax for ink! e2e `runtime_only` attribute argument - [#2083](https://github.com/paritytech/ink/pull/2083) + ### Fixed - Fix the `StorageVec` type by excluding the `len_cached` field from its type info - [#2052](https://github.com/paritytech/ink/pull/2052)