-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By declaring CLI options global we allow their usage in subcommands for the custom CLI.
- Loading branch information
1 parent
cf055ac
commit 9e7f242
Showing
7 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
use std::{convert::Infallible, panic::AssertUnwindSafe}; | ||
|
||
use async_trait::async_trait; | ||
use clap::Parser; | ||
use cucumber::{ | ||
cli::{self, Args}, | ||
given, WorldInit, | ||
}; | ||
use futures::FutureExt as _; | ||
|
||
#[derive(Args)] | ||
struct CustomCli { | ||
#[clap(subcommand)] | ||
command: Option<SubCommand>, | ||
} | ||
|
||
#[derive(clap::Subcommand)] | ||
pub enum SubCommand { | ||
Smoke(Smoke), | ||
} | ||
|
||
#[derive(Args)] | ||
pub struct Smoke { | ||
#[clap(long)] | ||
report_name: String, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, WorldInit)] | ||
struct World; | ||
|
||
#[async_trait(?Send)] | ||
impl cucumber::World for World { | ||
type Error = Infallible; | ||
|
||
async fn new() -> Result<Self, Self::Error> { | ||
Ok(World) | ||
} | ||
} | ||
|
||
#[given("an invalid step")] | ||
fn invalid_step(_world: &mut World) { | ||
assert!(false); | ||
} | ||
|
||
#[tokio::test] | ||
// This test uses a subcommand with the global option --tags to filter | ||
// on two failing tests and verifies that the error output contains | ||
// 2 failing steps. | ||
async fn tags_option_filter_all_with_subcommand() { | ||
let cli = cli::Opts::<_, _, _, CustomCli>::try_parse_from(&[ | ||
"test", | ||
"smoke", | ||
r#"--report-name="smoke.report""#, | ||
"--tags=@all", | ||
]) | ||
.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, "2 steps failed"); | ||
} | ||
|
||
#[tokio::test] | ||
// This test uses a subcommand with the global option --tags to filter | ||
// on one failing test and verifies that the error output contains | ||
// 1 failing step. | ||
async fn tags_option_filter_scenario1_with_subcommand() { | ||
let cli = cli::Opts::<_, _, _, CustomCli>::try_parse_from(&[ | ||
"test", | ||
"smoke", | ||
r#"--report-name="smoke.report""#, | ||
"--tags=@scenario-1", | ||
]) | ||
.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"); | ||
} | ||
|
||
#[tokio::test] | ||
// This test verifies that the global option --tags is still available | ||
// without subcommands and that the error output contains 1 failing step. | ||
async fn tags_option_filter_scenario1_without_subcommand() { | ||
let cli = cli::Opts::<_, _, _, CustomCli>::try_parse_from(&[ | ||
"test", | ||
"--tags=@scenario-1", | ||
]) | ||
.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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Feature: Test global option --tags with subcommands | ||
|
||
@scenario-1 @all | ||
Scenario: A scenario with two invalid steps | ||
Given an invalid step | ||
And an invalid step | ||
|
||
@scenario-2 @all | ||
Scenario: A scenario with one invalid step | ||
Given an invalid step | ||
|