Fix panic on --runs 0 (#923)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: reject --runs/--max-runs 0, clamp --min-runs to 1 * Validate positive run counts with clap ---------
authored by Vincent · committed by GitHub · parent 8d908b0 · Browse files · 3 files·+37 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: VXNCXNX <VXNCXNX@users.noreply.github.com>
- Co-authored-by: David Peter <mail@david-peter.de>
Diff stats
3 files+37 added−14 removed
- tests:+26 added−0 removed
- other:+11 added−14 removed
Provenance
- Commit
907de26- Tree
6546fee- Author
Vincent <93332837+VXNCXNX@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8d908b0
3 files changed+37 added−14 removed
modified:src/cli.rs+4 added
39 unmodified lines40 .long("warmup")41 .short('w')42 .value_name("NUM")43 .action(ArgAction::Set)44+ .value_parser(clap::value_parser!(u64))45 .help(46 "Perform NUM warmup runs before the actual benchmark. This can be used \47 to fill (disk) caches for I/O-heavy programs.",48 ),3 unmodified lines52 .long("min-runs")53 .short('m')54 .action(ArgAction::Set)55 .value_name("NUM")56+ .value_parser(clap::value_parser!(u64).range(1..))57 .help("Perform at least NUM runs for each command (default: 10)."),58 )59 .arg(60 Arg::new("max-runs")61 .long("max-runs")62 .short('M')63 .action(ArgAction::Set)64 .value_name("NUM")65+ .value_parser(clap::value_parser!(u64).range(1..))66 .help("Perform at most NUM runs for each command. By default, there is no limit."),67 )68 .arg(69 Arg::new("runs")1 unmodified line71 .conflicts_with_all(["max-runs", "min-runs"])72 .short('r')73 .action(ArgAction::Set)74 .value_name("NUM")75+ .value_parser(clap::value_parser!(u64).range(1..))76 .help("Perform exactly NUM runs for each command. If this option is not specified, \77 hyperfine automatically determines the number of runs."),78 )79 .arg(339 unmodified linesmodified:src/options.rs+7 added-14 removed
273 unmodified lines274 275 impl Options {276 pub fn from_cli_arguments<'a>(matches: &ArgMatches) -> Result<Self, OptionsError<'a>> {277 let mut options = Self::default();278- let param_to_u64 = |param| {279- matches280- .get_one::<String>(param)281- .map(|n| {282- n.parse::<u64>()283- .map_err(|e| OptionsError::IntParsingError(param, e))284- })285- .transpose()286- };278+ options.warmup_count = matches279+ .get_one::<u64>("warmup")280+ .copied()281+ .unwrap_or(options.warmup_count);282 288- options.warmup_count = param_to_u64("warmup")?.unwrap_or(options.warmup_count);283+ let mut min_runs = matches.get_one::<u64>("min-runs").copied();284+ let mut max_runs = matches.get_one::<u64>("max-runs").copied();285 290- let mut min_runs = param_to_u64("min-runs")?;291- let mut max_runs = param_to_u64("max-runs")?;292-293- if let Some(runs) = param_to_u64("runs")? {286+ if let Some(&runs) = matches.get_one::<u64>("runs") {287 min_runs = Some(runs);288 max_runs = Some(runs);289 }290 250 unmodified linesmodified:tests/integration_tests.rs+26 added
73 unmodified lines74 }75 }76 77 #[test]78+fn fails_with_zero_runs() {79+ for option in ["--runs", "--min-runs", "--max-runs"] {80+ hyperfine()81+ .arg(option)82+ .arg("0")83+ .arg("echo dummy benchmark")84+ .assert()85+ .code(2)86+ .stderr(predicate::str::contains(format!(87+ "invalid value '0' for '{option} <NUM>'"88+ )));89+ }90+}91+92+#[test]93+fn min_runs_of_one_still_performs_one_run() {94+ hyperfine_debug()95+ .arg("--min-runs=1")96+ .arg("--warmup=0")97+ .arg("sleep 4")98+ .assert()99+ .success()100+ .stdout(predicate::str::contains("Time (abs ≡)"));101+}102+103+#[test]104 fn can_run_commands_without_a_shell() {105 hyperfine()106 .arg("--runs=1")107 .arg("--show-output")684 unmodified lines