From 5b3e0ee856caa0f8e0b2f6295cb107c042bb7edd Mon Sep 17 00:00:00 2001 From: abhijeetbhagat Date: Mon, 10 Oct 2022 23:05:20 +0530 Subject: [PATCH 1/4] add list, run cli flags to integration tests. fixes: #338 --- test/integration-test/Cargo.toml | 3 +- test/integration-test/src/main.rs | 55 ++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/test/integration-test/Cargo.toml b/test/integration-test/Cargo.toml index bd6957f0..74b5801b 100644 --- a/test/integration-test/Cargo.toml +++ b/test/integration-test/Cargo.toml @@ -7,6 +7,8 @@ publish = false [dependencies] anyhow = "1" aya = { path = "../../aya" } +clap = { version = "3", features = ["derive"] } +env_logger = "0.9" inventory = "0.2" integration-test-macros = { path = "../integration-test-macros" } lazy_static = "1" @@ -14,4 +16,3 @@ libc = { version = "0.2.105" } log = "0.4" object = { version = "0.29", default-features = false, features = ["std", "read_core", "elf"] } regex = "1" -env_logger = "0.9" diff --git a/test/integration-test/src/main.rs b/test/integration-test/src/main.rs index a9f142b1..a61bf088 100644 --- a/test/integration-test/src/main.rs +++ b/test/integration-test/src/main.rs @@ -3,14 +3,59 @@ use log::info; mod tests; use tests::IntegrationTest; +use clap::Parser; +#[derive(Debug, Parser)] +pub struct RunOptions { + #[clap(short, long, value_parser)] + tests: Option>, +} + +#[derive(Debug, Parser)] +struct Options { + #[clap(subcommand)] + command: Command, +} + +#[derive(Debug, Parser)] +enum Command { + /// Run one or more tests: ... -- run -t test1 -t test2 + Run(RunOptions), + /// List all the tests: ... -- list + List +} + fn main() -> anyhow::Result<()> { env_logger::init(); - // Run the tests - for t in inventory::iter:: { - info!("Running {}", t.name); - if let Err(e) = (t.test_fn)() { - panic!("{}", e) + let cmd = Command::parse(); + + match cmd { + Command::Run(opts) => { + match opts.tests { + Some(tests) => { + for t in inventory::iter:: { + if tests.contains(&t.name.into()) { + info!("Running {}", t.name); + if let Err(e) = (t.test_fn)() { + panic!("{}", e) + } + } + } + } + None => { + for t in inventory::iter:: { + info!("Running {}", t.name); + if let Err(e) = (t.test_fn)() { + panic!("{}", e) + } + } + } + } + } + Command::List => { + for t in inventory::iter:: { + info!("{}", t.name); + } } } From 638cf514fb62d4b14f5d9756be95212bf3258cda Mon Sep 17 00:00:00 2001 From: abhijeetbhagat Date: Tue, 11 Oct 2022 13:32:41 +0530 Subject: [PATCH 2/4] use macro to log, execute test and handle error --- test/integration-test/src/main.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/test/integration-test/src/main.rs b/test/integration-test/src/main.rs index a61bf088..b2ad10be 100644 --- a/test/integration-test/src/main.rs +++ b/test/integration-test/src/main.rs @@ -24,6 +24,15 @@ enum Command { List } +macro_rules! exec_test { + ($test:expr) => {{ + info!("Running {}", $test.name); + if let Err(e) = ($test.test_fn)() { + panic!("{}", e) + } + }}; +} + fn main() -> anyhow::Result<()> { env_logger::init(); @@ -35,19 +44,13 @@ fn main() -> anyhow::Result<()> { Some(tests) => { for t in inventory::iter:: { if tests.contains(&t.name.into()) { - info!("Running {}", t.name); - if let Err(e) = (t.test_fn)() { - panic!("{}", e) - } + exec_test!(t) } } } None => { for t in inventory::iter:: { - info!("Running {}", t.name); - if let Err(e) = (t.test_fn)() { - panic!("{}", e) - } + exec_test!(t) } } } From 749966167092ec62bc7c9cd71d67c73b1a330649 Mon Sep 17 00:00:00 2001 From: abhijeetbhagat Date: Tue, 11 Oct 2022 16:43:29 +0530 Subject: [PATCH 3/4] fix formatting --- test/integration-test/src/main.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/test/integration-test/src/main.rs b/test/integration-test/src/main.rs index b2ad10be..5a135d3d 100644 --- a/test/integration-test/src/main.rs +++ b/test/integration-test/src/main.rs @@ -21,7 +21,7 @@ enum Command { /// Run one or more tests: ... -- run -t test1 -t test2 Run(RunOptions), /// List all the tests: ... -- list - List + List, } macro_rules! exec_test { @@ -39,22 +39,20 @@ fn main() -> anyhow::Result<()> { let cmd = Command::parse(); match cmd { - Command::Run(opts) => { - match opts.tests { - Some(tests) => { - for t in inventory::iter:: { - if tests.contains(&t.name.into()) { - exec_test!(t) - } - } - } - None => { - for t in inventory::iter:: { + Command::Run(opts) => match opts.tests { + Some(tests) => { + for t in inventory::iter:: { + if tests.contains(&t.name.into()) { exec_test!(t) } } } - } + None => { + for t in inventory::iter:: { + exec_test!(t) + } + } + }, Command::List => { for t in inventory::iter:: { info!("{}", t.name); From c83d012c514a6a02572c43a6ff3659ca88259d37 Mon Sep 17 00:00:00 2001 From: abhijeetbhagat Date: Tue, 11 Oct 2022 22:42:58 +0530 Subject: [PATCH 4/4] preserve existing behavior and avoid changes to gh workflows/xtask --- test/integration-test/src/main.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/test/integration-test/src/main.rs b/test/integration-test/src/main.rs index 5a135d3d..60744da5 100644 --- a/test/integration-test/src/main.rs +++ b/test/integration-test/src/main.rs @@ -4,16 +4,19 @@ mod tests; use tests::IntegrationTest; use clap::Parser; + #[derive(Debug, Parser)] +#[clap(author, version, about, long_about = None)] +#[clap(propagate_version = true)] pub struct RunOptions { #[clap(short, long, value_parser)] tests: Option>, } #[derive(Debug, Parser)] -struct Options { +struct Cli { #[clap(subcommand)] - command: Command, + command: Option, } #[derive(Debug, Parser)] @@ -33,13 +36,21 @@ macro_rules! exec_test { }}; } +macro_rules! exec_all_tests { + () => {{ + for t in inventory::iter:: { + exec_test!(t) + } + }}; +} + fn main() -> anyhow::Result<()> { env_logger::init(); - let cmd = Command::parse(); + let cli = Cli::parse(); - match cmd { - Command::Run(opts) => match opts.tests { + match &cli.command { + Some(Command::Run(opts)) => match &opts.tests { Some(tests) => { for t in inventory::iter:: { if tests.contains(&t.name.into()) { @@ -48,16 +59,17 @@ fn main() -> anyhow::Result<()> { } } None => { - for t in inventory::iter:: { - exec_test!(t) - } + exec_all_tests!() } }, - Command::List => { + Some(Command::List) => { for t in inventory::iter:: { info!("{}", t.name); } } + None => { + exec_all_tests!() + } } Ok(())