aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/run/data.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2023-04-16 19:19:09 +0000
committerGitHub <noreply@github.com>2023-04-16 19:19:09 +0000
commit55083fb3ccee36c623c91b48ecc7d1f563ed80f8 (patch)
treeb2166e9a5f93646f7123e2eb323331bf9a4b2fa2 /xtask/src/run/data.rs
parent56bf829931cd3f8267ad435f6ff8f3ae200418b4 (diff)
parentb7e4498a7136041d89541bdc7725c8c023fa5c9c (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/run/data.rs')
-rw-r--r--xtask/src/run/data.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/xtask/src/run/data.rs b/xtask/src/run/data.rs
new file mode 100644
index 0000000..eacd72c
--- /dev/null
+++ b/xtask/src/run/data.rs
@@ -0,0 +1,87 @@
+use std::{
+ ffi::OsString,
+ process::{ExitStatus, Stdio},
+};
+
+use diffy::{create_patch, PatchFormatter};
+
+use crate::cargo_command::CargoCommand;
+
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub enum OutputMode {
+ PipedAndCollected,
+ Inherited,
+}
+
+impl From<OutputMode> for Stdio {
+ fn from(value: OutputMode) -> Self {
+ match value {
+ OutputMode::PipedAndCollected => Stdio::piped(),
+ OutputMode::Inherited => Stdio::inherit(),
+ }
+ }
+}
+
+#[derive(Debug, Clone)]
+pub struct RunResult {
+ pub exit_status: ExitStatus,
+ pub stdout: String,
+ pub stderr: String,
+}
+
+#[derive(Debug)]
+pub enum FinalRunResult<'c> {
+ Success(CargoCommand<'c>, RunResult),
+ Failed(CargoCommand<'c>, RunResult),
+ CommandError(CargoCommand<'c>, anyhow::Error),
+}
+
+#[derive(Debug)]
+pub enum TestRunError {
+ FileCmpError {
+ expected: String,
+ got: String,
+ },
+ FileError {
+ file: String,
+ },
+ PathConversionError(OsString),
+ CommandError(RunResult),
+ #[allow(dead_code)]
+ IncompatibleCommand,
+}
+
+impl core::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, e.stderr
+ )
+ }
+ 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 std::error::Error for TestRunError {}