Skip to content
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

Add verbose CLI flag to the writer::Basic (#192) #193

Merged
merged 9 commits into from
Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
- Merging tags from `Feature` and `Rule` with `Scenario` when filtering with `--tags` CLI option. ([#166])
- `writer::AssertNormalized` forcing `Normalized` implementation. ([#182])
- `cli::Colored` trait for propagating `Coloring` to arbitrary `Writer`s. ([#189])
- `verbose` CLI option to the `writer::Basic` ([#193], [#192])

### Fixed

Expand All @@ -62,6 +63,8 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
[#181]: /../../pull/181
[#182]: /../../pull/182
[#188]: /../../pull/188
[#192]: /../../pull/192
[#193]: /../../pull/193
[cef3d480]: /../../commit/cef3d480579190425461ddb04a1248675248351e
[rev]: /../../commit/rev-full
[0110-1]: https://llg.cubic.org/docs/junit
Expand Down
10 changes: 7 additions & 3 deletions book/src/output/terminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,13 @@ async fn main() {
AnimalWorld::cucumber()
.max_concurrent_scenarios(1)
.with_writer(
writer::Basic::raw(io::stdout(), writer::Coloring::Never, false)
.summarized()
.assert_normalized(),
writer::Basic::raw(
io::stdout(),
writer::Coloring::Never,
writer::basic::Verbosity::None,
)
.summarized()
.assert_normalized(),
)
.run_and_exit("/tests/features/book/output/terminal.feature")
.await;
Expand Down
237 changes: 156 additions & 81 deletions src/writer/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ use crate::{
/// CLI options of a [`Basic`] [`Writer`].
#[derive(clap::Args, Clone, Copy, Debug)]
pub struct Cli {
/// Increased verbosity of an output: additionally outputs step's doc
/// string (if present).
#[clap(long, short)]
pub verbose: bool,
/// Increased verbosity of an output.
///
/// '-v' outputs World on failed Steps.
/// '-vv' additionally outputs Docstring.
#[clap(short, parse(from_occurrences))]
pub verbose: u8,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we loose an ability to specify -v 0. I guess we shoud shift them for -v being the default None and -vv outputing World on failed Steps.


/// Coloring policy for a console output.
#[clap(long, name = "auto|always|never", default_value = "auto")]
Expand Down Expand Up @@ -81,6 +83,64 @@ impl FromStr for Coloring {
}
}

/// Verbosity level of the output.
#[derive(Clone, Copy, Debug)]
pub enum Verbosity {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be declared in writer/mod.rs and be a common one.

/// Doesn't output additional info.
None,

/// Outputs [`World`] on [`Failed`] [`Step`]s.
///
/// [`Failed`]: event::Step::Failed
/// [`Step`]: gherkin::Step
OutputWorld,

/// Additionally to the [`OutputWorld`] outputs docstring.
///
/// [`OutputWorld`]: Self::OutputWorld
OutputWorldAndDocString,
}

impl From<u8> for Verbosity {
fn from(v: u8) -> Self {
match v {
0 => Self::None,
1 => Self::OutputWorld,
_ => Self::OutputWorldAndDocString,
}
}
}

impl From<Verbosity> for u8 {
fn from(v: Verbosity) -> Self {
match v {
Verbosity::None => 0,
Verbosity::OutputWorld => 1,
Verbosity::OutputWorldAndDocString => 2,
}
}
}

impl Verbosity {
/// Indicates, whether [`World`] should be outputted on the [`Failed`]
/// [`Step`]s.
///
/// [`Failed`]: event::Step::Failed
/// [`Step`]: gherkin::Step
#[must_use]
pub const fn output_world(&self) -> bool {
matches!(self, Self::OutputWorld | Self::OutputWorldAndDocString)
}

/// Indicates, whether [`Step::docstring`][1]s should be outputted.
///
/// [1]: gherkin::Step::docstring
#[must_use]
pub const fn output_docstring(&self) -> bool {
matches!(self, Self::OutputWorldAndDocString)
}
}

/// Default [`Writer`] implementation outputting to an [`io::Write`] implementor
/// ([`io::Stdout`] by default).
///
Expand Down Expand Up @@ -112,11 +172,8 @@ pub struct Basic<Out: io::Write = io::Stdout> {
/// Number of lines to clear.
lines_to_clear: usize,

/// Increased verbosity of an output: additionally outputs
/// [`Step::docstring`][1]s if present.
///
/// [1]: gherkin::Step::docstring
verbose: bool,
/// [`Verbosity`] of the output.
verbose: Verbosity,
}

#[async_trait(?Send)]
Expand Down Expand Up @@ -177,7 +234,7 @@ impl Basic {
/// [`Normalized`]: writer::Normalized
#[must_use]
pub fn stdout<W>() -> writer::Normalize<W, Self> {
Self::new(io::stdout(), Coloring::Auto, false)
Self::new(io::stdout(), Coloring::Auto, Verbosity::None)
}
}

Expand All @@ -190,7 +247,7 @@ impl<Out: io::Write> Basic<Out> {
pub fn new<W>(
output: Out,
color: Coloring,
verbose: bool,
verbose: Verbosity,
) -> writer::Normalize<W, Self> {
Self::raw(output, color, verbose).normalized()
}
Expand All @@ -204,23 +261,28 @@ impl<Out: io::Write> Basic<Out> {
///
/// [`Normalized`]: writer::Normalized
#[must_use]
pub fn raw(output: Out, color: Coloring, verbose: bool) -> Self {
pub fn raw(output: Out, color: Coloring, verbosity: Verbosity) -> Self {
let mut basic = Self {
output,
styles: Styles::new(),
indent: 0,
lines_to_clear: 0,
verbose: false,
verbose: verbosity,
};
basic.apply_cli(Cli { verbose, color });
basic.apply_cli(Cli {
verbose: verbosity.into(),
color,
});
basic
}

/// Applies the given [`Cli`] options to this [`Basic`] [`Writer`].
pub fn apply_cli(&mut self, cli: Cli) {
if cli.verbose {
self.verbose = true;
}
match cli.verbose {
1 => self.verbose = Verbosity::OutputWorld,
2 => self.verbose = Verbosity::OutputWorldAndDocString,
_ => {}
};
self.styles.apply_coloring(cli.color);
}

Expand Down Expand Up @@ -455,12 +517,14 @@ impl<Out: io::Write> Basic<Out> {
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}))
.and_then(|doc| self.verbose.output_docstring().then(
|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}
))
.unwrap_or_default(),
step.table
.as_ref()
Expand Down Expand Up @@ -497,7 +561,7 @@ impl<Out: io::Write> Basic<Out> {
.docstring
.as_ref()
.and_then(|doc| {
self.verbose.then(|| {
self.verbose.output_docstring().then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
Expand Down Expand Up @@ -534,27 +598,29 @@ impl<Out: io::Write> Basic<Out> {
self.output.write_line(&self.styles.skipped(format!(
"{indent}? {} {}{}{}\n\
{indent} Step skipped: {}:{}:{}",
step.keyword,
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)))
.unwrap_or_default(),
step.table
.as_ref()
.map(|t| format_table(t, self.indent))
.unwrap_or_default(),
feat.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or(&feat.name),
step.position.line,
step.position.col,
indent = " ".repeat(self.indent.saturating_sub(3)),
)))
step.keyword,
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.output_docstring().then(
|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
))
.unwrap_or_default(),
step.table
.as_ref()
.map(|t| format_table(t, self.indent))
.unwrap_or_default(),
feat.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or(&feat.name),
step.position.line,
step.position.col,
indent = " ".repeat(self.indent.saturating_sub(3)),
)))
}

/// Outputs the [failed] [`Step`].
Expand Down Expand Up @@ -595,10 +661,12 @@ impl<Out: io::Write> Basic<Out> {
{indent} Captured output: {}{}",
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)))
.and_then(|doc| self.verbose.output_docstring().then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}))
.unwrap_or_default(),
step.table
.as_ref()
Expand All @@ -619,6 +687,7 @@ impl<Out: io::Write> Basic<Out> {
format!("{:#?}", w),
self.indent.saturating_sub(3) + 3,
))
.filter(|_| self.verbose.output_world())
.unwrap_or_default(),
indent = " ".repeat(self.indent.saturating_sub(3))
));
Expand Down Expand Up @@ -689,12 +758,14 @@ impl<Out: io::Write> Basic<Out> {
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}))
.and_then(|doc| self.verbose.output_docstring().then(
|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}
))
.unwrap_or_default(),
step.table
.as_ref()
Expand Down Expand Up @@ -732,7 +803,7 @@ impl<Out: io::Write> Basic<Out> {
.docstring
.as_ref()
.and_then(|doc| {
self.verbose.then(|| {
self.verbose.output_docstring().then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
Expand Down Expand Up @@ -770,27 +841,29 @@ impl<Out: io::Write> Basic<Out> {
self.output.write_line(&self.styles.skipped(format!(
"{indent}?> {} {}{}{}\n\
{indent} Background step failed: {}:{}:{}",
step.keyword,
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)))
.unwrap_or_default(),
step.table
.as_ref()
.map(|t| format_table(t, self.indent))
.unwrap_or_default(),
feat.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or(&feat.name),
step.position.line,
step.position.col,
indent = " ".repeat(self.indent.saturating_sub(3)),
)))
step.keyword,
step.value,
step.docstring
.as_ref()
.and_then(|doc| self.verbose.output_docstring().then(
|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
))
.unwrap_or_default(),
step.table
.as_ref()
.map(|t| format_table(t, self.indent))
.unwrap_or_default(),
feat.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or(&feat.name),
step.position.line,
step.position.col,
indent = " ".repeat(self.indent.saturating_sub(3)),
)))
}

/// Outputs the [failed] [`Background`] [`Step`].
Expand Down Expand Up @@ -832,10 +905,12 @@ impl<Out: io::Write> Basic<Out> {
{indent} Captured output: {}{}",
step.docstring
.as_ref()
.and_then(|doc| self.verbose.then(|| format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)))
.and_then(|doc| self.verbose.output_docstring().then(|| {
format_str_with_indent(
doc,
self.indent.saturating_sub(3) + 3,
)
}))
.unwrap_or_default(),
step.table
.as_ref()
Expand Down
1 change: 1 addition & 0 deletions src/writer/discard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ where
Wr: Writer<W> + ?Sized,
{
/// Does nothing.
#[allow(clippy::unused_async)] // false positive: #[async_trait]
async fn write(&mut self, _: Val)
where
'val: 'async_trait,
Expand Down
Loading