Browse Source

test,xtask: Simplify ExitStatus handling

Tamir Duberstein 1 year ago
parent
commit
dc9f72adf0
4 changed files with 19 additions and 44 deletions
  1. 6 17
      test/integration-test/build.rs
  2. 4 7
      xtask/src/lib.rs
  3. 1 1
      xtask/src/public_api.rs
  4. 8 19
      xtask/src/run.rs

+ 6 - 17
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();
 

+ 4 - 7
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

+ 1 - 1
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(())
 }

+ 8 - 19
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<Vec<(String, PathBuf)>> {
     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::<Vec<_>>();
 
     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))
     }
 }