From 10a896ab9b0089a5ff9af4870488a6c604887cc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Tj=C3=A4der?= Date: Wed, 8 Feb 2023 22:09:32 +0100 Subject: [PATCH] xtask: Propagate stdio/stderr, exitcodes --- xtask/Cargo.toml | 1 + xtask/src/command.rs | 20 +++++++++----------- xtask/src/main.rs | 36 +++++++++++++++++++++++++++--------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 7341215c31..3c72bf1d3e 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -12,3 +12,4 @@ env_logger = "0.10.0" log = "0.4.17" rayon = "1.6.1" diffy = "0.3.0" +exitcode = "1.1.2" diff --git a/xtask/src/command.rs b/xtask/src/command.rs index 07ecc84fb6..d894fae282 100644 --- a/xtask/src/command.rs +++ b/xtask/src/command.rs @@ -1,4 +1,4 @@ -use crate::{debug, info, RunResult, Sizearguments, TestRunError}; +use crate::{debug, RunResult, Sizearguments, TestRunError}; use core::fmt; use os_pipe::pipe; use std::{fs::File, io::Read, process::Command}; @@ -315,19 +315,17 @@ pub fn run_command(command: &CargoCommand) -> anyhow::Result { .spawn()?; // retrieve output and clean up - let mut output = String::new(); - reader.read_to_string(&mut output)?; + let mut stdout = String::new(); + reader.read_to_string(&mut stdout)?; let exit_status = handle.wait()?; - let mut error_output = String::new(); - error_reader.read_to_string(&mut error_output)?; - if !error_output.is_empty() { - info!("{error_output}"); - } + let mut stderr = String::new(); + error_reader.read_to_string(&mut stderr)?; Ok(RunResult { exit_status, - output, + stdout, + stderr, }) } @@ -346,10 +344,10 @@ pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), file: expected_output_file.to_owned(), })?; - if expected_output != run.output { + if expected_output != run.stdout { Err(TestRunError::FileCmpError { expected: expected_output.clone(), - got: run.output.clone(), + got: run.stdout.clone(), }) } else if !run.exit_status.success() { Err(TestRunError::CommandError(run.clone())) diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 590f093eaa..e0bb8723c6 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -17,6 +17,7 @@ use std::{ }; use env_logger::Env; +use exitcode; use log::{debug, error, info, log_enabled, trace, Level}; use crate::{ @@ -157,7 +158,8 @@ pub enum Sizearguments { #[derive(Debug, Clone)] pub struct RunResult { exit_status: ExitStatus, - output: String, + stdout: String, + stderr: String, } #[derive(Debug)] @@ -190,7 +192,7 @@ impl fmt::Display for TestRunError { write!( f, "Command failed with exit status {}: {}", - e.exit_status, e.output + e.exit_status, e.stdout ) } TestRunError::PathConversionError(p) => { @@ -253,7 +255,7 @@ fn main() -> anyhow::Result<()> { \n{targets:#?}\n\ By default all targets are tested.", ); - process::exit(1); + process::exit(exitcode::USAGE); } } @@ -295,7 +297,7 @@ fn main() -> anyhow::Result<()> { \n{examples:#?}\n\ By default if example flag is emitted, all examples are tested.", ); - process::exit(1); + process::exit(exitcode::USAGE); } else { } examples_to_run @@ -543,7 +545,7 @@ fn package_filter(package: &Package) -> Vec { \n{packages:#?}\n\ By default all packages are tested.", ); - process::exit(1); + process::exit(exitcode::USAGE); } } else { package_selected = packages; @@ -566,7 +568,7 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> // cargo run <..> info!("Running example: {example}"); let cargo_run_result = run_command(command)?; - info!("{}", cargo_run_result.output); + info!("{}", cargo_run_result.stdout); // Create a file for the expected output if it does not exist or mismatches if overwrite { @@ -581,7 +583,7 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> })?; info!("Flag --overwrite-expected enabled"); info!("Creating/updating file: {expected_output_file}"); - file_handle.write_all(cargo_run_result.output.as_bytes())?; + file_handle.write_all(cargo_run_result.stdout.as_bytes())?; }; } else { run_successful(&cargo_run_result, &expected_output_file)?; @@ -595,8 +597,24 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()> | CargoCommand::Clippy { .. } | CargoCommand::ExampleSize { .. } => { let cargo_result = run_command(command)?; - if !cargo_result.output.is_empty() { - info!("{}", cargo_result.output); + if let Some(exit_code) = cargo_result.exit_status.code() { + if exit_code != exitcode::OK { + error!("Exit code from command: {exit_code}"); + if !cargo_result.stdout.is_empty() { + info!("{}", cargo_result.stdout); + } + if !cargo_result.stderr.is_empty() { + error!("{}", cargo_result.stderr); + } + process::exit(exit_code); + } else { + if !cargo_result.stdout.is_empty() { + info!("{}", cargo_result.stdout); + } + if !cargo_result.stderr.is_empty() { + info!("{}", cargo_result.stderr); + } + } } Ok(())