Skip to content

Set tags filter via environment variable #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions book/src/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Glob pattern to look for feature files with. If not specified, looks for `*.feature` files in the path configured in the test runner
Expand Down
2 changes: 1 addition & 1 deletion book/src/writing/tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ where
id = "tags",
long = "tags",
short = 't',
env = "CUCUMBER_FILTER_TAGS",
value_name = "tagexpr",
conflicts_with = "name",
global = true
Expand Down
34 changes: 33 additions & 1 deletion tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::panic::AssertUnwindSafe;
use std::{env, panic::AssertUnwindSafe};

use clap::Parser;
use cucumber::{World as _, cli, given};
Expand Down Expand Up @@ -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::<String>().unwrap();

assert_eq!(err, "1 step failed");
})
}