From 476c881672dba0c7a4c244d9a949a752cfd9ac35 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Tue, 14 Jul 2020 09:04:05 +0200 Subject: [PATCH 01/26] Initial commit Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master From 8117934976d3c9016c29e58f758b5f9b8f4f8e05 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Tue, 14 Jul 2020 12:45:10 +0200 Subject: [PATCH 02/26] Add async test helper to timeout and provide a task_executor automatically --- .gitlab-ci.yml | 1 + Cargo.lock | 19 ++++ Cargo.toml | 1 + test-utils/derive/Cargo.toml | 30 ++++++ test-utils/derive/src/lib.rs | 101 ++++++++++++++++++ test-utils/derive/tests/basic.rs | 58 ++++++++++ test-utils/derive/tests/errors.rs | 24 +++++ .../derive/tests/missing-func-parameter.rs | 24 +++++ .../tests/missing-func-parameter.stderr | 5 + .../derive/tests/too-many-func-parameters.rs | 26 +++++ .../tests/too-many-func-parameters.stderr | 13 +++ 11 files changed, 302 insertions(+) create mode 100644 test-utils/derive/Cargo.toml create mode 100644 test-utils/derive/src/lib.rs create mode 100644 test-utils/derive/tests/basic.rs create mode 100644 test-utils/derive/tests/errors.rs create mode 100644 test-utils/derive/tests/missing-func-parameter.rs create mode 100644 test-utils/derive/tests/missing-func-parameter.stderr create mode 100644 test-utils/derive/tests/too-many-func-parameters.rs create mode 100644 test-utils/derive/tests/too-many-func-parameters.stderr diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d3a7f36980063..2985bcd83ebe6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -207,6 +207,7 @@ test-linux-stable: &test-linux - $DEPLOY_TAG script: - WASM_BUILD_NO_COLOR=1 time cargo test --all --release --verbose --locked + - WASM_BUILD_NO_COLOR=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout - sccache -s unleash-check: diff --git a/Cargo.lock b/Cargo.lock index 0b24f9ef57218..edc03bff086af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1124,6 +1124,12 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "dissimilar" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc4b29f4b9bb94bf267d57269fd0706d343a160937108e9619fe380645428abb" + [[package]] name = "dns-parser" version = "0.8.0" @@ -8508,6 +8514,18 @@ dependencies = [ name = "substrate-test-utils" version = "2.0.0-rc4" +[[package]] +name = "substrate-test-utils-derive" +version = "0.8.0-rc4" +dependencies = [ + "futures 0.3.5", + "quote 1.0.6", + "sc-service", + "syn 1.0.33", + "tokio 0.2.18", + "trybuild", +] + [[package]] name = "substrate-wasm-builder" version = "1.0.11" @@ -9256,6 +9274,7 @@ version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "459186ab1afd6d93bd23c2269125f4f7694f8771fe0e64434b4bdc212b94034d" dependencies = [ + "dissimilar", "glob 0.3.0", "lazy_static", "serde", diff --git a/Cargo.toml b/Cargo.toml index ba146e55bca3f..fdc02f74d16f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -171,6 +171,7 @@ members = [ "primitives/utils", "primitives/wasm-interface", "test-utils/client", + "test-utils/derive", "test-utils/runtime", "test-utils/runtime/client", "test-utils/runtime/transaction-pool", diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml new file mode 100644 index 0000000000000..6d192556057bd --- /dev/null +++ b/test-utils/derive/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "substrate-test-utils-derive" +version = "0.8.0-rc4" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +autotests = false + +[dependencies] +quote = "1" +syn = { version = "1", default-features = false } + +[dev-dependencies] +tokio = { version = "0.2.13", features = ["macros"] } +futures = { version = "0.3.1", features = ["compat"] } +sc-service = { path = "../../client/service" } +trybuild = { version = "1.0", features = ["diff"] } + +[lib] +proc-macro = true + +[[test]] +name = "basic" +path = "tests/basic.rs" + +[[test]] +name = "errors" +path = "tests/errors.rs" diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs new file mode 100644 index 0000000000000..e13bac4d79b74 --- /dev/null +++ b/test-utils/derive/src/lib.rs @@ -0,0 +1,101 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use proc_macro::TokenStream; +use quote::quote; + +#[proc_macro_attribute] +pub fn test( + args: proc_macro::TokenStream, + item: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + impl_test(args, item) +} + +fn impl_test( + args: proc_macro::TokenStream, + item: proc_macro::TokenStream, +) -> proc_macro::TokenStream { + let input = syn::parse_macro_input!(item as syn::ItemFn); + let args = syn::parse_macro_input!(args as syn::AttributeArgs); + + parse_knobs(input, args).unwrap_or_else(|e| e.to_compile_error().into()) +} + +fn parse_knobs( + mut input: syn::ItemFn, + args: syn::AttributeArgs, +) -> Result { + let sig = &mut input.sig; + let body = &input.block; + let attrs = &input.attrs; + let vis = input.vis; + + if sig.inputs.len() != 1 { + let msg = "the test function accepts only one argument of type sc_service::TaskExecutor"; + return Ok(syn::Error::new_spanned(&sig, msg).to_compile_error().into()); + } + let (task_executor_name, task_executor_type) = match sig.inputs.pop().map(|x| x.into_value()) { + Some(syn::FnArg::Typed(x)) => (x.pat, x.ty), + _ => { + let msg = + "the test function accepts only one argument of type sc_service::TaskExecutor"; + return Ok(syn::Error::new_spanned(&sig, msg).to_compile_error().into()); + } + }; + + let header = { + quote! { + #[tokio::test(#(#args)*)] + } + }; + + let result = quote! { + #header + #(#attrs)* + #vis #sig { + use futures::future::FutureExt; + + let #task_executor_name: #task_executor_type = (|fut, _| { + tokio::spawn(fut); + }) + .into(); + let timeout_task = tokio::time::delay_for( + std::time::Duration::from_secs( + option_env!("INTEGRATION_TEST_ALLOWED_TIME") + .and_then(|x| x.parse().ok()) + .unwrap_or(600)) + ).fuse(); + let actual_test_task = async move { + #body + } + .fuse(); + + futures::pin_mut!(timeout_task, actual_test_task); + + futures::select! { + _ = timeout_task => { + panic!("the test took too long"); + }, + _ = actual_test_task => {}, + } + } + }; + + Ok(result.into()) +} diff --git a/test-utils/derive/tests/basic.rs b/test-utils/derive/tests/basic.rs new file mode 100644 index 0000000000000..100d0121a19dd --- /dev/null +++ b/test-utils/derive/tests/basic.rs @@ -0,0 +1,58 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use sc_service::{TaskExecutor, TaskType}; + +#[substrate_test_utils_derive::test] +async fn basic_test(_: TaskExecutor) { + assert!(true); +} + +#[substrate_test_utils_derive::test] +#[should_panic(expected = "boo!")] +async fn panicking_test(_: TaskExecutor) { + panic!("boo!"); +} + +#[substrate_test_utils_derive::test(max_threads = 2)] +async fn basic_test_with_args(_: TaskExecutor) { + assert!(true); +} + +#[substrate_test_utils_derive::test] +async fn rename_argument(ex: TaskExecutor) { + let ex2 = ex.clone(); + ex2.spawn(Box::pin(async { () }), TaskType::Blocking); + assert!(true); +} + +#[substrate_test_utils_derive::test] +#[should_panic(expected = "test took too long")] +// NOTE: enable this test only after setting INTEGRATION_TEST_ALLOWED_TIME to a smaller value +// +// INTEGRATION_TEST_ALLOWED_TIME=1 cargo test -- --ignored timeout +#[ignore] +async fn timeout(_: TaskExecutor) { + tokio::time::delay_for(std::time::Duration::from_secs( + option_env!("INTEGRATION_TEST_ALLOWED_TIME") + .expect("env var INTEGRATION_TEST_ALLOWED_TIME has been provided by the user") + .parse::() + .unwrap() + 1, + )) + .await; +} diff --git a/test-utils/derive/tests/errors.rs b/test-utils/derive/tests/errors.rs new file mode 100644 index 0000000000000..6b24edcc64fb5 --- /dev/null +++ b/test-utils/derive/tests/errors.rs @@ -0,0 +1,24 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#[test] +fn tests() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/missing-func-parameter.rs"); + t.compile_fail("tests/too-many-func-parameters.rs"); +} diff --git a/test-utils/derive/tests/missing-func-parameter.rs b/test-utils/derive/tests/missing-func-parameter.rs new file mode 100644 index 0000000000000..be615657ad467 --- /dev/null +++ b/test-utils/derive/tests/missing-func-parameter.rs @@ -0,0 +1,24 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#[substrate_test_utils_derive::test] +async fn missing_func_parameter() { + assert!(true); +} + +fn main() {} diff --git a/test-utils/derive/tests/missing-func-parameter.stderr b/test-utils/derive/tests/missing-func-parameter.stderr new file mode 100644 index 0000000000000..fbe0bc69918e8 --- /dev/null +++ b/test-utils/derive/tests/missing-func-parameter.stderr @@ -0,0 +1,5 @@ +error: the test function accepts only one argument of type sc_service::TaskExecutor + --> $DIR/missing-func-parameter.rs:20:1 + | +20 | async fn missing_func_parameter() { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/test-utils/derive/tests/too-many-func-parameters.rs b/test-utils/derive/tests/too-many-func-parameters.rs new file mode 100644 index 0000000000000..5377487ade72a --- /dev/null +++ b/test-utils/derive/tests/too-many-func-parameters.rs @@ -0,0 +1,26 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use sc_service::TaskExecutor; + +#[substrate_test_utils_derive::test] +async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { + assert!(true); +} + +fn main() {} diff --git a/test-utils/derive/tests/too-many-func-parameters.stderr b/test-utils/derive/tests/too-many-func-parameters.stderr new file mode 100644 index 0000000000000..712fb3486db54 --- /dev/null +++ b/test-utils/derive/tests/too-many-func-parameters.stderr @@ -0,0 +1,13 @@ +error: the test function accepts only one argument of type sc_service::TaskExecutor + --> $DIR/too-many-func-parameters.rs:22:1 + | +22 | async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: unused import: `sc_service::TaskExecutor` + --> $DIR/too-many-func-parameters.rs:19:5 + | +19 | use sc_service::TaskExecutor; + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default From 279679850e04edb1206808346962c9a6c87cb392 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 11:01:48 +0200 Subject: [PATCH 03/26] simplify error message to avoid difference between CI and locally --- test-utils/derive/tests/errors.rs | 2 +- test-utils/derive/tests/too-many-func-parameters.rs | 1 + .../derive/tests/too-many-func-parameters.stderr | 12 ++---------- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/test-utils/derive/tests/errors.rs b/test-utils/derive/tests/errors.rs index 6b24edcc64fb5..60dce68500d26 100644 --- a/test-utils/derive/tests/errors.rs +++ b/test-utils/derive/tests/errors.rs @@ -17,7 +17,7 @@ // along with this program. If not, see . #[test] -fn tests() { +fn substrate_test_utils_derive_trybuild() { let t = trybuild::TestCases::new(); t.compile_fail("tests/missing-func-parameter.rs"); t.compile_fail("tests/too-many-func-parameters.rs"); diff --git a/test-utils/derive/tests/too-many-func-parameters.rs b/test-utils/derive/tests/too-many-func-parameters.rs index 5377487ade72a..fd367cd088b0a 100644 --- a/test-utils/derive/tests/too-many-func-parameters.rs +++ b/test-utils/derive/tests/too-many-func-parameters.rs @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#[allow(unused_imports)] use sc_service::TaskExecutor; #[substrate_test_utils_derive::test] diff --git a/test-utils/derive/tests/too-many-func-parameters.stderr b/test-utils/derive/tests/too-many-func-parameters.stderr index 712fb3486db54..e30bb4ed8ee85 100644 --- a/test-utils/derive/tests/too-many-func-parameters.stderr +++ b/test-utils/derive/tests/too-many-func-parameters.stderr @@ -1,13 +1,5 @@ error: the test function accepts only one argument of type sc_service::TaskExecutor - --> $DIR/too-many-func-parameters.rs:22:1 + --> $DIR/too-many-func-parameters.rs:23:1 | -22 | async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { +23 | async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -warning: unused import: `sc_service::TaskExecutor` - --> $DIR/too-many-func-parameters.rs:19:5 - | -19 | use sc_service::TaskExecutor; - | ^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: `#[warn(unused_imports)]` on by default From a0535f71dc8df6fa53ea1a210817fe46d77a9429 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 11:44:22 +0200 Subject: [PATCH 04/26] forgot env var --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9039831b28e7c..0adcef243d5d9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -230,7 +230,7 @@ test-linux-stable: &test-linux script: # this job runs all tests in former runtime-benchmarks, frame-staking and wasmtime tests - time cargo test --workspace --locked --release --verbose --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml - - WASM_BUILD_NO_COLOR=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout + - WASM_BUILD_NO_COLOR=1 INTEGRATION_TEST_ALLOWED_TIME=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout - sccache -s unleash-check: From b86cc8e5769f8fe627e992c346fba48ef997b7b1 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 14:20:21 +0200 Subject: [PATCH 05/26] Use runtime env var instead of build env var --- test-utils/derive/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index 1449b2ef0de7c..b61301e52104f 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -77,7 +77,8 @@ fn parse_knobs( .into(); let timeout_task = tokio::time::delay_for( std::time::Duration::from_secs( - option_env!("INTEGRATION_TEST_ALLOWED_TIME") + std::env::var("INTEGRATION_TEST_ALLOWED_TIME") + .ok() .and_then(|x| x.parse().ok()) .unwrap_or(600)) ).fuse(); From 6b2f66f005f6e028c010e7e445a1bdc9a296cc2e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 16:26:33 +0200 Subject: [PATCH 06/26] Rename variable to SUBSTRATE_TEST_TIMEOUT --- .gitlab-ci.yml | 2 +- test-utils/derive/src/lib.rs | 2 +- test-utils/derive/tests/basic.rs | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0adcef243d5d9..a9f2343fc9c92 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -230,7 +230,7 @@ test-linux-stable: &test-linux script: # this job runs all tests in former runtime-benchmarks, frame-staking and wasmtime tests - time cargo test --workspace --locked --release --verbose --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml - - WASM_BUILD_NO_COLOR=1 INTEGRATION_TEST_ALLOWED_TIME=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout + - WASM_BUILD_NO_COLOR=1 SUBSTRATE_TEST_TIMEOUT=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout - sccache -s unleash-check: diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index b61301e52104f..7fa2094117be5 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -77,7 +77,7 @@ fn parse_knobs( .into(); let timeout_task = tokio::time::delay_for( std::time::Duration::from_secs( - std::env::var("INTEGRATION_TEST_ALLOWED_TIME") + std::env::var("SUBSTRATE_TEST_TIMEOUT") .ok() .and_then(|x| x.parse().ok()) .unwrap_or(600)) diff --git a/test-utils/derive/tests/basic.rs b/test-utils/derive/tests/basic.rs index 100d0121a19dd..8f8fda111cf32 100644 --- a/test-utils/derive/tests/basic.rs +++ b/test-utils/derive/tests/basic.rs @@ -43,14 +43,14 @@ async fn rename_argument(ex: TaskExecutor) { #[substrate_test_utils_derive::test] #[should_panic(expected = "test took too long")] -// NOTE: enable this test only after setting INTEGRATION_TEST_ALLOWED_TIME to a smaller value +// NOTE: enable this test only after setting SUBSTRATE_TEST_TIMEOUT to a smaller value // -// INTEGRATION_TEST_ALLOWED_TIME=1 cargo test -- --ignored timeout +// SUBSTRATE_TEST_TIMEOUT=1 cargo test -- --ignored timeout #[ignore] async fn timeout(_: TaskExecutor) { tokio::time::delay_for(std::time::Duration::from_secs( - option_env!("INTEGRATION_TEST_ALLOWED_TIME") - .expect("env var INTEGRATION_TEST_ALLOWED_TIME has been provided by the user") + std::env::var("SUBSTRATE_TEST_TIMEOUT") + .expect("env var SUBSTRATE_TEST_TIMEOUT has been provided by the user") .parse::() .unwrap() + 1, )) From 2a47de6746d707fb246f0ff0530bee4e3cd44fb2 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 22:35:17 +0200 Subject: [PATCH 07/26] CLEANUP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- test-utils/derive/src/lib.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index 7fa2094117be5..f72f8079e248d 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -20,17 +20,11 @@ use proc_macro::TokenStream; use quote::quote; #[proc_macro_attribute] -pub fn test( - args: proc_macro::TokenStream, - item: proc_macro::TokenStream, -) -> proc_macro::TokenStream { +pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { impl_test(args, item) } -fn impl_test( - args: proc_macro::TokenStream, - item: proc_macro::TokenStream, -) -> proc_macro::TokenStream { +fn impl_test(args: TokenStream, item: TokenStream) -> TokenStream { let input = syn::parse_macro_input!(item as syn::ItemFn); let args = syn::parse_macro_input!(args as syn::AttributeArgs); From 24a165bffd90b3ab84c4adce304d7284e94fb028 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 22 Jul 2020 22:36:05 +0200 Subject: [PATCH 08/26] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- test-utils/derive/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index f72f8079e248d..4f4cc04fc3995 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -42,14 +42,14 @@ fn parse_knobs( if sig.inputs.len() != 1 { let msg = "the test function accepts only one argument of type sc_service::TaskExecutor"; - return Ok(syn::Error::new_spanned(&sig, msg).to_compile_error().into()); + return Err(syn::Error::new_spanned(&sig, msg)); } let (task_executor_name, task_executor_type) = match sig.inputs.pop().map(|x| x.into_value()) { Some(syn::FnArg::Typed(x)) => (x.pat, x.ty), _ => { let msg = "the test function accepts only one argument of type sc_service::TaskExecutor"; - return Ok(syn::Error::new_spanned(&sig, msg).to_compile_error().into()); + return Err(syn::Error::new_spanned(&sig, msg)); } }; From f173c1c682146a801b6ed788810823d1f884f3cb Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 24 Jul 2020 08:19:37 +0200 Subject: [PATCH 09/26] Re-export from test-utils --- Cargo.lock | 11 +++++--- test-utils/Cargo.toml | 25 +++++++++++++++++++ test-utils/derive/Cargo.toml | 15 ----------- test-utils/derive/src/lib.rs | 10 ++++---- test-utils/src/lib.rs | 9 +++++++ test-utils/{derive => }/tests/basic.rs | 10 ++++---- test-utils/{derive => }/tests/errors.rs | 0 .../tests/missing-func-parameter.rs | 2 +- .../tests/missing-func-parameter.stderr | 0 .../tests/too-many-func-parameters.rs | 2 +- .../tests/too-many-func-parameters.stderr | 0 11 files changed, 53 insertions(+), 31 deletions(-) rename test-utils/{derive => }/tests/basic.rs (89%) rename test-utils/{derive => }/tests/errors.rs (100%) rename test-utils/{derive => }/tests/missing-func-parameter.rs (95%) rename test-utils/{derive => }/tests/missing-func-parameter.stderr (100%) rename test-utils/{derive => }/tests/too-many-func-parameters.rs (96%) rename test-utils/{derive => }/tests/too-many-func-parameters.stderr (100%) diff --git a/Cargo.lock b/Cargo.lock index c44ff60b85d65..93c84c4710557 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8614,17 +8614,20 @@ dependencies = [ [[package]] name = "substrate-test-utils" version = "2.0.0-rc5" +dependencies = [ + "futures 0.3.5", + "sc-service", + "substrate-test-utils-derive", + "tokio 0.2.21", + "trybuild", +] [[package]] name = "substrate-test-utils-derive" version = "0.8.0-rc4" dependencies = [ - "futures 0.3.5", "quote 1.0.6", - "sc-service", "syn 1.0.33", - "tokio 0.2.21", - "trybuild", ] [[package]] diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 6d56de9ff99c2..ec80809f56e9b 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -6,6 +6,31 @@ edition = "2018" license = "Apache-2.0" homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" +autotests = false [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +futures = { version = "0.3.1", features = ["compat"], optional = true } +substrate-test-utils-derive = { path = "./derive", optional = true } +tokio = { version = "0.2.13", features = ["macros"], optional = true } + +[dev-dependencies] +sc-service = { path = "../client/service" } +trybuild = { version = "1.0", features = ["diff"] } + +[features] +macros = [ + "futures", + "substrate-test-utils-derive", + "tokio", +] + +[[test]] +name = "basic" +path = "tests/basic.rs" + +[[test]] +name = "errors" +path = "tests/errors.rs" diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml index 6d192556057bd..45c1d6c4961d1 100644 --- a/test-utils/derive/Cargo.toml +++ b/test-utils/derive/Cargo.toml @@ -6,25 +6,10 @@ edition = "2018" license = "Apache-2.0" homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" -autotests = false [dependencies] quote = "1" syn = { version = "1", default-features = false } -[dev-dependencies] -tokio = { version = "0.2.13", features = ["macros"] } -futures = { version = "0.3.1", features = ["compat"] } -sc-service = { path = "../../client/service" } -trybuild = { version = "1.0", features = ["diff"] } - [lib] proc-macro = true - -[[test]] -name = "basic" -path = "tests/basic.rs" - -[[test]] -name = "errors" -path = "tests/errors.rs" diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index 4f4cc04fc3995..af7d944fa0c10 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -63,13 +63,13 @@ fn parse_knobs( #header #(#attrs)* #vis #sig { - use futures::future::FutureExt; + use substrate_test_utils::futures::future::FutureExt; let #task_executor_name: #task_executor_type = (|fut, _| { - tokio::spawn(fut).map(drop) + substrate_test_utils::tokio::spawn(fut).map(drop) }) .into(); - let timeout_task = tokio::time::delay_for( + let timeout_task = substrate_test_utils::tokio::time::delay_for( std::time::Duration::from_secs( std::env::var("SUBSTRATE_TEST_TIMEOUT") .ok() @@ -81,9 +81,9 @@ fn parse_knobs( } .fuse(); - futures::pin_mut!(timeout_task, actual_test_task); + substrate_test_utils::futures::pin_mut!(timeout_task, actual_test_task); - futures::select! { + substrate_test_utils::futures::select! { _ = timeout_task => { panic!("the test took too long"); }, diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 8163460df7427..ea379c9e0866d 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -17,6 +17,15 @@ //! Test utils +#[doc(hidden)] +#[cfg(feature = "macros")] +pub use futures; +#[cfg(feature = "macros")] +pub use substrate_test_utils_derive::test; +#[doc(hidden)] +#[cfg(feature = "macros")] +pub use tokio; + /// Panic when the vectors are different, without taking the order into account. /// /// # Examples diff --git a/test-utils/derive/tests/basic.rs b/test-utils/tests/basic.rs similarity index 89% rename from test-utils/derive/tests/basic.rs rename to test-utils/tests/basic.rs index 8f8fda111cf32..3e96bfe83d3a7 100644 --- a/test-utils/derive/tests/basic.rs +++ b/test-utils/tests/basic.rs @@ -18,30 +18,30 @@ use sc_service::{TaskExecutor, TaskType}; -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] async fn basic_test(_: TaskExecutor) { assert!(true); } -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] #[should_panic(expected = "boo!")] async fn panicking_test(_: TaskExecutor) { panic!("boo!"); } -#[substrate_test_utils_derive::test(max_threads = 2)] +#[substrate_test_utils::test(max_threads = 2)] async fn basic_test_with_args(_: TaskExecutor) { assert!(true); } -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] async fn rename_argument(ex: TaskExecutor) { let ex2 = ex.clone(); ex2.spawn(Box::pin(async { () }), TaskType::Blocking); assert!(true); } -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] #[should_panic(expected = "test took too long")] // NOTE: enable this test only after setting SUBSTRATE_TEST_TIMEOUT to a smaller value // diff --git a/test-utils/derive/tests/errors.rs b/test-utils/tests/errors.rs similarity index 100% rename from test-utils/derive/tests/errors.rs rename to test-utils/tests/errors.rs diff --git a/test-utils/derive/tests/missing-func-parameter.rs b/test-utils/tests/missing-func-parameter.rs similarity index 95% rename from test-utils/derive/tests/missing-func-parameter.rs rename to test-utils/tests/missing-func-parameter.rs index be615657ad467..bd34a76902ef9 100644 --- a/test-utils/derive/tests/missing-func-parameter.rs +++ b/test-utils/tests/missing-func-parameter.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] async fn missing_func_parameter() { assert!(true); } diff --git a/test-utils/derive/tests/missing-func-parameter.stderr b/test-utils/tests/missing-func-parameter.stderr similarity index 100% rename from test-utils/derive/tests/missing-func-parameter.stderr rename to test-utils/tests/missing-func-parameter.stderr diff --git a/test-utils/derive/tests/too-many-func-parameters.rs b/test-utils/tests/too-many-func-parameters.rs similarity index 96% rename from test-utils/derive/tests/too-many-func-parameters.rs rename to test-utils/tests/too-many-func-parameters.rs index fd367cd088b0a..9aeadc2a88430 100644 --- a/test-utils/derive/tests/too-many-func-parameters.rs +++ b/test-utils/tests/too-many-func-parameters.rs @@ -19,7 +19,7 @@ #[allow(unused_imports)] use sc_service::TaskExecutor; -#[substrate_test_utils_derive::test] +#[substrate_test_utils::test] async fn too_many_func_parameters(task_executor_1: TaskExecutor, task_executor_2: TaskExecutor) { assert!(true); } diff --git a/test-utils/derive/tests/too-many-func-parameters.stderr b/test-utils/tests/too-many-func-parameters.stderr similarity index 100% rename from test-utils/derive/tests/too-many-func-parameters.stderr rename to test-utils/tests/too-many-func-parameters.stderr From 8e458717078b242ffce7d3c4f66241d76f075125 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 24 Jul 2020 08:19:53 +0200 Subject: [PATCH 10/26] Default value to 120 --- .gitlab-ci.yml | 3 +++ test-utils/derive/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9f2343fc9c92..fa2a9b47dd3a8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -79,6 +79,8 @@ default: interruptible: true tags: - linux-docker + variables: + SUBSTRATE_TEST_TIMEOUT: 600 .build-only: &build-only only: @@ -224,6 +226,7 @@ test-linux-stable: &test-linux RUSTFLAGS: "-Cdebug-assertions=y -Dwarnings" RUST_BACKTRACE: 1 WASM_BUILD_NO_COLOR: 1 + SUBSTRATE_TEST_TIMEOUT: 600 except: variables: - $DEPLOY_TAG diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index af7d944fa0c10..167f900a565b8 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -74,7 +74,7 @@ fn parse_knobs( std::env::var("SUBSTRATE_TEST_TIMEOUT") .ok() .and_then(|x| x.parse().ok()) - .unwrap_or(600)) + .unwrap_or(120)) ).fuse(); let actual_test_task = async move { #body From b7e1b1d4cae769796b1f4c16130974e33649158f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 24 Jul 2020 08:27:03 +0200 Subject: [PATCH 11/26] fix wrong crate in ci --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fa2a9b47dd3a8..0ce15e5bd158a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -233,7 +233,7 @@ test-linux-stable: &test-linux script: # this job runs all tests in former runtime-benchmarks, frame-staking and wasmtime tests - time cargo test --workspace --locked --release --verbose --features runtime-benchmarks --manifest-path bin/node/cli/Cargo.toml - - WASM_BUILD_NO_COLOR=1 SUBSTRATE_TEST_TIMEOUT=1 time cargo test -p substrate-test-utils-derive --release --verbose --locked -- --ignored timeout + - WASM_BUILD_NO_COLOR=1 SUBSTRATE_TEST_TIMEOUT=1 time cargo test -p substrate-test-utils --release --verbose --locked -- --ignored timeout - sccache -s unleash-check: From a200f34e74f58abb5fd5d90c80ec968808d8924f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 24 Jul 2020 08:31:16 +0200 Subject: [PATCH 12/26] Revert "Default value to 120" This reverts commit 8e458717078b242ffce7d3c4f66241d76f075125. --- .gitlab-ci.yml | 3 --- test-utils/derive/src/lib.rs | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0ce15e5bd158a..a0c573e263773 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -79,8 +79,6 @@ default: interruptible: true tags: - linux-docker - variables: - SUBSTRATE_TEST_TIMEOUT: 600 .build-only: &build-only only: @@ -226,7 +224,6 @@ test-linux-stable: &test-linux RUSTFLAGS: "-Cdebug-assertions=y -Dwarnings" RUST_BACKTRACE: 1 WASM_BUILD_NO_COLOR: 1 - SUBSTRATE_TEST_TIMEOUT: 600 except: variables: - $DEPLOY_TAG diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index 167f900a565b8..af7d944fa0c10 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -74,7 +74,7 @@ fn parse_knobs( std::env::var("SUBSTRATE_TEST_TIMEOUT") .ok() .and_then(|x| x.parse().ok()) - .unwrap_or(120)) + .unwrap_or(600)) ).fuse(); let actual_test_task = async move { #body From c157c22831c44d3083f335d40ca65a36a28f473d Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 6 Aug 2020 10:27:44 +0200 Subject: [PATCH 13/26] Fix version --- test-utils/derive/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml index 45c1d6c4961d1..98fe48f2fa120 100644 --- a/test-utils/derive/Cargo.toml +++ b/test-utils/derive/Cargo.toml @@ -8,8 +8,8 @@ homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" [dependencies] -quote = "1" -syn = { version = "1", default-features = false } +quote = "1.0.6" +syn = { version = "1.0.33", default-features = false } [lib] proc-macro = true From d074de02293d3f69c9a31eade1bfbfdbff70323a Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 6 Aug 2020 11:17:47 +0200 Subject: [PATCH 14/26] WIP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + test-utils/derive/Cargo.toml | 3 ++- test-utils/derive/src/lib.rs | 25 ++++++++++++++++++------- test-utils/test-crate/Cargo.toml | 16 ++++++++++++++++ test-utils/test-crate/src/main.rs | 28 ++++++++++++++++++++++++++++ test-utils/tests/basic.rs | 2 +- 7 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 test-utils/test-crate/Cargo.toml create mode 100644 test-utils/test-crate/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 93c84c4710557..305b1300ccef7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8626,10 +8626,20 @@ dependencies = [ name = "substrate-test-utils-derive" version = "0.8.0-rc4" dependencies = [ + "proc-macro-crate", "quote 1.0.6", "syn 1.0.33", ] +[[package]] +name = "substrate-test-utils-test-crate" +version = "0.1.0" +dependencies = [ + "sc-service", + "substrate-test-utils", + "tokio 0.2.21", +] + [[package]] name = "substrate-wasm-builder" version = "2.0.0" diff --git a/Cargo.toml b/Cargo.toml index fdc02f74d16f0..f22e3427a70a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -175,6 +175,7 @@ members = [ "test-utils/runtime", "test-utils/runtime/client", "test-utils/runtime/transaction-pool", + "test-utils/test-crate", "utils/browser", "utils/build-script-utils", "utils/fork-tree", diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml index 98fe48f2fa120..fea9a3588a66c 100644 --- a/test-utils/derive/Cargo.toml +++ b/test-utils/derive/Cargo.toml @@ -9,7 +9,8 @@ repository = "https://github.com/paritytech/substrate/" [dependencies] quote = "1.0.6" -syn = { version = "1.0.33", default-features = false } +syn = { version = "1.0.33", features = ["full"] } +proc-macro-crate = "0.1.4" [lib] proc-macro = true diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index af7d944fa0c10..3b6a29e9085f3 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -16,8 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use proc_macro::TokenStream; +use proc_macro::{Span, TokenStream}; +use proc_macro_crate::crate_name; use quote::quote; +use std::env; #[proc_macro_attribute] pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { @@ -53,9 +55,18 @@ fn parse_knobs( } }; + let crate_name = if env::var("CARGO_PKG_NAME").unwrap() == "substrate-test-utils" { + syn::Ident::new("substrate_test_utils", Span::call_site().into()) + } else { + let crate_name = crate_name("substrate-test-utils") + .map_err(|e| syn::Error::new_spanned(&sig, e))?; + + syn::Ident::new(&crate_name, Span::call_site().into()) + }; + let header = { quote! { - #[tokio::test(#(#args)*)] + #[#crate_name::tokio::test(#(#args)*)] } }; @@ -63,13 +74,13 @@ fn parse_knobs( #header #(#attrs)* #vis #sig { - use substrate_test_utils::futures::future::FutureExt; + use #crate_name::futures::future::FutureExt; let #task_executor_name: #task_executor_type = (|fut, _| { - substrate_test_utils::tokio::spawn(fut).map(drop) + #crate_name::tokio::spawn(fut).map(drop) }) .into(); - let timeout_task = substrate_test_utils::tokio::time::delay_for( + let timeout_task = #crate_name::tokio::time::delay_for( std::time::Duration::from_secs( std::env::var("SUBSTRATE_TEST_TIMEOUT") .ok() @@ -81,9 +92,9 @@ fn parse_knobs( } .fuse(); - substrate_test_utils::futures::pin_mut!(timeout_task, actual_test_task); + #crate_name::futures::pin_mut!(timeout_task, actual_test_task); - substrate_test_utils::futures::select! { + #crate_name::futures::select! { _ = timeout_task => { panic!("the test took too long"); }, diff --git a/test-utils/test-crate/Cargo.toml b/test-utils/test-crate/Cargo.toml new file mode 100644 index 0000000000000..a269e86522d46 --- /dev/null +++ b/test-utils/test-crate/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "substrate-test-utils-test-crate" +version = "0.1.0" +authors = ["Parity Technologies "] +edition = "2018" +license = "Apache-2.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dev-dependencies] +tokio = { version = "0.2.13", features = ["macros"] } +test-utils = { path = "..", package = "substrate-test-utils", features = ["macros"] } +sc-service = { path = "../../client/service" } diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs new file mode 100644 index 0000000000000..16c392e6cfabb --- /dev/null +++ b/test-utils/test-crate/src/main.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 2020 Parity Technologies (UK) Ltd. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use sc_service::{TaskExecutor, TaskType}; + +#[test_utils::test] +async fn basic_test(_: TaskExecutor) { + assert!(true); +} + +fn main() { + println!("Hello, world!"); +} diff --git a/test-utils/tests/basic.rs b/test-utils/tests/basic.rs index 3e96bfe83d3a7..f9b2409466508 100644 --- a/test-utils/tests/basic.rs +++ b/test-utils/tests/basic.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sc_service::{TaskExecutor, TaskType}; +use sc_service::TaskExecutor; #[substrate_test_utils::test] async fn basic_test(_: TaskExecutor) { From f0c0e7f41a95b6f7f690747db45d233a26d2fda9 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 6 Aug 2020 11:22:23 +0200 Subject: [PATCH 15/26] WIP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- test-utils/test-crate/src/main.rs | 2 +- test-utils/tests/basic.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs index 16c392e6cfabb..e493c7270e0e9 100644 --- a/test-utils/test-crate/src/main.rs +++ b/test-utils/test-crate/src/main.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sc_service::{TaskExecutor, TaskType}; +use sc_service::{TaskExecutor}; #[test_utils::test] async fn basic_test(_: TaskExecutor) { diff --git a/test-utils/tests/basic.rs b/test-utils/tests/basic.rs index f9b2409466508..3e96bfe83d3a7 100644 --- a/test-utils/tests/basic.rs +++ b/test-utils/tests/basic.rs @@ -16,7 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use sc_service::TaskExecutor; +use sc_service::{TaskExecutor, TaskType}; #[substrate_test_utils::test] async fn basic_test(_: TaskExecutor) { From 3a5d4f06057bfde03a1ae401746e83062d686d82 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 6 Aug 2020 11:24:46 +0200 Subject: [PATCH 16/26] WIP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- test-utils/test-crate/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs index e493c7270e0e9..2d97a2c463145 100644 --- a/test-utils/test-crate/src/main.rs +++ b/test-utils/test-crate/src/main.rs @@ -24,5 +24,4 @@ async fn basic_test(_: TaskExecutor) { } fn main() { - println!("Hello, world!"); } From 40d1804f95c7189e0faae79f0da14ed5252cdc21 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 6 Aug 2020 19:37:50 +0200 Subject: [PATCH 17/26] remove feature flag --- test-utils/Cargo.toml | 13 +++---------- test-utils/src/lib.rs | 3 --- test-utils/test-crate/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index ec80809f56e9b..420bddce57843 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -12,21 +12,14 @@ autotests = false targets = ["x86_64-unknown-linux-gnu"] [dependencies] -futures = { version = "0.3.1", features = ["compat"], optional = true } -substrate-test-utils-derive = { path = "./derive", optional = true } -tokio = { version = "0.2.13", features = ["macros"], optional = true } +futures = { version = "0.3.1", features = ["compat"] } +substrate-test-utils-derive = { path = "./derive" } +tokio = { version = "0.2.13", features = ["macros"] } [dev-dependencies] sc-service = { path = "../client/service" } trybuild = { version = "1.0", features = ["diff"] } -[features] -macros = [ - "futures", - "substrate-test-utils-derive", - "tokio", -] - [[test]] name = "basic" path = "tests/basic.rs" diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index ea379c9e0866d..b3d9cdb9e5f02 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -18,12 +18,9 @@ //! Test utils #[doc(hidden)] -#[cfg(feature = "macros")] pub use futures; -#[cfg(feature = "macros")] pub use substrate_test_utils_derive::test; #[doc(hidden)] -#[cfg(feature = "macros")] pub use tokio; /// Panic when the vectors are different, without taking the order into account. diff --git a/test-utils/test-crate/Cargo.toml b/test-utils/test-crate/Cargo.toml index a269e86522d46..6d16edde12c59 100644 --- a/test-utils/test-crate/Cargo.toml +++ b/test-utils/test-crate/Cargo.toml @@ -12,5 +12,5 @@ targets = ["x86_64-unknown-linux-gnu"] [dev-dependencies] tokio = { version = "0.2.13", features = ["macros"] } -test-utils = { path = "..", package = "substrate-test-utils", features = ["macros"] } +test-utils = { path = "..", package = "substrate-test-utils" } sc-service = { path = "../../client/service" } From a8c62ff64801d7b9903eed015e70ceec122b136e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 7 Aug 2020 11:02:10 +0200 Subject: [PATCH 18/26] fix missing dependency --- test-utils/test-crate/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs index 2d97a2c463145..4922dfe291a13 100644 --- a/test-utils/test-crate/src/main.rs +++ b/test-utils/test-crate/src/main.rs @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#[cfg(test)] use sc_service::{TaskExecutor}; #[test_utils::test] @@ -23,5 +24,4 @@ async fn basic_test(_: TaskExecutor) { assert!(true); } -fn main() { -} +fn main() {} From 82ceab00bb527efa3c7c51811ff5f17fbdc14937 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 7 Aug 2020 11:02:47 +0200 Subject: [PATCH 19/26] CLEANUP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- test-utils/test-crate/src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs index 4922dfe291a13..980eb36786d12 100644 --- a/test-utils/test-crate/src/main.rs +++ b/test-utils/test-crate/src/main.rs @@ -16,11 +16,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#[cfg(test)] -use sc_service::{TaskExecutor}; - #[test_utils::test] -async fn basic_test(_: TaskExecutor) { +async fn basic_test(_: sc_service::TaskExecutor) { assert!(true); } From badbec37d95542f7328bd98ffa0dcd2b709019cb Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 7 Aug 2020 11:20:23 +0200 Subject: [PATCH 20/26] fix test --- test-utils/test-crate/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test-utils/test-crate/src/main.rs b/test-utils/test-crate/src/main.rs index 980eb36786d12..209f29f76132d 100644 --- a/test-utils/test-crate/src/main.rs +++ b/test-utils/test-crate/src/main.rs @@ -16,6 +16,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#[cfg(test)] #[test_utils::test] async fn basic_test(_: sc_service::TaskExecutor) { assert!(true); From a9d03e1d880c6c26055c66855f29eabb9e3d5a1e Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 12 Aug 2020 11:56:52 +0200 Subject: [PATCH 21/26] Removed autotests=false --- test-utils/Cargo.toml | 9 --------- test-utils/tests/{errors.rs => ui.rs} | 4 ++-- test-utils/tests/{ => ui}/missing-func-parameter.rs | 0 test-utils/tests/{ => ui}/missing-func-parameter.stderr | 0 test-utils/tests/{ => ui}/too-many-func-parameters.rs | 0 .../tests/{ => ui}/too-many-func-parameters.stderr | 0 6 files changed, 2 insertions(+), 11 deletions(-) rename test-utils/tests/{errors.rs => ui.rs} (88%) rename test-utils/tests/{ => ui}/missing-func-parameter.rs (100%) rename test-utils/tests/{ => ui}/missing-func-parameter.stderr (100%) rename test-utils/tests/{ => ui}/too-many-func-parameters.rs (100%) rename test-utils/tests/{ => ui}/too-many-func-parameters.stderr (100%) diff --git a/test-utils/Cargo.toml b/test-utils/Cargo.toml index 420bddce57843..3b2a3702430e7 100644 --- a/test-utils/Cargo.toml +++ b/test-utils/Cargo.toml @@ -6,7 +6,6 @@ edition = "2018" license = "Apache-2.0" homepage = "https://substrate.dev" repository = "https://github.com/paritytech/substrate/" -autotests = false [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -19,11 +18,3 @@ tokio = { version = "0.2.13", features = ["macros"] } [dev-dependencies] sc-service = { path = "../client/service" } trybuild = { version = "1.0", features = ["diff"] } - -[[test]] -name = "basic" -path = "tests/basic.rs" - -[[test]] -name = "errors" -path = "tests/errors.rs" diff --git a/test-utils/tests/errors.rs b/test-utils/tests/ui.rs similarity index 88% rename from test-utils/tests/errors.rs rename to test-utils/tests/ui.rs index 60dce68500d26..1f3b466c7dd6e 100644 --- a/test-utils/tests/errors.rs +++ b/test-utils/tests/ui.rs @@ -19,6 +19,6 @@ #[test] fn substrate_test_utils_derive_trybuild() { let t = trybuild::TestCases::new(); - t.compile_fail("tests/missing-func-parameter.rs"); - t.compile_fail("tests/too-many-func-parameters.rs"); + t.compile_fail("tests/ui/missing-func-parameter.rs"); + t.compile_fail("tests/ui/too-many-func-parameters.rs"); } diff --git a/test-utils/tests/missing-func-parameter.rs b/test-utils/tests/ui/missing-func-parameter.rs similarity index 100% rename from test-utils/tests/missing-func-parameter.rs rename to test-utils/tests/ui/missing-func-parameter.rs diff --git a/test-utils/tests/missing-func-parameter.stderr b/test-utils/tests/ui/missing-func-parameter.stderr similarity index 100% rename from test-utils/tests/missing-func-parameter.stderr rename to test-utils/tests/ui/missing-func-parameter.stderr diff --git a/test-utils/tests/too-many-func-parameters.rs b/test-utils/tests/ui/too-many-func-parameters.rs similarity index 100% rename from test-utils/tests/too-many-func-parameters.rs rename to test-utils/tests/ui/too-many-func-parameters.rs diff --git a/test-utils/tests/too-many-func-parameters.stderr b/test-utils/tests/ui/too-many-func-parameters.stderr similarity index 100% rename from test-utils/tests/too-many-func-parameters.stderr rename to test-utils/tests/ui/too-many-func-parameters.stderr From c7c3e5276a68c3cc742f339e593c1b823444a366 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 12 Aug 2020 12:53:34 +0200 Subject: [PATCH 22/26] Some doc... --- test-utils/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index b3d9cdb9e5f02..685f70ca4842f 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -19,6 +19,21 @@ #[doc(hidden)] pub use futures; +/// Marks async function to be executed by an async runtime and provide a `TaskExecutor`, suitable +/// to test environment. +/// +/// # Example +/// +/// ``` +/// use tokio; // WARNING: you must have tokio in the dependency of your crate to use this macro! +/// +/// #[substrate_test_utils::test] +/// async fn basic_test(task_executor: TaskExecutor) { +/// assert!(true); +/// // create your node in here and use task_executor +/// // then don't forget to gracefully shutdown your node before exit +/// } +/// ``` pub use substrate_test_utils_derive::test; #[doc(hidden)] pub use tokio; From 78037b8989a815f5d8cb0e79429a516f8aa1c045 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 12 Aug 2020 13:05:47 +0200 Subject: [PATCH 23/26] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- test-utils/derive/Cargo.toml | 2 +- test-utils/derive/src/lib.rs | 2 +- test-utils/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test-utils/derive/Cargo.toml b/test-utils/derive/Cargo.toml index fea9a3588a66c..5ec3e10108c03 100644 --- a/test-utils/derive/Cargo.toml +++ b/test-utils/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "substrate-test-utils-derive" -version = "0.8.0-rc4" +version = "0.8.0-rc5" authors = ["Parity Technologies "] edition = "2018" license = "Apache-2.0" diff --git a/test-utils/derive/src/lib.rs b/test-utils/derive/src/lib.rs index 3b6a29e9085f3..f5d627068963f 100644 --- a/test-utils/derive/src/lib.rs +++ b/test-utils/derive/src/lib.rs @@ -96,7 +96,7 @@ fn parse_knobs( #crate_name::futures::select! { _ = timeout_task => { - panic!("the test took too long"); + panic!("The test took too long!"); }, _ = actual_test_task => {}, } diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 685f70ca4842f..6f1c55066b37a 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -22,7 +22,7 @@ pub use futures; /// Marks async function to be executed by an async runtime and provide a `TaskExecutor`, suitable /// to test environment. /// -/// # Example +/// # Example /// /// ``` /// use tokio; // WARNING: you must have tokio in the dependency of your crate to use this macro! From 0587d76b12dc9e09be3b8bab733ec8a0cac97341 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 12 Aug 2020 13:07:25 +0200 Subject: [PATCH 24/26] WIP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- Cargo.lock | 2 +- test-utils/src/lib.rs | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 305b1300ccef7..c894dc50bd53e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8624,7 +8624,7 @@ dependencies = [ [[package]] name = "substrate-test-utils-derive" -version = "0.8.0-rc4" +version = "0.8.0-rc5" dependencies = [ "proc-macro-crate", "quote 1.0.6", diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 6f1c55066b37a..ee18bb50207f3 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -22,11 +22,13 @@ pub use futures; /// Marks async function to be executed by an async runtime and provide a `TaskExecutor`, suitable /// to test environment. /// +/// # Requirements +/// +/// You must have tokio in the dependency of your crate to use this macro. +/// /// # Example /// /// ``` -/// use tokio; // WARNING: you must have tokio in the dependency of your crate to use this macro! -/// /// #[substrate_test_utils::test] /// async fn basic_test(task_executor: TaskExecutor) { /// assert!(true); From e4b9ff1fce215fa149d0f61ae5f687c4244b5848 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 12 Aug 2020 13:09:01 +0200 Subject: [PATCH 25/26] WIP Forked at: 60e3a693b29789045614e2ed73126695bc8b0794 Parent branch: origin/master --- test-utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index ee18bb50207f3..1ff376da18154 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -24,7 +24,7 @@ pub use futures; /// /// # Requirements /// -/// You must have tokio in the dependency of your crate to use this macro. +/// You must have tokio in the `[dev-dependencies]` of your crate to use this macro. /// /// # Example /// From 948c64ca6e81aad1edfc2fd322969fedcf535f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 12 Aug 2020 15:40:15 +0200 Subject: [PATCH 26/26] Update test-utils/src/lib.rs --- test-utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index 1ff376da18154..224eacd5129e3 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -28,7 +28,7 @@ pub use futures; /// /// # Example /// -/// ``` +/// ``` /// #[substrate_test_utils::test] /// async fn basic_test(task_executor: TaskExecutor) { /// assert!(true);