Support list of exit codes in --ignore-failure
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 16348dc · Browse files · 4 files·+136 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+136 added−24 removed
- tests:+72 added−0 removed
- other:+64 added−24 removed
Provenance
- Commit
b5a6860- Tree
4dd8d16- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
16348dc
4 files changed+136 added−24 removed
modified:src/benchmark/executor.rs+35 added-17 removed
77 unmodified lines78 79 let result = execute_and_measure(command)80 .with_context(|| format!("Failed to run command '{command_name}'"))?;81 82- if command_failure_action == CmdFailureAction::RaiseError && !result.status.success() {83- let when = match iteration {84- BenchmarkIteration::NonBenchmarkRun => "a non-benchmark run".to_string(),85- BenchmarkIteration::Warmup(0) => "the first warmup run".to_string(),86- BenchmarkIteration::Warmup(i) => format!("warmup iteration {i}"),87- BenchmarkIteration::Benchmark(0) => "the first benchmark run".to_string(),88- BenchmarkIteration::Benchmark(i) => format!("benchmark iteration {i}"),82+ if !result.status.success() {83+ use crate::util::exit_code::extract_exit_code;84+85+ let should_fail = match command_failure_action {86+ CmdFailureAction::RaiseError => true,87+ CmdFailureAction::IgnoreAllFailures => false,88+ CmdFailureAction::IgnoreSpecificFailures(ref codes) => {89+ // Only fail if the exit code is not in the list of codes to ignore90+ if let Some(exit_code) = extract_exit_code(result.status) {91+ !codes.contains(&exit_code)92+ } else {93+ // If we can't extract an exit code, treat it as a failure94+ true95+ }96+ }97 };90- bail!(91- "{cause} in {when}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \92- Alternatively, use the '--show-output' option to debug what went wrong.",93- cause=result.status.code().map_or(94- "The process has been terminated by a signal".into(),95- |c| format!("Command terminated with non-zero exit code {c}")98 99+ if should_fail {100+ let when = match iteration {101+ BenchmarkIteration::NonBenchmarkRun => "a non-benchmark run".to_string(),102+ BenchmarkIteration::Warmup(0) => "the first warmup run".to_string(),103+ BenchmarkIteration::Warmup(i) => format!("warmup iteration {i}"),104+ BenchmarkIteration::Benchmark(0) => "the first benchmark run".to_string(),105+ BenchmarkIteration::Benchmark(i) => format!("benchmark iteration {i}"),106+ };107+ bail!(108+ "{cause} in {when}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \109+ Alternatively, use the '--show-output' option to debug what went wrong.",110+ cause=result.status.code().map_or(111+ "The process has been terminated by a signal".into(),112+ |c| format!("Command terminated with non-zero exit code {c}")113+97- ),98- );114+ ),115+ );116+ }117 }118 119 Ok(result)120 }18 unmodified lines139 ) -> Result<(TimingResult, ExitStatus)> {140 let result = run_command_and_measure_common(141 command.get_command()?,142 iteration,125- command_failure_action.unwrap_or(self.options.command_failure_action),143+ command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),144 &self.options.command_input_policy,145 output_policy,146 &command.get_command_line(),147 )?;56 unmodified lines204 205 let mut result = run_command_and_measure_common(206 command_builder,207 iteration,190- command_failure_action.unwrap_or(self.options.command_failure_action),208+ command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),209 &self.options.command_input_policy,210 output_policy,211 &command.get_command_line(),212 )?;156 unmodified linesmodified:src/cli.rs+8 added-2 removed
224 unmodified lines225 )226 .arg(227 Arg::new("ignore-failure")228 .long("ignore-failure")229- .action(ArgAction::SetTrue)229+ .action(ArgAction::Set)230+ .value_name("MODE")231+ .num_args(0..=1)232+ .default_missing_value("all-non-zero")233+ .require_equals(true)234 .short('i')231- .help("Ignore non-zero exit codes of the benchmarked programs."),235+ .help("Ignore failures of the benchmarked programs. Without a value or with \236+ 'all-non-zero', all non-zero exit codes are ignored. You can also provide \237+ a comma-separated list of exit codes to ignore (e.g., --ignore-failure=1,2)."),238 )239 .arg(240 Arg::new("style")241 .long("style")173 unmodified linesmodified:src/options.rs+21 added-5 removed
65 unmodified lines66 }67 }68 69 /// Action to take when an executed command fails.70-#[derive(Debug, Clone, Copy, PartialEq, Eq)]70+#[derive(Debug, Clone, PartialEq, Eq)]71 pub enum CmdFailureAction {72 /// Exit with an error message73 RaiseError,74 75- /// Simply ignore the non-zero exit code76- Ignore,75+ /// Ignore all non-zero exit codes76+ IgnoreAllFailures,77+78+ /// Ignore specific exit codes79+ IgnoreSpecificFailures(Vec<i32>),80 }81 82 /// Output style type option83 #[derive(Debug, Clone, Copy, PartialEq, Eq)]333 unmodified lines417 (true, None) => ExecutorKind::Mock(None),418 }419 };420 418- if matches.get_flag("ignore-failure") {419- options.command_failure_action = CmdFailureAction::Ignore;421+ if let Some(mode) = matches.get_one::<String>("ignore-failure") {422+ options.command_failure_action = match mode.as_str() {423+ "all-non-zero" | "" => CmdFailureAction::IgnoreAllFailures,424+ codes => {425+ let exit_codes: Result<Vec<i32>, _> = codes426+ .split(',')427+ .map(|s| {428+ s.trim()429+ .parse::<i32>()430+ .map_err(|e| OptionsError::IntParsingError("ignore-failure", e))431+ })432+ .collect();433+ CmdFailureAction::IgnoreSpecificFailures(exit_codes?)434+ }435+ };436 }437 438 options.time_unit = match matches.get_one::<String>("time-unit").map(|s| s.as_str()) {439 Some("microsecond") => Some(Unit::MicroSecond),108 unmodified linesmodified:tests/integration_tests.rs+72 added
275 unmodified lines276 .assert()277 .success();278 }279 280+#[cfg(unix)]281 #[test]282+fn can_ignore_specific_exit_codes() {283+ // Test that specifying exit code 1 ignores it284+ hyperfine()285+ .arg("--runs=1")286+ .arg("--ignore-failure=1")287+ .arg("exit 1")288+ .assert()289+ .success();290+291+ // Test that other exit codes still fail292+ hyperfine()293+ .arg("--runs=1")294+ .arg("--ignore-failure=1")295+ .arg("exit 2")296+ .assert()297+ .failure()298+ .stderr(predicate::str::contains(299+ "Command terminated with non-zero exit code 2",300+ ));301+}302+303+#[cfg(unix)]304+#[test]305+fn can_ignore_multiple_exit_codes() {306+ // Test that all specified exit codes are ignored307+ hyperfine()308+ .arg("--runs=1")309+ .arg("--ignore-failure=1,2,3")310+ .arg("exit 1")311+ .assert()312+ .success();313+314+ hyperfine()315+ .arg("--runs=1")316+ .arg("--ignore-failure=1,2,3")317+ .arg("exit 2")318+ .assert()319+ .success();320+321+ hyperfine()322+ .arg("--runs=1")323+ .arg("--ignore-failure=1,2,3")324+ .arg("exit 3")325+ .assert()326+ .success();327+328+ // Test that other exit codes still fail329+ hyperfine()330+ .arg("--runs=1")331+ .arg("--ignore-failure=1,2,3")332+ .arg("exit 4")333+ .assert()334+ .failure()335+ .stderr(predicate::str::contains(336+ "Command terminated with non-zero exit code 4",337+ ));338+}339+340+#[cfg(unix)]341+#[test]342+fn ignore_failure_with_all_non_zero() {343+ // Test explicit "all-non-zero" mode344+ hyperfine()345+ .arg("--runs=1")346+ .arg("--ignore-failure=all-non-zero")347+ .arg("exit 5")348+ .assert()349+ .success();350+}351+352+#[test]353 fn shows_output_of_benchmarked_command() {354 hyperfine()355 .arg("--runs=2")356 .arg("--command-name=dummy")368 unmodified lines