diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2023-04-16 19:19:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-16 19:19:09 +0000 |
| commit | 55083fb3ccee36c623c91b48ecc7d1f563ed80f8 (patch) | |
| tree | b2166e9a5f93646f7123e2eb323331bf9a4b2fa2 /xtask/src/main.rs | |
| parent | 56bf829931cd3f8267ad435f6ff8f3ae200418b4 (diff) | |
| parent | b7e4498a7136041d89541bdc7725c8c023fa5c9c (diff) | |
Merge #736
736: More `xtasks` and add examples to `rtic` repo r=korken89 a=datdenkikniet
This was in #732 before, but decluttering that PR seemed sensible
Co-authored-by: datdenkikniet <jcdra1@gmail.com>
Diffstat (limited to 'xtask/src/main.rs')
| -rw-r--r-- | xtask/src/main.rs | 156 |
1 files changed, 21 insertions, 135 deletions
diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 2bfe851..30c3da0 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,33 +1,19 @@ mod argument_parsing; mod build; -mod cargo_commands; -mod command; +mod cargo_command; +mod run; -use argument_parsing::{ExtraArguments, Globals, Package}; +use argument_parsing::ExtraArguments; use clap::Parser; -use command::OutputMode; use core::fmt; -use diffy::{create_patch, PatchFormatter}; -use std::{ - error::Error, - ffi::OsString, - fs::File, - io::prelude::*, - path::{Path, PathBuf}, - process::ExitStatus, - str, -}; +use std::{path::Path, str}; use log::{error, info, log_enabled, trace, Level}; use crate::{ argument_parsing::{Backends, BuildOrCheck, Cli, Commands}, build::init_build_dir, - cargo_commands::{ - build_and_check_size, cargo, cargo_book, cargo_clippy, cargo_doc, cargo_example, - cargo_format, cargo_test, run_test, - }, - command::{handle_results, run_command, run_successful, CargoCommand}, + run::*, }; #[derive(Debug, Clone, Copy)] @@ -69,56 +55,6 @@ const ARMV7M: Target = Target::new("thumbv7m-none-eabi", false); const ARMV8MBASE: Target = Target::new("thumbv8m.base-none-eabi", false); const ARMV8MMAIN: Target = Target::new("thumbv8m.main-none-eabi", false); -#[derive(Debug, Clone)] -pub struct RunResult { - exit_status: ExitStatus, - stdout: String, - stderr: String, -} - -#[derive(Debug)] -pub enum TestRunError { - FileCmpError { expected: String, got: String }, - FileError { file: String }, - PathConversionError(OsString), - CommandError(RunResult), - IncompatibleCommand, -} -impl fmt::Display for TestRunError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TestRunError::FileCmpError { expected, got } => { - let patch = create_patch(expected, got); - writeln!(f, "Differing output in files.\n")?; - let pf = PatchFormatter::new().with_color(); - writeln!(f, "{}", pf.fmt_patch(&patch))?; - write!( - f, - "See flag --overwrite-expected to create/update expected output." - ) - } - TestRunError::FileError { file } => { - write!(f, "File error on: {file}\nSee flag --overwrite-expected to create/update expected output.") - } - TestRunError::CommandError(e) => { - write!( - f, - "Command failed with exit status {}: {}", - e.exit_status, e.stdout - ) - } - TestRunError::PathConversionError(p) => { - write!(f, "Can't convert path from `OsString` to `String`: {p:?}") - } - TestRunError::IncompatibleCommand => { - write!(f, "Can't run that command in this context") - } - } - } -} - -impl Error for TestRunError {} - fn main() -> anyhow::Result<()> { // if there's an `xtask` folder, we're *probably* at the root of this repo (we can't just // check the name of `env::current_dir()` because people might clone it into a different name) @@ -152,6 +88,12 @@ fn main() -> anyhow::Result<()> { trace!("default logging level: {0}", globals.verbose); + log::debug!( + "Stderr of child processes is inherited: {}", + globals.stderr_inherited + ); + log::debug!("Partial features: {}", globals.partial); + let backend = if let Some(backend) = globals.backend { backend } else { @@ -265,7 +207,7 @@ fn main() -> anyhow::Result<()> { Commands::Qemu(args) | Commands::Run(args) => { // x86_64 target not valid info!("Testing for backend: {backend:?}"); - run_test( + qemu_run_examples( globals, &cargologlevel, backend, @@ -285,71 +227,15 @@ fn main() -> anyhow::Result<()> { info!("Running mdbook"); cargo_book(globals, &args.arguments) } - }; - - handle_results(globals, final_run_results) -} - -// run example binary `example` -fn command_parser( - glob: &Globals, - command: &CargoCommand, - overwrite: bool, -) -> anyhow::Result<RunResult> { - let output_mode = if glob.stderr_inherited { - OutputMode::Inherited - } else { - OutputMode::PipedAndCollected - }; - - match *command { - CargoCommand::Qemu { example, .. } | CargoCommand::Run { example, .. } => { - let run_file = format!("{example}.run"); - let expected_output_file = ["rtic", "ci", "expected", &run_file] - .iter() - .collect::<PathBuf>() - .into_os_string() - .into_string() - .map_err(TestRunError::PathConversionError)?; - - // cargo run <..> - info!("Running example: {example}"); - let cargo_run_result = run_command(command, output_mode)?; - info!("{}", cargo_run_result.stdout); - - // Create a file for the expected output if it does not exist or mismatches - if overwrite { - let result = run_successful(&cargo_run_result, &expected_output_file); - if let Err(e) = result { - // FileError means the file did not exist or was unreadable - error!("Error: {e}"); - let mut file_handle = File::create(&expected_output_file).map_err(|_| { - TestRunError::FileError { - file: expected_output_file.clone(), - } - })?; - info!("Flag --overwrite-expected enabled"); - info!("Creating/updating file: {expected_output_file}"); - file_handle.write_all(cargo_run_result.stdout.as_bytes())?; - }; - } else { - run_successful(&cargo_run_result, &expected_output_file)?; - }; - - Ok(cargo_run_result) + Commands::UsageExampleCheck(examples) => { + info!("Checking usage examples"); + cargo_usage_example(globals, BuildOrCheck::Check, examples.examples()?) } - CargoCommand::Format { .. } - | CargoCommand::ExampleCheck { .. } - | CargoCommand::ExampleBuild { .. } - | CargoCommand::Check { .. } - | CargoCommand::Build { .. } - | CargoCommand::Clippy { .. } - | CargoCommand::Doc { .. } - | CargoCommand::Test { .. } - | CargoCommand::Book { .. } - | CargoCommand::ExampleSize { .. } => { - let cargo_result = run_command(command, output_mode)?; - Ok(cargo_result) + Commands::UsageExampleBuild(examples) => { + info!("Building usage examples"); + cargo_usage_example(globals, BuildOrCheck::Build, examples.examples()?) } - } + }; + + handle_results(globals, final_run_results).map_err(|_| anyhow::anyhow!("Commands failed")) } |
