23 lines
1.0 KiB
Rust
23 lines
1.0 KiB
Rust
//! Shared test helper for driving `git` as a subprocess in unit tests.
|
|
//!
|
|
//! `Command::output()` resolves to `Ok` even when the spawned process exits
|
|
//! non-zero — a bare `.expect(...)`/`.unwrap()` on that `Output` only
|
|
//! checks that the process could be spawned, not that git itself
|
|
//! succeeded. A failed `git commit` (e.g. missing `user.name`/`user.email`
|
|
//! identity) then silently leaves the repo without the commit the rest of
|
|
//! the test assumes exists, surfacing later as a confusing, unrelated
|
|
//! assertion failure instead of the real git error.
|
|
|
|
use std::io;
|
|
use std::process::Output;
|
|
|
|
/// Unwrap a `git` subprocess result, panicking with `context` and git's
|
|
/// stderr if the process failed to spawn or exited non-zero.
|
|
pub(crate) fn git_ok(output: io::Result<Output>, context: &str) -> Output {
|
|
let output = output.unwrap_or_else(|e| panic!("{context}: failed to run git: {e}"));
|
|
if !output.status.success() {
|
|
panic!("{context}: {}", String::from_utf8_lossy(&output.stderr));
|
|
}
|
|
output
|
|
}
|