Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions crates/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
9 changes: 5 additions & 4 deletions crates/cli/src/commands/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Name or a substring of the tests to run
#[arg()]
name: Option<String>,

#[command(flatten)]
additional_flags: TestFlags,
},
Expand Down
24 changes: 7 additions & 17 deletions crates/cli/src/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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;

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()?;

Expand All @@ -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)
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions crates/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion crates/test/src/macros/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<syn::Meta, syn::Token![,]>;

pub fn expand(args: TokenStream, input: syn::ItemFn) -> syn::Result<TokenStream> {
Expand All @@ -15,7 +17,7 @@ pub fn expand(args: TokenStream, input: syn::ItemFn) -> syn::Result<TokenStream>
// TODO: args?
fn expand_inner(input: &syn::ItemFn, _args: AttributeArgs) -> syn::Result<proc_macro2::TokenStream> {
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;
Expand Down
15 changes: 15 additions & 0 deletions examples/basic/tests/ignore_default_tests.rs
Original file line number Diff line number Diff line change
@@ -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!()
}
}