Skip to content

Commit

Permalink
Tests filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
kpob committed Jun 28, 2024
1 parent 0f63c68 commit c8fd6ac
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 27 deletions.
62 changes: 42 additions & 20 deletions src/actions/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Module responsible for running contracts tests.
use super::build::BuildAction;
use crate::{command, log, project::Project};

Expand All @@ -9,7 +8,7 @@ pub struct TestAction<'a> {
backend: Option<String>,
passthrough_args: Vec<String>,
skip_build: bool,
test: Option<String>,
filters: TestFilters,
}

/// TestAction implementation.
Expand All @@ -18,16 +17,18 @@ impl<'a> TestAction<'a> {
pub fn new(
project: &Project,
backend: Option<String>,
test: Option<String>,
passthrough_args: Vec<String>,
skip_build: bool,
tests: Vec<String>,
filter: Option<String>,
) -> TestAction {
let filters = TestFilters::new(tests, filter);
TestAction {
backend,
passthrough_args,
skip_build,
project,
test,
filters,
}
}
}
Expand All @@ -48,7 +49,12 @@ 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.args());

command::cargo_test_odra_vm(
self.project.project_root(),
&self.filters,
self.get_passthrough_args(),
);
}

/// Test specific backend.
Expand All @@ -57,7 +63,8 @@ impl TestAction<'_> {
command::cargo_test_backend(
self.project.project_root(),
self.backend_name(),
self.args(),
&self.filters,
self.get_passthrough_args(),
);
}

Expand All @@ -71,23 +78,38 @@ 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();
log::info("Building finished.")
}
}

pub struct TestFilters {
targets: Vec<String>,
filter: Option<String>,
}

impl TestFilters {
fn new(targets: Vec<String>, filter: Option<String>) -> Self {
Self { targets, filter }
}

pub fn as_args(&self) -> Vec<&str> {
let mut args = match &self.targets.len() {
0 => vec!["--tests"],
_ => self
.targets
.iter()
.map(|t| vec!["--test", t.as_str()])
.flatten()
.collect(),
};

if let Some(filter) = &self.filter {
args.push(filter.as_str());
}

args
}
}
10 changes: 7 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ 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.
/// Test only the specified test target.
#[clap(value_parser, long, short)]
pub test: Option<String>,
pub test: Vec<String>,
/// Run only tests containing the given name.
#[clap(value_parser)]
pub filter: Option<String>,
}

#[derive(clap::Args, Debug)]
Expand Down Expand Up @@ -176,9 +179,10 @@ pub fn make_action() {
TestAction::new(
&project,
test.backend,
test.test,
test.args,
test.skip_build,
test.test,
test.filter,
)
.test();
}
Expand Down
18 changes: 14 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use clap::Parser;
use Error::InvalidInternalCommand;

use crate::{
actions::test::TestFilters,
cli::Cargo,
consts::{ODRA_BACKEND_ENV_KEY, ODRA_MODULE_ENV_KEY},
errors::Error,
Expand Down Expand Up @@ -150,18 +151,27 @@ pub fn cargo_generate_schema_files(current_dir: PathBuf, contract_name: &str, mo
}

/// Runs cargo test.
pub fn cargo_test_odra_vm(current_dir: PathBuf, mut args: Vec<&str>) {
pub fn cargo_test_odra_vm<'a>(
current_dir: PathBuf,
filters: &'a TestFilters,
mut args: Vec<&'a str>,
) {
log::info("Running cargo test...");
let mut tail_args = vec![];
let mut tail_args = filters.as_args();
tail_args.append(&mut args);
cargo(current_dir, "test", tail_args);
}

/// Runs cargo test with backend features.
pub fn cargo_test_backend(project_root: PathBuf, backend_name: &str, mut args: Vec<&str>) {
pub fn cargo_test_backend<'a>(
project_root: PathBuf,
backend_name: &str,
filters: &'a TestFilters,
mut args: Vec<&'a str>,
) {
env::set_var(ODRA_BACKEND_ENV_KEY, backend_name);
log::info("Running cargo test...");
let mut tail_args = vec![];
let mut tail_args = filters.as_args();
tail_args.append(&mut args);
cargo(project_root, "test", tail_args)
}
Expand Down

0 comments on commit c8fd6ac

Please sign in to comment.