xtask: Propagate stdio/stderr, exitcodes

This commit is contained in:
Henrik Tjäder 2023-02-08 22:09:32 +01:00
parent 6742936e07
commit 10a896ab9b
3 changed files with 37 additions and 20 deletions

View file

@ -12,3 +12,4 @@ env_logger = "0.10.0"
log = "0.4.17" log = "0.4.17"
rayon = "1.6.1" rayon = "1.6.1"
diffy = "0.3.0" diffy = "0.3.0"
exitcode = "1.1.2"

View file

@ -1,4 +1,4 @@
use crate::{debug, info, RunResult, Sizearguments, TestRunError}; use crate::{debug, RunResult, Sizearguments, TestRunError};
use core::fmt; use core::fmt;
use os_pipe::pipe; use os_pipe::pipe;
use std::{fs::File, io::Read, process::Command}; use std::{fs::File, io::Read, process::Command};
@ -315,19 +315,17 @@ pub fn run_command(command: &CargoCommand) -> anyhow::Result<RunResult> {
.spawn()?; .spawn()?;
// retrieve output and clean up // retrieve output and clean up
let mut output = String::new(); let mut stdout = String::new();
reader.read_to_string(&mut output)?; reader.read_to_string(&mut stdout)?;
let exit_status = handle.wait()?; let exit_status = handle.wait()?;
let mut error_output = String::new(); let mut stderr = String::new();
error_reader.read_to_string(&mut error_output)?; error_reader.read_to_string(&mut stderr)?;
if !error_output.is_empty() {
info!("{error_output}");
}
Ok(RunResult { Ok(RunResult {
exit_status, 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(), file: expected_output_file.to_owned(),
})?; })?;
if expected_output != run.output { if expected_output != run.stdout {
Err(TestRunError::FileCmpError { Err(TestRunError::FileCmpError {
expected: expected_output.clone(), expected: expected_output.clone(),
got: run.output.clone(), got: run.stdout.clone(),
}) })
} else if !run.exit_status.success() { } else if !run.exit_status.success() {
Err(TestRunError::CommandError(run.clone())) Err(TestRunError::CommandError(run.clone()))

View file

@ -17,6 +17,7 @@ use std::{
}; };
use env_logger::Env; use env_logger::Env;
use exitcode;
use log::{debug, error, info, log_enabled, trace, Level}; use log::{debug, error, info, log_enabled, trace, Level};
use crate::{ use crate::{
@ -157,7 +158,8 @@ pub enum Sizearguments {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RunResult { pub struct RunResult {
exit_status: ExitStatus, exit_status: ExitStatus,
output: String, stdout: String,
stderr: String,
} }
#[derive(Debug)] #[derive(Debug)]
@ -190,7 +192,7 @@ impl fmt::Display for TestRunError {
write!( write!(
f, f,
"Command failed with exit status {}: {}", "Command failed with exit status {}: {}",
e.exit_status, e.output e.exit_status, e.stdout
) )
} }
TestRunError::PathConversionError(p) => { TestRunError::PathConversionError(p) => {
@ -253,7 +255,7 @@ fn main() -> anyhow::Result<()> {
\n{targets:#?}\n\ \n{targets:#?}\n\
By default all targets are tested.", By default all targets are tested.",
); );
process::exit(1); process::exit(exitcode::USAGE);
} }
} }
@ -295,7 +297,7 @@ fn main() -> anyhow::Result<()> {
\n{examples:#?}\n\ \n{examples:#?}\n\
By default if example flag is emitted, all examples are tested.", By default if example flag is emitted, all examples are tested.",
); );
process::exit(1); process::exit(exitcode::USAGE);
} else { } else {
} }
examples_to_run examples_to_run
@ -543,7 +545,7 @@ fn package_filter(package: &Package) -> Vec<String> {
\n{packages:#?}\n\ \n{packages:#?}\n\
By default all packages are tested.", By default all packages are tested.",
); );
process::exit(1); process::exit(exitcode::USAGE);
} }
} else { } else {
package_selected = packages; package_selected = packages;
@ -566,7 +568,7 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()>
// cargo run <..> // cargo run <..>
info!("Running example: {example}"); info!("Running example: {example}");
let cargo_run_result = run_command(command)?; 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 // Create a file for the expected output if it does not exist or mismatches
if overwrite { if overwrite {
@ -581,7 +583,7 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()>
})?; })?;
info!("Flag --overwrite-expected enabled"); info!("Flag --overwrite-expected enabled");
info!("Creating/updating file: {expected_output_file}"); 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 { } else {
run_successful(&cargo_run_result, &expected_output_file)?; run_successful(&cargo_run_result, &expected_output_file)?;
@ -595,8 +597,24 @@ fn command_parser(command: &CargoCommand, overwrite: bool) -> anyhow::Result<()>
| CargoCommand::Clippy { .. } | CargoCommand::Clippy { .. }
| CargoCommand::ExampleSize { .. } => { | CargoCommand::ExampleSize { .. } => {
let cargo_result = run_command(command)?; let cargo_result = run_command(command)?;
if !cargo_result.output.is_empty() { if let Some(exit_code) = cargo_result.exit_status.code() {
info!("{}", cargo_result.output); 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(()) Ok(())