1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-08 23:28:18 -05:00

refactor(cli/flags): use an optional non zero usize for fail-fast (#11804)

Changes the type of the `fail_fast` flag from `Option<usize>` to
`Option<NonZeroUsize>` as an optional value of zero isn't sound.
This commit is contained in:
Casper Beyer 2021-08-23 18:37:02 +08:00 committed by GitHub
parent 198699faba
commit f3b2f23a1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 16 deletions

View file

@ -101,7 +101,7 @@ pub enum DenoSubcommand {
Test { Test {
doc: bool, doc: bool,
no_run: bool, no_run: bool,
fail_fast: Option<usize>, fail_fast: Option<NonZeroUsize>,
quiet: bool, quiet: bool,
allow_none: bool, allow_none: bool,
include: Option<Vec<String>>, include: Option<Vec<String>>,
@ -1047,16 +1047,9 @@ fn test_subcommand<'a, 'b>() -> App<'a, 'b> {
.takes_value(true) .takes_value(true)
.require_equals(true) .require_equals(true)
.value_name("N") .value_name("N")
.validator(|val: String| match val.parse::<usize>() { .validator(|val: String| match val.parse::<NonZeroUsize>() {
Ok(val) => { Ok(_) => Ok(()),
if val == 0 { Err(_) => Err("fail-fast should be a non zero integer".to_string()),
return Err(
"fail-fast should be an number greater than 0".to_string(),
);
}
Ok(())
}
Err(_) => Err("fail-fast should be a number".to_string()),
}), }),
) )
.arg( .arg(
@ -1786,7 +1779,7 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
if let Some(value) = matches.value_of("fail-fast") { if let Some(value) = matches.value_of("fail-fast") {
Some(value.parse().unwrap()) Some(value.parse().unwrap())
} else { } else {
Some(1) Some(NonZeroUsize::new(1).unwrap())
} }
} else { } else {
None None
@ -3663,7 +3656,7 @@ mod tests {
subcommand: DenoSubcommand::Test { subcommand: DenoSubcommand::Test {
no_run: false, no_run: false,
doc: false, doc: false,
fail_fast: Some(3), fail_fast: Some(NonZeroUsize::new(3).unwrap()),
filter: None, filter: None,
allow_none: false, allow_none: false,
quiet: false, quiet: false,
@ -3674,6 +3667,9 @@ mod tests {
..Flags::default() ..Flags::default()
} }
); );
let r = flags_from_vec(svec!["deno", "test", "--fail-fast=0"]);
assert!(r.is_err());
} }
#[test] #[test]

View file

@ -77,6 +77,7 @@ use std::env;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use std::iter::once; use std::iter::once;
use std::num::NonZeroUsize;
use std::path::PathBuf; use std::path::PathBuf;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
@ -1004,7 +1005,7 @@ async fn test_command(
include: Option<Vec<String>>, include: Option<Vec<String>>,
no_run: bool, no_run: bool,
doc: bool, doc: bool,
fail_fast: Option<usize>, fail_fast: Option<NonZeroUsize>,
quiet: bool, quiet: bool,
allow_none: bool, allow_none: bool,
filter: Option<String>, filter: Option<String>,

View file

@ -469,7 +469,7 @@ pub async fn run_tests(
doc_modules: Vec<ModuleSpecifier>, doc_modules: Vec<ModuleSpecifier>,
test_modules: Vec<ModuleSpecifier>, test_modules: Vec<ModuleSpecifier>,
no_run: bool, no_run: bool,
fail_fast: Option<usize>, fail_fast: Option<NonZeroUsize>,
quiet: bool, quiet: bool,
allow_none: bool, allow_none: bool,
filter: Option<String>, filter: Option<String>,
@ -621,7 +621,7 @@ pub async fn run_tests(
} }
if let Some(x) = fail_fast { if let Some(x) = fail_fast {
if summary.failed >= x { if summary.failed >= x.get() {
break; break;
} }
} }