-
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
Add verbose
CLI flag to the writer::Basic
(#192)
#193
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
63bae2b
Add `skip_world` flag to the `writer::Basic`
ilslv 827dcb2
CHANGELOG
ilslv b659c24
Add `verbose` flag
ilslv 77a5343
Merge branch 'main' into skip-printing-world
ilslv 3045c7f
Corrections
ilslv 229db9c
Corrections
ilslv fd1ad6b
Corrections and book update
tyranron bc342a6
Fixes
tyranron 021cb4b
Fix defaults
tyranron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
||
/// Coloring policy for a console output. | ||
#[clap(long, name = "auto|always|never", default_value = "auto")] | ||
|
@@ -81,6 +83,64 @@ impl FromStr for Coloring { | |
} | ||
} | ||
|
||
/// Verbosity level of the output. | ||
#[derive(Clone, Copy, Debug)] | ||
pub enum Verbosity { | ||
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. This may be declared in |
||
/// 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). | ||
/// | ||
|
@@ -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)] | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -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() | ||
} | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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() | ||
|
@@ -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, | ||
|
@@ -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`]. | ||
|
@@ -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() | ||
|
@@ -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)) | ||
)); | ||
|
@@ -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() | ||
|
@@ -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, | ||
|
@@ -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`]. | ||
|
@@ -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() | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 way we loose an ability to specify
-v 0
. I guess we shoud shift them for-v
being the defaultNone
and-vv
outputing World on failed Steps.