From d31f66e7c6cac98eb8a15e902d3cf8aac9a6a6f5 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 3 Aug 2023 11:28:32 -0400 Subject: [PATCH] Simplify ExitStatus handling --- test/integration-test/build.rs | 23 ++++++----------------- xtask/src/lib.rs | 11 ++++------- xtask/src/public_api.rs | 2 +- xtask/src/run.rs | 27 ++++++++------------------- 4 files changed, 19 insertions(+), 44 deletions(-) diff --git a/test/integration-test/build.rs b/test/integration-test/build.rs index cd816164..2e3bef84 100644 --- a/test/integration-test/build.rs +++ b/test/integration-test/build.rs @@ -4,7 +4,7 @@ use std::{ fs, io::{BufRead as _, BufReader}, path::PathBuf, - process::{Child, Command, Stdio}, + process::{Child, Command, Output, Stdio}, }; use cargo_metadata::{ @@ -166,16 +166,11 @@ fn main() { ) .unwrap(); - let status = child - .wait() + let output = child + .wait_with_output() .unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}")); - match status.code() { - Some(code) => match code { - 0 => {} - code => panic!("{cmd:?} exited with status code {code}"), - }, - None => panic!("{cmd:?} terminated by signal"), - } + let Output { status, .. } = &output; + assert_eq!(status.code(), Some(0), "{cmd:?} failed: {output:?}"); } let target = format!("{target}-unknown-none"); @@ -257,13 +252,7 @@ fn main() { let status = child .wait() .unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}")); - match status.code() { - Some(code) => match code { - 0 => {} - code => panic!("{cmd:?} exited with status code {code}"), - }, - None => panic!("{cmd:?} terminated by signal"), - } + assert_eq!(status.code(), Some(0), "{cmd:?} failed: {status:?}"); stderr.join().map_err(std::panic::resume_unwind).unwrap(); diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 09db4cbc..9e5c8737 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context as _, Result}; +use anyhow::{bail, Context as _, Result}; use std::{ fs, path::{Path, PathBuf}, @@ -13,13 +13,10 @@ pub fn exec(cmd: &mut Command) -> Result<()> { let status = cmd .status() .with_context(|| format!("failed to run {cmd:?}"))?; - match status.code() { - Some(code) => match code { - 0 => Ok(()), - code => Err(anyhow!("{cmd:?} exited with code {code}")), - }, - None => Err(anyhow!("{cmd:?} terminated by signal")), + if status.code() != Some(0) { + bail!("{cmd:?} failed: {status:?}") } + Ok(()) } // Create a symlink in the out directory to work around the fact that cargo ignores anything diff --git a/xtask/src/public_api.rs b/xtask/src/public_api.rs index 56db350b..b5b58f07 100644 --- a/xtask/src/public_api.rs +++ b/xtask/src/public_api.rs @@ -62,7 +62,7 @@ pub fn public_api(options: Options, metadata: Metadata) -> Result<()> { }); if !errors.is_empty() { - bail!("public API errors:\n{errors}"); + bail!("public API errors:\n{errors}") } Ok(()) } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 7a29ad5f..6742a26b 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -6,7 +6,7 @@ use std::{ process::{Child, Command, Stdio}, }; -use anyhow::{bail, Context as _, Result}; +use anyhow::{anyhow, bail, Context as _, Result}; use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; use clap::Parser; use xtask::AYA_BUILD_INTEGRATION_BPF; @@ -77,14 +77,9 @@ pub fn build(opts: BuildOptions) -> Result> { let status = child .wait() .with_context(|| format!("failed to wait for {cmd:?}"))?; - match status.code() { - Some(code) => match code { - 0 => {} - code => bail!("{cmd:?} exited with status code {code}"), - }, - None => bail!("{cmd:?} terminated by signal"), + if status.code() != Some(0) { + bail!("{cmd:?} failed: {status:?}") } - Ok(executables) } @@ -98,7 +93,7 @@ pub fn run(opts: Options) -> Result<()> { let binaries = build(build_options).context("error while building userspace application")?; let mut args = runner.trim().split_terminator(' '); - let runner = args.next().ok_or(anyhow::anyhow!("no first argument"))?; + let runner = args.next().ok_or(anyhow!("no first argument"))?; let args = args.collect::>(); let mut failures = String::new(); @@ -110,24 +105,18 @@ pub fn run(opts: Options) -> Result<()> { .args(run_args.iter()) .arg("--test-threads=1"); - println!("{} running {cmd:?}", name); + println!("{name} running {cmd:?}"); let status = cmd .status() .with_context(|| format!("failed to run {cmd:?}"))?; - match status.code() { - Some(code) => match code { - 0 => {} - code => writeln!(&mut failures, "{} exited with status code {code}", name) - .context("String write failed")?, - }, - None => writeln!(&mut failures, "{} terminated by signal", name) - .context("String write failed")?, + if status.code() != Some(0) { + writeln!(&mut failures, "{name} failed: {status:?}").context("String write failed")? } } if failures.is_empty() { Ok(()) } else { - Err(anyhow::anyhow!("failures:\n{}", failures)) + Err(anyhow!("failures:\n{}", failures)) } }