2023-02-04 15:22:43 +01:00
|
|
|
use crate::Sizearguments;
|
2021-09-22 13:22:45 +02:00
|
|
|
use crate::{RunResult, TestRunError};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
|
|
|
use os_pipe::pipe;
|
2021-12-26 10:43:57 +01:00
|
|
|
use std::{fs::File, io::Read, process::Command};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum BuildMode {
|
|
|
|
Release,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[derive(Debug)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub enum CargoCommand<'a> {
|
|
|
|
Run {
|
|
|
|
example: &'a str,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2021-12-26 10:43:57 +01:00
|
|
|
BuildAll {
|
2021-08-26 10:58:59 +02:00
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-04 15:22:43 +01:00
|
|
|
Size {
|
|
|
|
example: &'a str,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
arguments: Option<Sizearguments>,
|
|
|
|
},
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> CargoCommand<'a> {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
CargoCommand::Run { .. } => "run",
|
2023-02-04 15:22:43 +01:00
|
|
|
CargoCommand::Size { .. } => "size",
|
2021-12-26 10:43:57 +01:00
|
|
|
CargoCommand::BuildAll { .. } => "build",
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn args(&self) -> Vec<&str> {
|
|
|
|
match self {
|
|
|
|
CargoCommand::Run {
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
2022-12-14 21:26:40 +01:00
|
|
|
let mut args = vec![
|
2023-01-02 14:34:05 +01:00
|
|
|
"+nightly",
|
2022-12-14 21:26:40 +01:00
|
|
|
self.name(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target,
|
|
|
|
"--features",
|
|
|
|
"test-critical-section",
|
|
|
|
];
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
if let Some(feature_name) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature_name]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2021-12-26 10:43:57 +01:00
|
|
|
CargoCommand::BuildAll {
|
2021-08-26 10:58:59 +02:00
|
|
|
target,
|
|
|
|
features,
|
2021-12-26 10:43:57 +01:00
|
|
|
mode,
|
2021-08-26 10:58:59 +02:00
|
|
|
} => {
|
2023-01-09 21:02:53 +01:00
|
|
|
let mut args = vec![
|
|
|
|
"+nightly",
|
|
|
|
self.name(),
|
|
|
|
"--examples",
|
|
|
|
"--target",
|
|
|
|
target,
|
|
|
|
"--features",
|
|
|
|
"test-critical-section",
|
|
|
|
];
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
if let Some(feature_name) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature_name]);
|
|
|
|
}
|
2021-12-26 10:43:57 +01:00
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
args
|
2023-02-04 15:22:43 +01:00
|
|
|
}
|
|
|
|
CargoCommand::Size {
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments,
|
|
|
|
} => {
|
|
|
|
let mut args = vec![
|
|
|
|
"+nightly",
|
|
|
|
self.name(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target,
|
|
|
|
"--features",
|
|
|
|
"test-critical-section",
|
|
|
|
];
|
|
|
|
if let Some(feature_name) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature_name]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
if let Some(Sizearguments::Other(arguments)) = arguments {
|
|
|
|
// Arguments to cargo size must be passed after "--"
|
|
|
|
args.extend_from_slice(&["--"]);
|
|
|
|
for arg in arguments {
|
|
|
|
args.extend_from_slice(&[arg.as_str()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn command(&self) -> &str {
|
2023-02-04 15:22:43 +01:00
|
|
|
"cargo"
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildMode {
|
2023-02-04 15:22:43 +01:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub fn to_flag(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
BuildMode::Release => Some("--release"),
|
|
|
|
BuildMode::Debug => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for BuildMode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let cmd = match self {
|
|
|
|
BuildMode::Release => "release",
|
|
|
|
BuildMode::Debug => "debug",
|
|
|
|
};
|
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
write!(f, "{cmd}")
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn run_command(command: &CargoCommand) -> anyhow::Result<RunResult> {
|
|
|
|
let (mut reader, writer) = pipe()?;
|
|
|
|
println!("👟 {} {}", command.command(), command.args().join(" "));
|
|
|
|
|
|
|
|
let mut handle = Command::new(command.command())
|
|
|
|
.args(command.args())
|
|
|
|
.stdout(writer)
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
// retrieve output and clean up
|
|
|
|
let mut output = String::new();
|
|
|
|
reader.read_to_string(&mut output)?;
|
|
|
|
let exit_status = handle.wait()?;
|
|
|
|
|
|
|
|
Ok(RunResult {
|
|
|
|
exit_status,
|
|
|
|
output,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:16:36 +01:00
|
|
|
/// Check if `run` was successful.
|
2021-08-26 10:58:59 +02:00
|
|
|
/// returns Ok in case the run went as expected,
|
|
|
|
/// Err otherwise
|
2021-09-22 13:22:45 +02:00
|
|
|
pub fn run_successful(run: &RunResult, expected_output_file: String) -> Result<(), TestRunError> {
|
|
|
|
let mut file_handle =
|
|
|
|
File::open(expected_output_file.clone()).map_err(|_| TestRunError::FileError {
|
|
|
|
file: expected_output_file.clone(),
|
|
|
|
})?;
|
2021-08-26 10:58:59 +02:00
|
|
|
let mut expected_output = String::new();
|
2021-09-22 13:22:45 +02:00
|
|
|
file_handle
|
|
|
|
.read_to_string(&mut expected_output)
|
|
|
|
.map_err(|_| TestRunError::FileError {
|
|
|
|
file: expected_output_file.clone(),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if expected_output != run.output {
|
|
|
|
Err(TestRunError::FileCmpError {
|
|
|
|
expected: expected_output.clone(),
|
|
|
|
got: run.output.clone(),
|
|
|
|
})
|
|
|
|
} else if !run.exit_status.success() {
|
|
|
|
Err(TestRunError::CommandError(run.clone()))
|
2021-08-26 10:58:59 +02:00
|
|
|
} else {
|
2021-09-22 13:22:45 +02:00
|
|
|
Ok(())
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|