From 35f96024c92bf01b32dfe17c5b10c4f31d5e52d9 Mon Sep 17 00:00:00 2001 From: Alex Hornby Date: Sun, 25 Jun 2023 15:39:27 +0100 Subject: [PATCH] preserve exit status code tested on command line with: cargo run -- foo; echo $? before: 0 after: 2 added a test --- src/main.rs | 14 ++++++++++---- tests/integration_buck2.rs | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index a08189f..bd16777 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,22 +224,28 @@ fn get_buck2_path() -> Result { } fn main() -> Result<(), Error> { - let buck2_path = dbg!(get_buck2_path()?); + let binary_path = dbg!(get_buck2_path()?); // Collect information indented for buck2 binary. let mut args = env::args_os(); args.next(); // Skip buckle - let envs = env::vars_os(); + + let envs = env::vars_os(); // Pass all file descriptors through as well. - Command::new(buck2_path) + let status = Command::new(&binary_path) .args(args) .envs(envs) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit()) .output() - .expect("Failed to execute buck2."); + .unwrap_or_else(|_| panic!("Failed to execute {:?}", &binary_path)) + .status; + + if !status.success() { + std::process::exit(status.code().unwrap_or(1)); + } Ok(()) } diff --git a/tests/integration_buck2.rs b/tests/integration_buck2.rs index 2fd115b..b5046d0 100644 --- a/tests/integration_buck2.rs +++ b/tests/integration_buck2.rs @@ -13,3 +13,13 @@ fn test_buck2_latest() { assert!(stdout.starts_with("buck2 "), "found {}", stdout); assert.success(); } + +#[test] +fn test_buck2_fail() { + let mut cmd = Command::cargo_bin("buckle").unwrap(); + cmd.arg("--totally-unknown-argument"); + let assert = cmd.assert(); + let stderr = String::from_utf8(assert.get_output().stderr.to_vec()).unwrap(); + assert!(stderr.contains("/buckle/buck2/"), "found {}", stderr); + assert.failure(); +}