diff --git a/crates/cli/src/cli.rs b/crates/cli/src/cli.rs index 54aa53d..2186fd3 100644 --- a/crates/cli/src/cli.rs +++ b/crates/cli/src/cli.rs @@ -37,14 +37,13 @@ impl Cli { Ok(()) } - Command::Test { - tests, - additional_flags, - } => { + Command::Test { name, additional_flags } => { let config_path = Config::get_default_path()?; let loaded_config = Config::load(config_path)?; - Ok(Test::run(loaded_config.test, tests, additional_flags)?) + let filter = name.clone().unwrap_or_default(); + + Ok(Test::run(loaded_config.test, filter, additional_flags)?) } Command::Regtest => { let config_path = Config::get_default_path()?; diff --git a/crates/cli/src/commands/core.rs b/crates/cli/src/commands/core.rs index 5a08505..c5a667e 100644 --- a/crates/cli/src/commands/core.rs +++ b/crates/cli/src/commands/core.rs @@ -9,13 +9,14 @@ pub enum Command { }, /// Prints current Simplex config in use Config, - /// Spins up the local Electrs + Elements regtest + /// Spins up local Electrs + Elements regtest Regtest, /// Runs Simplex tests Test { - /// The list of test names to run - #[arg(long)] - tests: Vec, + /// Name or a substring of the tests to run + #[arg()] + name: Option, + #[command(flatten)] additional_flags: TestFlags, }, diff --git a/crates/cli/src/commands/test.rs b/crates/cli/src/commands/test.rs index 41c6cc5..2eeaa53 100644 --- a/crates/cli/src/commands/test.rs +++ b/crates/cli/src/commands/test.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use std::process::Stdio; -use smplx_test::TestConfig; +use smplx_test::{SMPLX_TEST_MARKER, TestConfig}; use super::core::TestFlags; use super::error::CommandError; @@ -9,11 +9,11 @@ use super::error::CommandError; pub struct Test {} impl Test { - pub fn run(config: TestConfig, tests: &[String], flags: &TestFlags) -> Result<(), CommandError> { + pub fn run(config: TestConfig, filter: String, flags: &TestFlags) -> Result<(), CommandError> { let cache_path = Self::get_test_config_cache_name()?; config.to_file(&cache_path)?; - let mut cargo_test_command = Self::build_cargo_test_command(&cache_path, tests, flags); + let mut cargo_test_command = Self::build_cargo_test_command(&cache_path, filter, flags); let output = cargo_test_command.output()?; @@ -33,10 +33,10 @@ impl Test { Ok(()) } - fn build_cargo_test_command(cache_path: &PathBuf, tests: &[String], flags: &TestFlags) -> std::process::Command { + fn build_cargo_test_command(cache_path: &PathBuf, filter: String, flags: &TestFlags) -> std::process::Command { let mut cargo_test_command = std::process::Command::new("sh"); - cargo_test_command.args(["-c".to_string(), Self::build_test_command(tests, flags)]); + cargo_test_command.args(["-c".to_string(), Self::build_test_command(filter, flags)]); cargo_test_command .env(smplx_test::TEST_ENV_NAME, cache_path) @@ -47,20 +47,10 @@ impl Test { cargo_test_command } - fn build_test_command(tests: &[String], flags: &TestFlags) -> String { + fn build_test_command(filter: String, flags: &TestFlags) -> String { let mut command_as_arg = String::new(); - if tests.is_empty() { - command_as_arg.push_str("cargo test --tests"); - } else { - let mut arg = "cargo test".to_string(); - - for test_name in tests { - arg.push_str(&format!(" --test {test_name}")); - } - - command_as_arg.push_str(&arg); - } + command_as_arg.push_str(&format!("cargo test {filter}_{SMPLX_TEST_MARKER}")); let flag_args = Self::build_test_flags(flags); diff --git a/crates/test/src/lib.rs b/crates/test/src/lib.rs index 055408e..d071b51 100644 --- a/crates/test/src/lib.rs +++ b/crates/test/src/lib.rs @@ -4,3 +4,4 @@ pub mod error; pub mod macros; pub use config::{RpcConfig, TEST_ENV_NAME, TestConfig}; +pub use macros::core::SMPLX_TEST_MARKER; diff --git a/crates/test/src/macros/core.rs b/crates/test/src/macros/core.rs index 3b31fd8..81c56d3 100644 --- a/crates/test/src/macros/core.rs +++ b/crates/test/src/macros/core.rs @@ -3,6 +3,8 @@ use syn::parse::Parser; use crate::TEST_ENV_NAME; +pub const SMPLX_TEST_MARKER: &str = "_smplx_test"; + type AttributeArgs = syn::punctuated::Punctuated; pub fn expand(args: TokenStream, input: syn::ItemFn) -> syn::Result { @@ -15,7 +17,7 @@ pub fn expand(args: TokenStream, input: syn::ItemFn) -> syn::Result // TODO: args? fn expand_inner(input: &syn::ItemFn, _args: AttributeArgs) -> syn::Result { let ret = &input.sig.output; - let name = &input.sig.ident; + let name = quote::format_ident!("{}_{}", &input.sig.ident.to_string(), SMPLX_TEST_MARKER); let inputs = &input.sig.inputs; let body = &input.block; let attrs = &input.attrs; diff --git a/examples/basic/tests/ignore_default_tests.rs b/examples/basic/tests/ignore_default_tests.rs new file mode 100644 index 0000000..cea22d8 --- /dev/null +++ b/examples/basic/tests/ignore_default_tests.rs @@ -0,0 +1,15 @@ +// Dummy test to insure that `simplex test` runs only `#[simplex::test]` functions + +#[cfg(test)] +mod test { + + #[simplex::test] + fn smplx_test_invoked(_: simplex::TestContext) -> anyhow::Result<()> { + Ok(()) + } + + #[test] + fn default_tests_should_not_be_invoked() { + panic!() + } +}