Use chdir() instead of unstable option, also confirm whenver a command succeeds because

why not
This commit is contained in:
datdenkikniet 2023-04-15 23:22:00 +02:00
parent deeb3877f0
commit 9dc9f49263
3 changed files with 63 additions and 35 deletions

View file

@ -326,7 +326,7 @@ impl UsageExamplesOpt {
let usage_examples: Vec<_> = std::fs::read_dir("./examples")? let usage_examples: Vec<_> = std::fs::read_dir("./examples")?
.filter_map(Result::ok) .filter_map(Result::ok)
.filter(|p| p.metadata().ok().map(|p| p.is_dir()).unwrap_or(false)) .filter(|p| p.metadata().ok().map(|p| p.is_dir()).unwrap_or(false))
.filter_map(|p| p.file_name().as_os_str().to_str().map(ToString::to_string)) .filter_map(|p| p.file_name().to_str().map(ToString::to_string))
.collect(); .collect();
let selected_examples: Option<Vec<String>> = self let selected_examples: Option<Vec<String>> = self

View file

@ -1,3 +1,5 @@
use std::path::PathBuf;
use crate::{ use crate::{
argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata}, argument_parsing::{Backends, BuildOrCheck, ExtraArguments, Globals, PackageOpt, TestMetadata},
command::{BuildMode, CargoCommand}, command::{BuildMode, CargoCommand},
@ -186,6 +188,7 @@ pub fn cargo_example<'c>(
target: backend.to_target(), target: backend.to_target(),
features, features,
mode: BuildMode::Release, mode: BuildMode::Release,
dir: Some(PathBuf::from("./rtic")),
}, },
}; };
(globals, command, false) (globals, command, false)
@ -320,6 +323,7 @@ pub fn qemu_run_examples<'c>(
target, target,
features: features.clone(), features: features.clone(),
mode: BuildMode::Release, mode: BuildMode::Release,
dir: Some(PathBuf::from("./rtic")),
}; };
let cmd_qemu = CargoCommand::Qemu { let cmd_qemu = CargoCommand::Qemu {
@ -328,6 +332,7 @@ pub fn qemu_run_examples<'c>(
target, target,
features: features.clone(), features: features.clone(),
mode: BuildMode::Release, mode: BuildMode::Release,
dir: Some(PathBuf::from("./rtic")),
}; };
#[cfg(not(feature = "rayon"))] #[cfg(not(feature = "rayon"))]
@ -363,6 +368,7 @@ pub fn build_and_check_size<'c>(
target, target,
features: features.clone(), features: features.clone(),
mode: BuildMode::Release, mode: BuildMode::Release,
dir: Some(PathBuf::from("./rtic")),
}; };
if let Err(err) = command_parser(globals, &cmd, false) { if let Err(err) = command_parser(globals, &cmd, false) {
error!("{err}"); error!("{err}");
@ -375,6 +381,7 @@ pub fn build_and_check_size<'c>(
features: features.clone(), features: features.clone(),
mode: BuildMode::Release, mode: BuildMode::Release,
arguments: arguments.clone(), arguments: arguments.clone(),
dir: Some(PathBuf::from("./rtic")),
}; };
(globals, cmd, false) (globals, cmd, false)
}); });

View file

