From aa52368cf26aa65aed4f39820c088e2d3d99f0d5 Mon Sep 17 00:00:00 2001 From: Liam Bigelow <40188355+bglw@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:23:38 +1200 Subject: [PATCH] Add arguments to the CLI to run a specific test --- CHANGELOG.md | 2 ++ test_ci.sh | 8 +++++ toolproof/src/main.rs | 9 +++++- toolproof/src/options.rs | 14 +++++++++ .../test_suite/base/run_one.toolproof.yml | 29 +++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100755 test_ci.sh create mode 100644 toolproof/test_suite/base/run_one.toolproof.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index 52ab771..fd54a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ ## Unreleased +* Adds `-n ` / `--name ` arguments to the CLI to run a specific test + ## v0.3.0 (August 16, 2024) * Adds platform flags for tests and individual steps diff --git a/test_ci.sh b/test_ci.sh new file mode 100755 index 0000000..f5fa6a4 --- /dev/null +++ b/test_ci.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +cd $SCRIPT_DIR + +TOOLPROOF=$(realpath "$SCRIPT_DIR/target/$1/toolproof") + +cargo run --release -- --placeholders toolproof_path="$TOOLPROOF" -c 1 diff --git a/toolproof/src/main.rs b/toolproof/src/main.rs index fdc83e2..5be0d1e 100644 --- a/toolproof/src/main.rs +++ b/toolproof/src/main.rs @@ -250,7 +250,14 @@ async fn main_inner() -> Result<(), ()> { ctx, }); - let run_mode = if universe.ctx.params.interactive && !universe.ctx.params.all { + let run_mode = if let Some(run_name) = universe.ctx.params.run_name.as_ref() { + let Some((path, _)) = universe.tests.iter().find(|(_, t)| t.name == *run_name) else { + eprintln!("Test name {run_name} does not exist"); + return Err(()); + }; + + RunMode::One(path.clone()) + } else if universe.ctx.params.interactive && !universe.ctx.params.all { match get_run_mode(&universe) { Ok(mode) => mode, Err(e) => { diff --git a/toolproof/src/options.rs b/toolproof/src/options.rs index 5c864d1..90e8550 100644 --- a/toolproof/src/options.rs +++ b/toolproof/src/options.rs @@ -106,6 +106,12 @@ fn get_cli_matches() -> ArgMatches { ) .action(clap::ArgAction::SetTrue), ) + .arg( + arg!( + -n --name "Exact name of a test to run") + .long_help("case-sensitive") + .required(false) + ) .arg( arg!( --browser ... "Specify which browser to use when running browser automation tests" @@ -145,6 +151,10 @@ pub struct ToolproofParams { /// Run all tests when in interactive mode pub all: bool, + /// Run a specific test + #[setting(env = "TOOLPROOF_RUN_NAME")] + pub run_name: Option, + /// Specify which browser to use when running browser automation tests #[setting(env = "TOOLPROOF_BROWSER")] pub browser: ToolproofBrowserImpl, @@ -205,6 +215,10 @@ impl ToolproofParams { self.all = true; } + if let Some(name) = cli_matches.get_one::("name") { + self.run_name = Some(name.clone()); + } + if let Some(root) = cli_matches.get_one::("root") { self.root = Some(root.clone()); } diff --git a/toolproof/test_suite/base/run_one.toolproof.yml b/toolproof/test_suite/base/run_one.toolproof.yml new file mode 100644 index 0000000..8b5447f --- /dev/null +++ b/toolproof/test_suite/base/run_one.toolproof.yml @@ -0,0 +1,29 @@ +name: Run a single toolproof test + +steps: + - step: I have a "my_test_a.toolproof.yml" file with the content {yaml} + yaml: |- + name: Inner test A + + steps: + - I run 'echo "toolproof 🙂"' + - stdout should contain "🙂" + - step: I have a "my_test_b.toolproof.yml" file with the content {yaml} + yaml: |- + name: Inner test B + + steps: + - I run 'echo "toolproof 🙁"' + - stdout should contain "🙂" + - I run "%toolproof_path% -n 'Inner test A'" + - step: "stdout should contain 'Passing tests: 1'" + - step: "stdout should contain 'Failing tests: 0'" + - step: "stdout should contain 'Skipped tests: 0'" + - step: "stdout should contain 'All tests passed'" + - stderr should be empty + - I run "%toolproof_path% -n 'Inner test B'" and expect it to fail + - step: "stdout should contain 'Passing tests: 0'" + - step: "stdout should contain 'Failing tests: 1'" + - step: "stdout should contain 'Skipped tests: 0'" + - step: "stdout should contain 'Some tests failed'" + - stderr should be empty