-
Notifications
You must be signed in to change notification settings - Fork 71
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
CLI redesign (#58, #134, #140) #144
Changes from 20 commits
f031fc2
a4a8f62
f0220bb
696de67
7b24407
d9c2589
ba2eccf
0b58059
d7bb724
c48d942
4119545
f0c65db
7c47f3b
90cab88
2e83a94
d104147
75f542a
cf8e669
d345c4c
c2d254c
04f2c6b
e0b85c0
df487e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,31 +8,200 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! CLI options. | ||
//! Tools for composing CLI options. | ||
//! | ||
//! Main part of this module is [`Opts`], which composes all strongly-typed | ||
//! `CLI` options from [`Parser`], [`Runner`] and [`Writer`] and adds filtering | ||
//! based on [`Regex`] or [`Tag Expressions`][1]. | ||
//! | ||
//! [1]: https://cucumber.io/docs/cucumber/api/#tag-expressions | ||
//! [`Parser`]: crate::Parser | ||
//! [`Runner`]: crate::Runner | ||
//! [`Writer`]: crate::Writer | ||
|
||
use gherkin::tagexpr::TagOperation; | ||
use regex::Regex; | ||
use structopt::StructOpt; | ||
|
||
/// Run the tests, pet a dog!. | ||
/// | ||
/// __WARNING__ ⚠️: This CLI exists only for backwards compatibility. In `0.11` | ||
/// it will be completely reworked: | ||
/// [cucumber-rs/cucumber#134][1]. | ||
/// | ||
/// [1]: https://github.com/cucumber-rs/cucumber/issues/134 | ||
#[derive(clap::Parser, Debug)] | ||
pub struct Opts { | ||
/// Regex to select scenarios from. | ||
#[clap(short = 'e', long = "expression", name = "regex")] | ||
pub filter: Option<Regex>, | ||
|
||
/// __WARNING__ ⚠️: This option does nothing at the moment and is deprecated | ||
/// for removal in the next major release. | ||
/// Any output of step functions is not captured by default. | ||
#[clap(long)] | ||
pub nocapture: bool, | ||
|
||
/// __WARNING__ ⚠️: This option does nothing at the moment and is deprecated | ||
/// for removal in the next major release. | ||
#[clap(long)] | ||
pub debug: bool, | ||
#[derive(Debug, StructOpt)] | ||
pub struct Opts<Parser, Runner, Writer, Custom = Empty> | ||
where | ||
Custom: StructOpt, | ||
Parser: StructOpt, | ||
Runner: StructOpt, | ||
Writer: StructOpt, | ||
{ | ||
/// Regex to filter scenarios with. | ||
#[structopt( | ||
short = "n", | ||
long = "name", | ||
name = "regex", | ||
visible_alias = "scenario-name" | ||
)] | ||
pub re_filter: Option<Regex>, | ||
|
||
/// Tag expression to filter scenarios with. | ||
#[structopt( | ||
short = "t", | ||
long = "tags", | ||
name = "tagexpr", | ||
visible_alias = "scenario-tags", | ||
conflicts_with = "regex" | ||
)] | ||
pub tags_filter: Option<TagOperation>, | ||
|
||
/// [`Parser`] CLI options. | ||
/// | ||
/// [`Parser`]: crate::Parser | ||
#[structopt(flatten)] | ||
pub parser: Parser, | ||
|
||
/// [`Runner`] CLI options. | ||
/// | ||
/// [`Runner`]: crate::Runner | ||
#[structopt(flatten)] | ||
pub runner: Runner, | ||
|
||
/// [`Writer`] CLI options. | ||
/// | ||
/// [`Writer`]: crate::Writer | ||
#[structopt(flatten)] | ||
pub writer: Writer, | ||
|
||
/// Custom CLI options. | ||
#[structopt(flatten)] | ||
pub custom: Custom, | ||
} | ||
|
||
// Workaround for overwritten doc-comments. | ||
// https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332 | ||
#[cfg_attr( | ||
not(doc), | ||
allow(missing_docs, clippy::missing_docs_in_private_items) | ||
)] | ||
#[cfg_attr(doc, doc = "Empty CLI options.")] | ||
#[derive(Clone, Copy, Debug, StructOpt)] | ||
pub struct Empty { | ||
/// This field exists only because [`StructOpt`] derive macro doesn't | ||
/// support unit structs. | ||
#[structopt(skip)] | ||
skipped: (), | ||
} | ||
|
||
// Workaround for overwritten doc-comments. | ||
// https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332 | ||
#[cfg_attr( | ||
not(doc), | ||
allow(missing_docs, clippy::missing_docs_in_private_items) | ||
)] | ||
#[cfg_attr( | ||
doc, | ||
doc = r#" | ||
Composes two [`StructOpt`] derivers together. | ||
|
||
# Example | ||
|
||
This struct is especially useful, when implementing custom [`Writer`], which | ||
wraps another [`Writer`]. | ||
|
||
```rust | ||
# use async_trait::async_trait; | ||
# use cucumber::{ | ||
# cli, event, parser, ArbitraryWriter, FailureWriter, World, Writer, | ||
# }; | ||
# use structopt::StructOpt; | ||
# | ||
struct CustomWriter<Wr>(Wr); | ||
|
||
#[derive(StructOpt)] | ||
struct Cli { | ||
#[structopt(long)] | ||
custom_option: Option<String>, | ||
} | ||
|
||
#[async_trait(?Send)] | ||
impl<W, Wr> Writer<W> for CustomWriter<Wr> | ||
where | ||
W: World, | ||
Wr: Writer<W>, | ||
{ | ||
type Cli = cli::Compose<Cli, Wr::Cli>; | ||
|
||
async fn handle_event( | ||
&mut self, | ||
ev: parser::Result<event::Cucumber<W>>, | ||
cli: &Self::Cli, | ||
) { | ||
// Some custom logic including `cli.left.custom_option`. | ||
|
||
self.0.handle_event(ev, &cli.right).await; | ||
} | ||
} | ||
|
||
// useful blanket impls | ||
|
||
#[async_trait(?Send)] | ||
impl<'val, W, Wr, Val> ArbitraryWriter<'val, W, Val> for CustomWriter<Wr> | ||
where | ||
W: World, | ||
Self: Writer<W>, | ||
Wr: ArbitraryWriter<'val, W, Val>, | ||
Val: 'val, | ||
{ | ||
async fn write(&mut self, val: Val) | ||
where | ||
'val: 'async_trait, | ||
{ | ||
self.0.write(val).await; | ||
} | ||
} | ||
|
||
impl<W, Wr> FailureWriter<W> for CustomWriter<Wr> | ||
where | ||
W: World, | ||
Self: Writer<W>, | ||
Wr: FailureWriter<W>, | ||
{ | ||
fn failed_steps(&self) -> usize { | ||
self.0.failed_steps() | ||
} | ||
|
||
fn parsing_errors(&self) -> usize { | ||
self.0.parsing_errors() | ||
} | ||
|
||
fn hook_errors(&self) -> usize { | ||
self.0.hook_errors() | ||
} | ||
} | ||
``` | ||
|
||
[`Writer`]: crate::Writer"# | ||
)] | ||
#[derive(Debug, StructOpt)] | ||
pub struct Compose<L, R> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need it anymore. If it's still may be useful in some way for library users, it should have an example in its documentation (or on a module level), describing the case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left it because it can be very useful for custom |
||
where | ||
L: StructOpt, | ||
R: StructOpt, | ||
{ | ||
/// [`StructOpt`] deriver. | ||
#[structopt(flatten)] | ||
pub left: L, | ||
|
||
/// [`StructOpt`] deriver. | ||
#[structopt(flatten)] | ||
pub right: R, | ||
} | ||
|
||
impl<L, R> Compose<L, R> | ||
where | ||
L: StructOpt, | ||
R: StructOpt, | ||
{ | ||
/// Unpacks [`Compose`] into underlying `CLI`s. | ||
pub fn into_inner(self) -> (L, R) { | ||
let Compose { left, right } = self; | ||
(left, right) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option and the one above have the same descriptions, which is quite confusing.