aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/command.rs
diff options
context:
space:
mode:
authorGravatar bors[bot] <26634292+bors[bot]@users.noreply.github.com> 2021-09-21 13:00:12 +0000
committerGravatar GitHub <noreply@github.com> 2021-09-21 13:00:12 +0000
commitc8621d78b9b1c0c67dff31404ade873a9d7b426e (patch)
treea958fa60fbeafedb7d8578c47fcaf506722e316f /xtask/src/command.rs
parentbf9df9fe73e9c1442a7a31ae93a91e7a8288f6f3 (diff)
parent7f45254e3939af5aa940c65e52c63fa83b93c16d (diff)
downloadrtic-c8621d78b9b1c0c67dff31404ade873a9d7b426e.tar.gz
rtic-c8621d78b9b1c0c67dff31404ade873a9d7b426e.tar.zst
rtic-c8621d78b9b1c0c67dff31404ade873a9d7b426e.zip
Merge #526
526: implement run-pass tests as xtasks r=korken89 a=Lotterleben resolves https://github.com/rtic-rs/cortex-m-rtic/issues/499 . With this PR, you should be able to run `cargo xtask --target <desired target>` or `cargo xtask --target all` locally. Of course, it also reconfigures the CI workflow to do the same. Note that I've translated the old `Run-pass tests` verbatim for now, which means the code includes checks for a `"types"`example which doesn't exist anymore. The examples could be collected much more nicely to prevent leftovers like this in the future, but imo that could also be achieved in a separate PR. Co-authored-by: Lotte Steenbrink <lotte.steenbrink@ferrous-systems.com>
Diffstat (limited to 'xtask/src/command.rs')
-rw-r--r--xtask/src/command.rs162
1 files changed, 162 insertions, 0 deletions
diff --git a/xtask/src/command.rs b/xtask/src/command.rs
new file mode 100644
index 00000000..8bf49849
--- /dev/null
+++ b/xtask/src/command.rs
@@ -0,0 +1,162 @@
+use crate::RunResult;
+use core::fmt;
+use os_pipe::pipe;
+use std::{fs::File, io::Read, path::Path, process::Command};
+
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum BuildMode {
+ Release,
+ Debug,
+}
+
+pub enum CargoCommand<'a> {
+ Run {
+ example: &'a str,
+ target: &'a str,
+ features: Option<&'a str>,
+ mode: BuildMode,
+ },
+ Build {
+ example: &'a str,
+ target: &'a str,
+ features: Option<&'a str>,
+ mode: BuildMode,
+ },
+ Objcopy {
+ example: &'a str,
+ target: &'a str,
+ features: Option<&'a str>,
+ ihex: &'a str,
+ },
+ Size {
+ example_paths: Vec<&'a Path>,
+ },
+ Clean,
+}
+
+impl<'a> CargoCommand<'a> {
+ fn name(&self) -> &str {
+ match self {
+ CargoCommand::Run { .. } => "run",
+ CargoCommand::Size { example_paths: _ } => "rust-size",
+ CargoCommand::Clean => "clean",
+ CargoCommand::Build { .. } => "build",
+ CargoCommand::Objcopy { .. } => "objcopy",
+ }
+ }
+
+ pub fn args(&self) -> Vec<&str> {
+ match self {
+ CargoCommand::Run {
+ example,
+ target,
+ features,
+ mode,
+ }
+ | CargoCommand::Build {
+ example,
+ target,
+ features,
+ mode,
+ } => {
+ let mut args = vec![self.name(), "--example", example, "--target", target];
+
+ if let Some(feature_name) = features {
+ args.extend_from_slice(&["--features", feature_name]);
+ }
+ if let Some(flag) = mode.to_flag() {
+ args.push(flag);
+ }
+ args
+ }
+ CargoCommand::Size { example_paths } => {
+ example_paths.iter().map(|p| p.to_str().unwrap()).collect()
+ }
+ CargoCommand::Clean => vec!["clean"],
+ CargoCommand::Objcopy {
+ example,
+ target,
+ features,
+ ihex,
+ } => {
+ let mut args = vec![self.name(), "--example", example, "--target", target];
+
+ if let Some(feature_name) = features {
+ args.extend_from_slice(&["--features", feature_name]);
+ }
+
+ // this always needs to go at the end
+ args.extend_from_slice(&["--", "-O", "ihex", ihex]);
+ args
+ }
+ }
+ }
+
+ 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 :)
+ CargoCommand::Size { .. } => "rust-size",
+ _ => "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
+pub fn run_successful(run: &RunResult, expected_output_file: String) -> anyhow::Result<()> {
+ let mut file_handle = File::open(expected_output_file)?;
+ let mut expected_output = String::new();
+ file_handle.read_to_string(&mut expected_output)?;
+ if expected_output == run.output && run.exit_status.success() {
+ Ok(())
+ } else {
+ Err(anyhow::anyhow!(
+ "Run failed with exit status {}: {}",
+ run.exit_status,
+ run.output
+ ))
+ }
+}
'streaming'>streaming Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/packages/astro/test/fixtures (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2021-11-19Add Debug tests (#1882)Gravatar Matthew Phillips 4-0/+62
2021-11-19[ci] collect statsGravatar FredKSchott 1-0/+1
2021-11-19Version Packages (next) (#1881)astro@0.21.0-next.8@astrojs/renderer-vue@0.2.0-next.2Gravatar github-actions[bot] 25-23/+42
2021-11-18Improve HMR (#1896)Gravatar Drew Powers 2-10/+18
2021-11-18update depsGravatar Fred K. Schott 5-352/+65
2021-11-18fix #1778Gravatar Fred K. Schott 2-1/+8
2021-11-18Update compiler (#1869)Gravatar Nate Moore 3-11/+6
2021-11-18remove unused remark dependency (#1894)Gravatar Fred K. Schott 2-245/+13
2021-11-18Improve error messages (#1875)Gravatar Drew Powers 39-61/+448
2021-11-18pin astro compiler to older versionGravatar Fred K. Schott 2-5/+5
2021-11-18Update yarn.lock to reflect the state of the package.json files (#1892)Gravatar Jonathan Neal 5-1300/+1000
2021-11-19[ci] yarn formatGravatar FredKSchott 1-5/+3