-
Notifications
You must be signed in to change notification settings - Fork 480
Support custom environment in E2E tests #1645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HCastano
merged 19 commits into
use-ink:master
from
pmikolajczyk41:pmikolajczyk41/e2e/custom-env
Feb 14, 2023
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d66a194
Remove seemingly unused struct
pmikolajczyk41 d91ab9c
Customize environment in e2e
pmikolajczyk41 3ded505
Example contract
pmikolajczyk41 0821d69
Ignore test
pmikolajczyk41 1bd6c99
CHANGELOG.md, README.md
pmikolajczyk41 f968ffe
Remove feature leftover
pmikolajczyk41 e289ded
Review comments
pmikolajczyk41 6a63605
Remove dependencies
pmikolajczyk41 a7499b0
syn::Path
pmikolajczyk41 557b4ea
::ink
pmikolajczyk41 6a914d5
README.md
pmikolajczyk41 192047f
Example docs
pmikolajczyk41 d22506b
Review
pmikolajczyk41 5abff86
Add feature instead of ignoring
pmikolajczyk41 d1737eb
Merge remote-tracking branch 'origin/master' into pmikolajczyk41/e2e/…
pmikolajczyk41 9e9d0de
Doc rewording. Event type matching
pmikolajczyk41 21f0651
Missing testcase
pmikolajczyk41 0953a0f
Fix test
pmikolajczyk41 3921217
Fix test
pmikolajczyk41 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,13 @@ pub struct E2EConfig { | |
| whitelisted_attributes: WhitelistedAttributes, | ||
| /// Additional contracts that have to be built before executing the test. | ||
| additional_contracts: Vec<String>, | ||
| /// The [`Environment`](https://docs.rs/ink_env/4.0.0-rc/ink_env/trait.Environment.html) to use | ||
| /// during test execution. | ||
| /// | ||
| /// If no `Environment` is specified, the | ||
| /// [`DefaultEnvironment`](https://docs.rs/ink_env/4.0.0-rc/ink_env/enum.DefaultEnvironment.html) | ||
| /// will be used. | ||
| environment: Option<syn::Path>, | ||
| } | ||
|
|
||
| impl TryFrom<ast::AttributeArgs> for E2EConfig { | ||
|
|
@@ -36,6 +43,7 @@ impl TryFrom<ast::AttributeArgs> for E2EConfig { | |
| fn try_from(args: ast::AttributeArgs) -> Result<Self, Self::Error> { | ||
| let mut whitelisted_attributes = WhitelistedAttributes::default(); | ||
| let mut additional_contracts: Option<(syn::LitStr, ast::MetaNameValue)> = None; | ||
| let mut environment: Option<(syn::Path, ast::MetaNameValue)> = None; | ||
|
|
||
| for arg in args.into_iter() { | ||
| if arg.name.is_ident("keep_attr") { | ||
|
|
@@ -46,15 +54,27 @@ impl TryFrom<ast::AttributeArgs> for E2EConfig { | |
| ast, | ||
| arg, | ||
| "additional_contracts", | ||
| "e2e test", | ||
| "E2E test", | ||
| )) | ||
| } | ||
| if let ast::PathOrLit::Lit(syn::Lit::Str(lit_str)) = &arg.value { | ||
| additional_contracts = Some((lit_str.clone(), arg)) | ||
| } else { | ||
| return Err(format_err_spanned!( | ||
| arg, | ||
| "expected a bool literal for `additional_contracts` ink! e2e test configuration argument", | ||
| "expected a string literal for `additional_contracts` ink! E2E test configuration argument", | ||
| )) | ||
| } | ||
| } else if arg.name.is_ident("environment") { | ||
| if let Some((_, ast)) = environment { | ||
| return Err(duplicate_config_err(ast, arg, "environment", "E2E test")) | ||
| } | ||
| if let ast::PathOrLit::Path(path) = &arg.value { | ||
| environment = Some((path.clone(), arg)) | ||
| } else { | ||
| return Err(format_err_spanned!( | ||
| arg, | ||
| "expected a path for `environment` ink! E2E test configuration argument", | ||
| )) | ||
| } | ||
| } else { | ||
|
|
@@ -67,9 +87,12 @@ impl TryFrom<ast::AttributeArgs> for E2EConfig { | |
| let additional_contracts = additional_contracts | ||
| .map(|(value, _)| value.value().split(' ').map(String::from).collect()) | ||
| .unwrap_or_else(Vec::new); | ||
| let environment = environment.map(|(path, _)| path); | ||
|
|
||
| Ok(E2EConfig { | ||
| additional_contracts, | ||
| whitelisted_attributes, | ||
| environment, | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -80,20 +103,10 @@ impl E2EConfig { | |
| pub fn additional_contracts(&self) -> Vec<String> { | ||
| self.additional_contracts.clone() | ||
| } | ||
| } | ||
|
|
||
| /// The environmental types definition. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct Environment { | ||
| /// The underlying Rust type. | ||
| pub path: syn::Path, | ||
| } | ||
|
|
||
| impl Default for Environment { | ||
| fn default() -> Self { | ||
| Self { | ||
| path: syn::parse_quote! { ::ink_env::DefaultEnvironment }, | ||
| } | ||
| /// Custom environment for the contracts, if specified. | ||
| pub fn environment(&self) -> Option<syn::Path> { | ||
| self.environment.clone() | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -128,18 +141,72 @@ mod tests { | |
| } | ||
|
|
||
| #[test] | ||
| fn duplicate_args_fails() { | ||
| fn duplicate_additional_contracts_fails() { | ||
| assert_try_from( | ||
| syn::parse_quote! { | ||
| additional_contracts = "adder/Cargo.toml", | ||
| additional_contracts = "adder/Cargo.toml", | ||
| }, | ||
| Err( | ||
| "encountered duplicate ink! e2e test `additional_contracts` configuration argument", | ||
| "encountered duplicate ink! E2E test `additional_contracts` configuration argument", | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn duplicate_environment_fails() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would also be cool to see some UI tests for the E2E macros, but that would be for a future PR |
||
| assert_try_from( | ||
| syn::parse_quote! { | ||
| environment = crate::CustomEnvironment, | ||
| environment = crate::CustomEnvironment, | ||
| }, | ||
| Err( | ||
| "encountered duplicate ink! E2E test `environment` configuration argument", | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn environment_as_literal_fails() { | ||
| assert_try_from( | ||
| syn::parse_quote! { | ||
| environment = "crate::CustomEnvironment", | ||
| }, | ||
| Err("expected a path for `environment` ink! E2E test configuration argument"), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn specifying_environment_works() { | ||
| assert_try_from( | ||
| syn::parse_quote! { | ||
| environment = crate::CustomEnvironment, | ||
| }, | ||
| Ok(E2EConfig { | ||
| environment: Some(syn::parse_quote! { crate::CustomEnvironment }), | ||
| ..Default::default() | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn full_config_works() { | ||
| assert_try_from( | ||
| syn::parse_quote! { | ||
| additional_contracts = "adder/Cargo.toml flipper/Cargo.toml", | ||
| environment = crate::CustomEnvironment, | ||
HCastano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| Ok(E2EConfig { | ||
| whitelisted_attributes: Default::default(), | ||
| additional_contracts: vec![ | ||
| "adder/Cargo.toml".into(), | ||
| "flipper/Cargo.toml".into(), | ||
| ], | ||
| environment: Some(syn::parse_quote! { crate::CustomEnvironment }), | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn keep_attr_works() { | ||
| let mut attrs = WhitelistedAttributes::default(); | ||
|
|
@@ -152,6 +219,7 @@ mod tests { | |
| Ok(E2EConfig { | ||
| whitelisted_attributes: attrs, | ||
| additional_contracts: Vec::new(), | ||
| environment: None, | ||
| }), | ||
| ) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Ignore build artifacts from the local tests sub-crate. | ||
| /target/ | ||
|
|
||
| # Ignore backup files creates by cargo fmt. | ||
| **/*.rs.bk | ||
|
|
||
| # Remove Cargo.lock when creating an executable, leave it for libraries | ||
| # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| Cargo.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| [package] | ||
| name = "custom-environment" | ||
| version = "4.0.0-rc" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2021" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| ink = { path = "../../crates/ink", default-features = false } | ||
|
|
||
| scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
| scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true } | ||
|
|
||
| [dev-dependencies] | ||
| ink_e2e = { path = "../../crates/e2e" } | ||
|
|
||
| [lib] | ||
HCastano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| path = "lib.rs" | ||
|
|
||
| [features] | ||
| default = ["std"] | ||
| std = [ | ||
| "ink/std", | ||
| "scale/std", | ||
| "scale-info/std", | ||
| ] | ||
| ink-as-dependency = [] | ||
| e2e-tests = [] | ||
|
|
||
| # Assumes that the node used in E2E testing allows for at least 6 event topics. | ||
| permissive-node = [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # `custom-environment` example | ||
|
|
||
| ## What is this example about? | ||
|
|
||
| It demonstrates how to use custom environment, both in the contract and in the E2E tests. | ||
|
|
||
| ## Chain-side configuration | ||
|
|
||
| To integrate this example into Substrate you need to adjust pallet contracts configuration in your runtime: | ||
|
|
||
| ```rust | ||
| // In your node's runtime configuration file (runtime.rs) | ||
| parameter_types! { | ||
| pub Schedule: pallet_contracts::Schedule<Runtime> = pallet_contracts::Schedule::<Runtime> { | ||
| limits: pallet_contracts::Limits { | ||
| event_topics: 6, | ||
| ..Default::default() | ||
| }, | ||
| ..Default::default() | ||
| }; | ||
| } | ||
|
|
||
| impl pallet_contracts::Config for Runtime { | ||
| … | ||
| type Schedule = Schedule; | ||
| … | ||
| } | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.