From 13e66805e20a8548c7366574838229470174e7c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20Pobiar=C5=BCyn?= Date: Tue, 25 Jun 2024 09:35:23 +0200 Subject: [PATCH] Add `--test` to `cargo odra test` #48 --- src/actions/init.rs | 3 ++- src/actions/test.rs | 21 +++++++++++++++++++-- src/cli.rs | 12 +++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/actions/init.rs b/src/actions/init.rs index 36d86ac..557db15 100644 --- a/src/actions/init.rs +++ b/src/actions/init.rs @@ -11,7 +11,8 @@ use crate::{ command::{rename_file, replace_in_file}, consts::{ODRA_TEMPLATE_GH_RAW_REPO, ODRA_TEMPLATE_GH_REPO}, errors::Error, - log, paths, + log, + paths, project::OdraLocation, template::TemplateGenerator, utils::odra_latest_version, diff --git a/src/actions/test.rs b/src/actions/test.rs index d6620aa..7872c8a 100644 --- a/src/actions/test.rs +++ b/src/actions/test.rs @@ -9,6 +9,7 @@ pub struct TestAction<'a> { backend: Option, passthrough_args: Vec, skip_build: bool, + test: Option, } /// TestAction implementation. @@ -17,6 +18,7 @@ impl<'a> TestAction<'a> { pub fn new( project: &Project, backend: Option, + test: Option, passthrough_args: Vec, skip_build: bool, ) -> TestAction { @@ -25,6 +27,7 @@ impl<'a> TestAction<'a> { passthrough_args, skip_build, project, + test, } } } @@ -45,7 +48,7 @@ impl TestAction<'_> { /// Test code against OdraVM. fn test_odra_vm(&self) { log::info("Testing against OdraVM ..."); - command::cargo_test_odra_vm(self.project.project_root(), self.get_passthrough_args()); + command::cargo_test_odra_vm(self.project.project_root(), self.args()); } /// Test specific backend. @@ -54,7 +57,7 @@ impl TestAction<'_> { command::cargo_test_backend( self.project.project_root(), self.backend_name(), - self.get_passthrough_args(), + self.args(), ); } @@ -68,6 +71,20 @@ impl TestAction<'_> { self.passthrough_args.iter().map(AsRef::as_ref).collect() } + /// Returns arguments to be passed to `cargo test` command. + /// + /// This includes the test name and passthrough arguments. + fn args(&self) -> Vec<&str> { + [ + self.test + .as_ref() + .map(|t| vec![t.as_str()]) + .unwrap_or_default(), + self.get_passthrough_args(), + ] + .concat() + } + /// Build *.wasm files before testing. fn build_wasm_files(&self) { BuildAction::new(self.project, None).build(); diff --git a/src/cli.rs b/src/cli.rs index 2a2c731..1af8051 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -118,6 +118,9 @@ pub struct TestCommand { /// Skip building wasm files. #[clap(value_parser, long, short, default_value = "false")] pub skip_build: bool, + /// Run only tests containing the given name. + #[clap(value_parser, long, short)] + pub test: Option, } #[derive(clap::Args, Debug)] @@ -170,7 +173,14 @@ pub fn make_action() { } OdraSubcommand::Test(test) => { let project = Project::detect(current_dir); - TestAction::new(&project, test.backend, test.args, test.skip_build).test(); + TestAction::new( + &project, + test.backend, + test.test, + test.args, + test.skip_build, + ) + .test(); } OdraSubcommand::Generate(generate) => { let project = Project::detect(current_dir);