Skip to content

Commit

Permalink
snapshot tests: save captured stdout/err even on an Err variant (#881)
Browse files Browse the repository at this point in the history
output captured stdout/err even on Err return
  • Loading branch information
suaviloquence authored Aug 22, 2024
1 parent 74635ee commit 5b35f9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/snapshot_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ impl Write for StaticWriter {
/// Struct for printing the result of an invocation of `cargo-semver-checks`
#[derive(Debug)]
struct CommandOutput {
/// Whether the invocation of `cargo-semver-checks` was successful (i.e., there are no semver-breaking changes),
/// from [`Report::success`](cargo_semver_checks::Report::success).
success: bool,
/// The stderr of the invocation.
stderr: String,
/// The stdout of the invocation.
Expand All @@ -99,7 +96,6 @@ struct CommandOutput {

impl fmt::Display for CommandOutput {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "success: {}", self.success)?;
writeln!(f, "--- stdout ---\n{}", self.stdout)?;
writeln!(f, "--- stderr ---\n{}", self.stderr)?;

Expand All @@ -108,14 +104,23 @@ impl fmt::Display for CommandOutput {
}

#[derive(Debug)]
struct CommandResult(anyhow::Result<CommandOutput>);
struct CommandResult {
/// Whether the invocation of `cargo-semver-checks` was successful (i.e., there are no semver-breaking changes),
/// from [`Report::success`](cargo_semver_checks::Report::success), or an `Err` if `cargo-semver-checks` exited
/// early with an `Err` variant.
result: anyhow::Result<bool>,
/// Captured `stdout` and `stderr` for the command run, regardless of whether it was successful.
output: CommandOutput,
}

impl fmt::Display for CommandResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Ok(d) => write!(f, "{d}"),
Err(e) => writeln!(f, "--- error ---\n{e}"),
}
match &self.result {
Ok(success) => writeln!(f, "success: {success}")?,
Err(e) => writeln!(f, "--- error ---\n{e}")?,
};

write!(f, "{}", self.output)
}
}

Expand Down Expand Up @@ -180,11 +185,13 @@ fn assert_integration_test(test_name: &str, invocation: &[&str]) {
.try_into_inner()
.expect("failed to get unique reference to stderr");

let result = CommandResult(result.map(|report| CommandOutput {
success: report.success(),
stdout: String::from_utf8(stdout).expect("failed to convert to UTF-8"),
stderr: String::from_utf8(stderr).expect("failed to convert to UTF-8"),
}));
let stdout = String::from_utf8(stdout).expect("failed to convert to UTF-8");
let stderr = String::from_utf8(stderr).expect("failed to convert to UTF-8");

let result = CommandResult {
result: result.map(|report| report.success()),
output: CommandOutput { stderr, stdout },
};

insta::assert_snapshot!(format!("{test_name}-output"), result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ expression: result
no crates with library targets selected, nothing to semver-check
note: only library targets contain an API surface that can be checked for semver
note: skipped the following crates since they have no library target: bin_only
--- stdout ---

--- stderr ---

0 comments on commit 5b35f9f

Please sign in to comment.