@ -51,6 +51,7 @@ pub enum CargoCommand<'a> {
target: Target<'a>, target: Target<'a>,
features: Option<String>, features: Option<String>,
mode: BuildMode, mode: BuildMode,
dir: Option<PathBuf>,
}, },
ExampleBuild { ExampleBuild {
cargoarg: &'a Option<&'a str>, cargoarg: &'a Option<&'a str>,
@ -58,6 +59,7 @@ pub enum CargoCommand<'a> {
target: Target<'a>, target: Target<'a>,
features: Option<String>, features: Option<String>,
mode: BuildMode, mode: BuildMode,
dir: Option<PathBuf>,
}, },
ExampleCheck { ExampleCheck {
cargoarg: &'a Option<&'a str>, cargoarg: &'a Option<&'a str>,
@ -111,6 +113,7 @@ pub enum CargoCommand<'a> {
features: Option<String>, features: Option<String>,
mode: BuildMode, mode: BuildMode,
arguments: Option<ExtraArguments>, arguments: Option<ExtraArguments>,
dir: Option<PathBuf>,
}, },
CheckInDir { CheckInDir {
mode: BuildMode, mode: BuildMode,
@ -179,22 +182,32 @@ impl core::fmt::Display for CargoCommand<'_> {
target, target,
features, features,
mode, mode,
} => write!( dir,
f, } => {
"Run example {example} in QEMU {}", let details = details(target, mode, features, cargoarg);
details(target, mode, features, cargoarg) if let Some(dir) = dir {
), let dir = dir.to_str().unwrap_or("Not displayable");
write!(f, "Run example {example} in QEMU from {dir} {details}",)
} else {
write!(f, "Run example {example} in QEMU {details}",)
}
}
CargoCommand::ExampleBuild { CargoCommand::ExampleBuild {
cargoarg, cargoarg,
example, example,
target, target,
features, features,
mode, mode,
} => write!( dir,
f, } => {
"Build example {example} {}", let details = details(target, mode, features, cargoarg);
details(target, mode, features, cargoarg) if let Some(dir) = dir {
), let dir = dir.to_str().unwrap_or("Not displayable");
write!(f, "Build example {example} in {dir} {details}")
} else {
write!(f, "Build example {example} {details}",)
}
}
CargoCommand::ExampleCheck { CargoCommand::ExampleCheck {
cargoarg, cargoarg,
example, example,
@ -221,7 +234,7 @@ impl core::fmt::Display for CargoCommand<'_> {
) )
} }
CargoCommand::BuildInDir { mode, dir } => { CargoCommand::BuildInDir { mode, dir } => {
let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); let dir = dir.to_str().unwrap_or("Not displayable");
write!(f, "Build {dir} ({mode})") write!(f, "Build {dir} ({mode})")
} }
CargoCommand::Check { CargoCommand::Check {
@ -239,7 +252,7 @@ impl core::fmt::Display for CargoCommand<'_> {
) )
} }
CargoCommand::CheckInDir { mode, dir } => { CargoCommand::CheckInDir { mode, dir } => {
let dir = dir.as_os_str().to_str().unwrap_or("Not displayable"); let dir = dir.to_str().unwrap_or("Not displayable");
write!(f, "Check {dir} ({mode})") write!(f, "Check {dir} ({mode})")
} }
CargoCommand::Clippy { CargoCommand::Clippy {
@ -315,12 +328,15 @@ impl core::fmt::Display for CargoCommand<'_> {
features, features,
mode, mode,
arguments: _, arguments: _,
dir,
} => { } => {
write!( let details = details(target, mode, features, cargoarg);
f, if let Some(dir) = dir {
"Compute size of example {example} {}", let dir = dir.to_str().unwrap_or("Not displayable");
details(target, mode, features, cargoarg) write!(f, "Compute size of example {example} from {dir} {details}",)
) } else {
write!(f, "Compute size of example {example} {details}")
}
} }
} }
} }
@ -328,9 +344,15 @@ impl core::fmt::Display for CargoCommand<'_> {
impl<'a> CargoCommand<'a> { impl<'a> CargoCommand<'a> {
pub fn as_cmd_string(&self) -> String { pub fn as_cmd_string(&self) -> String {
let cd = if let Some(Some(chdir)) = self.chdir().map(|p| p.to_str()) {
format!("cd {chdir} && ")
} else {
format!("")
};
let executable = self.executable(); let executable = self.executable();
let args = self.args().join(" "); let args = self.args().join(" ");
format!("{executable} {args}") format!("{cd}{executable} {args}")
} }
fn command(&self) -> &'static str { fn command(&self) -> &'static str {
@ -406,6 +428,8 @@ impl<'a> CargoCommand<'a> {
target, target,
features, features,
mode, mode,
// Dir is exposed through chdir instead
dir: _,
} => { } => {
let mut args = vec!["+nightly"]; let mut args = vec!["+nightly"];
@ -413,10 +437,6 @@ impl<'a> CargoCommand<'a> {
args.extend_from_slice(&[cargoarg]); args.extend_from_slice(&[cargoarg]);
} }
// We need to be in the `rtic` directory to pick up
// the correct .cargo/config.toml file
args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]);
args.extend_from_slice(&[ args.extend_from_slice(&[
self.command(), self.command(),
"--example", "--example",
@ -588,16 +608,14 @@ impl<'a> CargoCommand<'a> {
target, target,
features, features,
mode, mode,
// Dir is exposed through chdir instead
dir: _,
} => { } => {
let mut args = vec!["+nightly"]; let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg { if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]); args.extend_from_slice(&[cargoarg]);
} }
// We need to be in the `rtic` directory to pick up
// the correct .cargo/config.toml file
args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]);
args.extend_from_slice(&[ args.extend_from_slice(&[
self.command(), self.command(),
"--example", "--example",
@ -648,16 +666,14 @@ impl<'a> CargoCommand<'a> {
features, features,
mode, mode,
arguments, arguments,
// Dir is exposed through chdir instead
dir: _,
} => { } => {
let mut args = vec!["+nightly"]; let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg { if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]); args.extend_from_slice(&[cargoarg]);
} }
// We need to be in the `rtic` directory to pick up
// the correct .cargo/config.toml file
args.extend_from_slice(&["-Z", "unstable-options", "-C", "rtic"]);
args.extend_from_slice(&[ args.extend_from_slice(&[
self.command(), self.command(),
"--example", "--example",
@ -708,6 +724,9 @@ impl<'a> CargoCommand<'a> {
CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => { CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => {
Some(dir) Some(dir)
} }
CargoCommand::Qemu { dir, .. }
| CargoCommand::ExampleBuild { dir, .. }
| CargoCommand::ExampleSize { dir, .. } => dir.as_ref(),
_ => None, _ => None,
} }
} }
@ -752,7 +771,7 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R
.stderr(stderr_mode); .stderr(stderr_mode);
if let Some(dir) = command.chdir() { if let Some(dir) = command.chdir() {
process.current_dir(dir); process.current_dir(dir.canonicalize()?);
} }
let result = process.output()?; let result = process.output()?;
@ -765,7 +784,9 @@ pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::R
log::info!("\n{}", stdout); log::info!("\n{}", stdout);
} }
if !exit_status.success() { if exit_status.success() {
log::info!("✅ Success.")
} else {
log::error!("❌ Command failed. Run to completion for the summary."); log::error!("❌ Command failed. Run to completion for the summary.");
} }
@ -843,7 +864,7 @@ pub fn handle_results(globals: &Globals, results: Vec<FinalRunResult>) -> Result
successes.for_each(|(cmd, stdout, stderr)| { successes.for_each(|(cmd, stdout, stderr)| {
let path = if let Some(dir) = cmd.chdir() { let path = if let Some(dir) = cmd.chdir() {
let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); let path = dir.to_str().unwrap_or("Not displayable");
format!(" (in {path}") format!(" (in {path}")
} else { } else {
format!("") format!("")
@ -860,7 +881,7 @@ pub fn handle_results(globals: &Globals, results: Vec<FinalRunResult>) -> Result
errors.clone().for_each(|(cmd, stdout, stderr)| { errors.clone().for_each(|(cmd, stdout, stderr)| {
if let Some(dir) = cmd.chdir() { if let Some(dir) = cmd.chdir() {
let path = dir.as_os_str().to_str().unwrap_or("Not displayable"); let path = dir.to_str().unwrap_or("Not displayable");
error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string()); error!("❌ Failed: {cmd} (in {path}) \n {}", cmd.as_cmd_string());
} else { } else {
error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string()); error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string());