2023-02-08 22:09:32 +01:00
|
|
|
use crate::{debug, RunResult, Sizearguments, TestRunError};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
|
|
|
use os_pipe::pipe;
|
2023-02-06 13:21:04 +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> {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci
|
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
example: &'a str,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2021-08-26 10:58:59 +02:00
|
|
|
example: &'a str,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleBuild {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2021-08-26 10:58:59 +02:00
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2023-02-05 01:50:29 +01:00
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode,
|
2023-02-05 01:50:29 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Build {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
package: Vec<String>,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
mode: BuildMode,
|
|
|
|
},
|
|
|
|
Check {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
package: Vec<String>,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
},
|
|
|
|
Clippy {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
package: Vec<String>,
|
|
|
|
target: &'a str,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
},
|
2023-02-24 00:10:01 +01:00
|
|
|
Format {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
package: Vec<String>,
|
|
|
|
check_only: bool,
|
|
|
|
},
|
2023-02-24 22:56:36 +01:00
|
|
|
Doc {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
features: Option<&'a str>,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-04 15:22:43 +01:00
|
|
|
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 {
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run",
|
|
|
|
CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check",
|
|
|
|
CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build",
|
|
|
|
CargoCommand::ExampleSize { .. } => "size",
|
|
|
|
CargoCommand::Clippy { .. } => "clippy",
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format { .. } => "fmt",
|
2023-02-24 22:56:36 +01:00
|
|
|
CargoCommand::Doc { .. } => "doc",
|
2023-02-06 13:21:04 +01:00
|
|
|
// TODO
|
|
|
|
// CargoCommand::Test { .. } => "test",
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn args(&self) -> Vec<&str> {
|
|
|
|
match self {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci, for now the same as Qemu
|
2021-08-26 10:58:59 +02:00
|
|
|
CargoCommand::Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2021-08-26 10:58:59 +02:00
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
2023-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
args.extend_from_slice(&[self.name(), "--example", example, "--target", target]);
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
example,
|
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-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
args.extend_from_slice(&[self.name(), "--example", example, "--target", target]);
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Build {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
package,
|
2023-02-05 01:50:29 +01:00
|
|
|
target,
|
|
|
|
features,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode,
|
2023-02-05 01:50:29 +01:00
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
|
|
|
|
args.extend_from_slice(&[self.name(), "--target", target]);
|
|
|
|
if !package.is_empty() {
|
|
|
|
for package in package {
|
|
|
|
args.extend_from_slice(&["--package", package]);
|
|
|
|
}
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
args.extend_from_slice(&[self.name(), "--target", target]);
|
|
|
|
if !package.is_empty() {
|
|
|
|
for package in package {
|
|
|
|
args.extend_from_slice(&["--package", package]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
|
|
|
CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
|
|
|
args.extend_from_slice(&[self.name(), "--target", target]);
|
|
|
|
if !package.is_empty() {
|
|
|
|
for package in package {
|
|
|
|
args.extend_from_slice(&["--package", package]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-24 22:56:36 +01:00
|
|
|
CargoCommand::Doc { cargoarg, features } => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
|
|
|
args.extend_from_slice(&[self.name()]);
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
check_only,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly", self.name()];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !package.is_empty() {
|
|
|
|
for package in package {
|
|
|
|
args.extend_from_slice(&["--package", package]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *check_only {
|
|
|
|
args.extend_from_slice(&["--check"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleBuild {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
args.extend_from_slice(&[self.name(), "--example", example, "--target", target]);
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
|
|
|
CargoCommand::ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
args.extend_from_slice(&[self.name(), "--example", example, "--target", target]);
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
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
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-04 15:22:43 +01:00
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments,
|
|
|
|
} => {
|
2023-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
args.extend_from_slice(&[self.name(), "--example", example, "--target", target]);
|
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
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()?;
|
2023-02-06 13:21:04 +01:00
|
|
|
let (mut error_reader, error_writer) = pipe()?;
|
2023-02-05 01:50:29 +01:00
|
|
|
debug!("👟 {} {}", command.command(), command.args().join(" "));
|
2021-08-26 10:58:59 +02:00
|
|
|
|
|
|
|
let mut handle = Command::new(command.command())
|
|
|
|
.args(command.args())
|
|
|
|
.stdout(writer)
|
2023-02-06 13:21:04 +01:00
|
|
|
.stderr(error_writer)
|
2021-08-26 10:58:59 +02:00
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
// retrieve output and clean up
|
2023-02-08 22:09:32 +01:00
|
|
|
let mut stdout = String::new();
|
|
|
|
reader.read_to_string(&mut stdout)?;
|
2021-08-26 10:58:59 +02:00
|
|
|
let exit_status = handle.wait()?;
|
|
|
|
|
2023-02-08 22:09:32 +01:00
|
|
|
let mut stderr = String::new();
|
|
|
|
error_reader.read_to_string(&mut stderr)?;
|
2023-02-06 13:21:04 +01:00
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
Ok(RunResult {
|
|
|
|
exit_status,
|
2023-02-08 22:09:32 +01:00
|
|
|
stdout,
|
|
|
|
stderr,
|
2021-08-26 10:58:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2023-02-05 01:50:29 +01:00
|
|
|
pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> {
|
2021-09-22 13:22:45 +02:00
|
|
|
let mut file_handle =
|
2023-02-05 01:50:29 +01:00
|
|
|
File::open(expected_output_file).map_err(|_| TestRunError::FileError {
|
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
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 {
|
2023-02-05 01:50:29 +01:00
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
|
|
|
|
2023-02-08 22:09:32 +01:00
|
|
|
if expected_output != run.stdout {
|
2021-09-22 13:22:45 +02:00
|
|
|
Err(TestRunError::FileCmpError {
|
|
|
|
expected: expected_output.clone(),
|
2023-02-08 22:09:32 +01:00
|
|
|
got: run.stdout.clone(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|