From b67c534ac43e90fca27872ee6b39a61416d52f69 Mon Sep 17 00:00:00 2001 From: Sergio Machado Date: Fri, 13 Jun 2025 19:39:52 -0400 Subject: [PATCH] Set tags filter via environment variable --- Cargo.toml | 2 +- book/src/cli.md | 2 ++ book/src/writing/tags.md | 2 +- src/cli.rs | 1 + tests/cli.rs | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c390606b..98fec84b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,7 +64,7 @@ timestamps = [] tracing = ["dep:crossbeam-utils", "dep:tracing", "dep:tracing-subscriber"] [dependencies] -clap = { version = "4.3.2", features = ["derive", "wrap_help"] } +clap = { version = "4.3.2", features = ["derive", "wrap_help", "env"] } console = "0.15" derive_more = { version = "2.0", features = ["as_ref", "debug", "deref", "deref_mut", "display", "error", "from", "from_str", "into"] } either = "1.6" diff --git a/book/src/cli.md b/book/src/cli.md index cd949a88..9aa74875 100644 --- a/book/src/cli.md +++ b/book/src/cli.md @@ -24,6 +24,8 @@ Options: Tag expression to filter scenarios by. Note: Tags from Feature, Rule and Scenario are merged together on filtering, so be careful about conflicting tags on different levels. + + [env: CUCUMBER_FILTER_TAGS=@scenario-1] -i, --input Glob pattern to look for feature files with. If not specified, looks for `*.feature` files in the path configured in the test runner diff --git a/book/src/writing/tags.md b/book/src/writing/tags.md index 662f9484..1eaa07bb 100644 --- a/book/src/writing/tags.md +++ b/book/src/writing/tags.md @@ -31,7 +31,7 @@ Feature: Animal feature ``` To filter out running [scenario]s we may use: -- either `--tags` [CLI] option providing [tag expressions] (also consider [escaping]); +- either `--tags` [CLI] option providing [tag expressions] (also consider [escaping]). The expression can alternatively also be set using the `CUCUMBER_FILTER_TAGS` environment variable. - or [`filter_run()`]-like method. ![record](../rec/writing_tags_filtering.gif) diff --git a/src/cli.rs b/src/cli.rs index a7c49db9..3a5423b7 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -109,6 +109,7 @@ where id = "tags", long = "tags", short = 't', + env = "CUCUMBER_FILTER_TAGS", value_name = "tagexpr", conflicts_with = "name", global = true diff --git a/tests/cli.rs b/tests/cli.rs index 0bf10eb5..db38232e 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,4 +1,4 @@ -use std::panic::AssertUnwindSafe; +use std::{env, panic::AssertUnwindSafe}; use clap::Parser; use cucumber::{World as _, cli, given}; @@ -92,3 +92,35 @@ async fn tags_option_filters_scenario1_no_subcommand() { assert_eq!(err, "1 step failed"); } + +// This test verifies that the global option `--tags` is still available without +// subcommands and that the error output contains 1 failing step. +// (env variable variant) +#[test] +fn tags_option_filters_scenario1_no_subcommand_env() { + unsafe { + env::set_var("CUCUMBER_FILTER_TAGS", "@scenario-1"); + } + + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(async { + let cli = + cli::Opts::<_, _, _, CustomCli>::try_parse_from(&["test"]) + .expect("Invalid command line"); + + let res = World::cucumber() + .with_cli(cli) + .run_and_exit("tests/features/cli"); + + let err = AssertUnwindSafe(res) + .catch_unwind() + .await + .expect_err("should err"); + let err = err.downcast_ref::().unwrap(); + + assert_eq!(err, "1 step failed"); + }) +}