rtic/xtask/src/command.rs

165 lines
4.5 KiB
Rust
Raw Normal View History

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,
},
2021-12-26 10:43:57 +01:00
// Size {
// example_paths: Vec<&'a Path>,
// },
// Clean,
2021-08-26 10:58:59 +02:00
}
impl<'a> CargoCommand<'a> {
fn name(&self) -> &str {
match self {
CargoCommand::Run { .. } => "run",
2021-12-26 10:43:57 +01:00
// CargoCommand::Size { example_paths: _ } => "rust-size",
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,
} => {
let mut args = vec![
2023-01-02 14:34:05 +01:00
"+nightly",
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-02 14:34:05 +01:00
let mut args = vec!["+nightly", self.name(), "--examples", "--target", target];
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
2021-12-26 10:43:57 +01:00
} // CargoCommand::Size { example_paths } => {
// example_paths.iter().map(|p| p.to_str().unwrap()).collect()
// }
2021-08-26 10:58:59 +02:00
}
}
pub fn command(&self) -> &str {
match self {
// we need to cheat a little here:
// `cargo size` can't be ran on multiple files, so we're using `rust-size` instead
// which isn't a command that starts wizh `cargo`. So we're sneakily swapping them out :)
2021-12-26 10:43:57 +01:00
// CargoCommand::Size { .. } => "rust-size",
2021-08-26 10:58:59 +02:00
_ => "cargo",
}
}
}
impl BuildMode {
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",
};
write!(f, "{}", cmd)
}
}
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,
})
}
/// Check if `run` was sucessful.
/// 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
}
}