use super::*;
struct StubGit {
invocations: RefCell<Vec<Vec<String>>>,
fail_at: usize,
diff: Option<String>,
}
impl StubGit {
fn failing_at(idx: usize) -> Self {
Self {
invocations: RefCell::new(Vec::new()),
fail_at: idx,
diff: Some(String::new()),
}
}
fn with_filtered(idx: usize, paths: &str) -> Self {
Self {
diff: Some(paths.into()),
..Self::failing_at(idx)
}
}
fn diff_capture_failing() -> Self {
Self {
diff: None,
..Self::failing_at(usize::MAX)
}
}
}
impl GitRunner for StubGit {
fn run(&self, _dest: &Path, args: &[&str]) -> std::io::Result<()> {
let idx = self.invocations.borrow().len();
self.invocations
.borrow_mut()
.push(args.iter().map(|s| (*s).to_owned()).collect());
if idx == self.fail_at {
Err(std::io::Error::other("stub fail"))
} else {
Ok(())
}
}
fn run_capture(&self, _dest: &Path, args: &[&str]) -> std::io::Result<String> {
if args.first() == Some(&"diff") {
return self
.diff
.clone()
.ok_or_else(|| std::io::Error::other("stub fail"));
}
Ok("deadbeefsha".into())
}
}
#[test]
fn add_failure_surfaces_as_git_error() {
let err = merge(&PathBuf::from("/x"), "p1-cmp", &StubGit::failing_at(1)).unwrap_err();
assert_git_op(err, "compaction merge add");
}
#[test]
fn commit_failure_surfaces_as_git_error() {
let err = merge(&PathBuf::from("/x"), "p1-cmp", &StubGit::failing_at(2)).unwrap_err();
assert_git_op(err, "compaction merge commit");
}
#[test]
fn filter_diff_failure_surfaces_as_git_error() {
let err = merge(
&PathBuf::from("/x"),
"p1-cmp",
&StubGit::diff_capture_failing(),
)
.unwrap_err();
assert_git_op(err, "compaction merge filter");
}
#[test]
fn filter_restore_failure_surfaces_as_git_error() {
let stub = StubGit::with_filtered(2, "messages/003-goal.md\n");
let err = merge(&PathBuf::from("/x"), "p1-cmp", &stub).unwrap_err();
assert_git_op(err, "compaction merge filter");
}