|
| 1 | +use miette::Diagnostic; |
| 2 | +use moon_app::extract_task_exit_code; |
| 3 | +use moon_process::ProcessError; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +// Mirrors how `TaskRunnerError::RunFailed` wraps `Box<ProcessError>`, which |
| 7 | +// surfaces the process error as a `Box<ProcessError>` in the error chain. |
| 8 | +#[derive(Debug, Diagnostic, Error)] |
| 9 | +#[error("task failed to run")] |
| 10 | +struct WrappedError { |
| 11 | + #[source] |
| 12 | + error: Box<ProcessError>, |
| 13 | +} |
| 14 | + |
| 15 | +fn exit_nonzero(code: Option<i32>) -> ProcessError { |
| 16 | + ProcessError::ExitNonZero { |
| 17 | + bin: "bash".into(), |
| 18 | + status: "exit code".into(), |
| 19 | + code, |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +fn wrapped(code: Option<i32>) -> miette::Report { |
| 24 | + WrappedError { |
| 25 | + error: Box::new(exit_nonzero(code)), |
| 26 | + } |
| 27 | + .into() |
| 28 | +} |
| 29 | + |
| 30 | +mod extract_task_exit_code { |
| 31 | + use super::*; |
| 32 | + |
| 33 | + #[test] |
| 34 | + fn extracts_from_boxed_process_error() { |
| 35 | + assert_eq!(extract_task_exit_code(&wrapped(Some(80))), Some(80)); |
| 36 | + } |
| 37 | + |
| 38 | + #[test] |
| 39 | + fn extracts_from_bare_process_error() { |
| 40 | + let report: miette::Report = exit_nonzero(Some(2)).into(); |
| 41 | + |
| 42 | + assert_eq!(extract_task_exit_code(&report), Some(2)); |
| 43 | + } |
| 44 | + |
| 45 | + #[test] |
| 46 | + fn propagates_codes_above_127() { |
| 47 | + assert_eq!(extract_task_exit_code(&wrapped(Some(200))), Some(200)); |
| 48 | + } |
| 49 | + |
| 50 | + #[test] |
| 51 | + fn none_for_signal_without_code() { |
| 52 | + assert_eq!(extract_task_exit_code(&wrapped(None)), None); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn none_for_injected_abort_code() { |
| 57 | + // Aborts before a process spawns are recorded with an exit code of -1 |
| 58 | + assert_eq!(extract_task_exit_code(&wrapped(Some(-1))), None); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn none_for_zero_code() { |
| 63 | + assert_eq!(extract_task_exit_code(&wrapped(Some(0))), None); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn none_for_out_of_u8_range() { |
| 68 | + // e.g. large Windows exit codes that can't map to a u8 |
| 69 | + assert_eq!(extract_task_exit_code(&wrapped(Some(300))), None); |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn none_for_non_process_error() { |
| 74 | + let report = miette::miette!("something else went wrong"); |
| 75 | + |
| 76 | + assert_eq!(extract_task_exit_code(&report), None); |
| 77 | + } |
| 78 | +} |
0 commit comments