test,xtask: Simplify ExitStatus handling

pull/727/head
Tamir Duberstein 2 years ago
parent 72afd877b5
commit dc9f72adf0
No known key found for this signature in database

@ -4,7 +4,7 @@ use std::{
fs, fs,
io::{BufRead as _, BufReader}, io::{BufRead as _, BufReader},
path::PathBuf, path::PathBuf,
process::{Child, Command, Stdio}, process::{Child, Command, Output, Stdio},
}; };
use cargo_metadata::{ use cargo_metadata::{
@ -166,16 +166,11 @@ fn main() {
) )
.unwrap(); .unwrap();
let status = child let output = child
.wait() .wait_with_output()
.unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}")); .unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}"));
match status.code() { let Output { status, .. } = &output;
Some(code) => match code { assert_eq!(status.code(), Some(0), "{cmd:?} failed: {output:?}");
0 => {}
code => panic!("{cmd:?} exited with status code {code}"),
},
None => panic!("{cmd:?} terminated by signal"),
}
} }
let target = format!("{target}-unknown-none"); let target = format!("{target}-unknown-none");
@ -257,13 +252,7 @@ fn main() {
let status = child let status = child
.wait() .wait()
.unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}")); .unwrap_or_else(|err| panic!("failed to wait for {cmd:?}: {err}"));
match status.code() { assert_eq!(status.code(), Some(0), "{cmd:?} failed: {status:?}");
Some(code) => match code {
0 => {}
code => panic!("{cmd:?} exited with status code {code}"),
},
None => panic!("{cmd:?} terminated by signal"),
}
stderr.join().map_err(std::panic::resume_unwind).unwrap(); stderr.join().map_err(std::panic::resume_unwind).unwrap();

@ -1,4 +1,4 @@
use anyhow::{anyhow, Context as _, Result}; use anyhow::{bail, Context as _, Result};
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -13,13 +13,10 @@ pub fn exec(cmd: &mut Command) -> Result<()> {
let status = cmd let status = cmd
.status() .status()
.with_context(|| format!("failed to run {cmd:?}"))?; .with_context(|| format!("failed to run {cmd:?}"))?;
match status.code() { if status.code() != Some(0) {
Some(code) => match code { bail!("{cmd:?} failed: {status:?}")
0 => Ok(()),
code => Err(anyhow!("{cmd:?} exited with code {code}")),
},
None => Err(anyhow!("{cmd:?} terminated by signal")),
} }
Ok(())
} }
// Create a symlink in the out directory to work around the fact that cargo ignores anything // Create a symlink in the out directory to work around the fact that cargo ignores anything

@ -62,7 +62,7 @@ pub fn public_api(options: Options, metadata: Metadata) -> Result<()> {
}); });
if !errors.is_empty() { if !errors.is_empty() {
bail!("public API errors:\n{errors}"); bail!("public API errors:\n{errors}")
} }
Ok(()) Ok(())
} }

@ -6,7 +6,7 @@ use std::{
process::{Child, Command, Stdio}, 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 cargo_metadata::{Artifact, CompilerMessage, Message, Target};
use clap::Parser; use clap::Parser;
use xtask::AYA_BUILD_INTEGRATION_BPF; use xtask::AYA_BUILD_INTEGRATION_BPF;
@ -77,14 +77,9 @@ pub fn build(opts: BuildOptions) -> Result<Vec<(String, PathBuf)>> {
let status = child let status = child
.wait() .wait()
.with_context(|| format!("failed to wait for {cmd:?}"))?; .with_context(|| format!("failed to wait for {cmd:?}"))?;
match status.code() { if status.code() != Some(0) {
Some(code) => match code { bail!("{cmd:?} failed: {status:?}")
0 => {}
code => bail!("{cmd:?} exited with status code {code}"),
},
None => bail!("{cmd:?} terminated by signal"),
} }
Ok(executables) Ok(executables)
} }
@ -98,7 +93,7 @@ pub fn run(opts: Options) -> Result<()> {
let binaries = build(build_options).context("error while building userspace application")?; let binaries = build(build_options).context("error while building userspace application")?;
let mut args = runner.trim().split_terminator(' '); 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 args = args.collect::<Vec<_>>();
let mut failures = String::new(); let mut failures = String::new();
@ -110,24 +105,18 @@ pub fn run(opts: Options) -> Result<()> {
.args(run_args.iter()) .args(run_args.iter())
.arg("--test-threads=1"); .arg("--test-threads=1");
println!("{} running {cmd:?}", name); println!("{name} running {cmd:?}");
let status = cmd let status = cmd
.status() .status()
.with_context(|| format!("failed to run {cmd:?}"))?; .with_context(|| format!("failed to run {cmd:?}"))?;
match status.code() { if status.code() != Some(0) {
Some(code) => match code { writeln!(&mut failures, "{name} failed: {status:?}").context("String write failed")?
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 failures.is_empty() { if failures.is_empty() {
Ok(()) Ok(())
} else { } else {
Err(anyhow::anyhow!("failures:\n{}", failures)) Err(anyhow!("failures:\n{}", failures))
} }
} }

Loading…
Cancel
Save