use super::*;
use crate::test_support::{SharedSink, fixture, remove};
use std::time::Duration;
fn quick() -> Cadence {
return Cadence::from_interval(Duration::from_millis(40));
}
#[test]
fn a_free_index_is_not_waited_for() {
let root: std::path::PathBuf = fixture("holder-wait-free");
let sink: SharedSink = SharedSink::default();
let outcome: IndexWait = wait_for_index_release(root.join("index").as_path(), "merge", Duration::from_secs(30), quick(), &mut sink.clone());
assert_eq!(outcome, IndexWait::NotHeld);
assert_eq!(sink.text(), "");
remove(root.as_path());
}
#[test]
fn a_released_lock_ends_the_wait_after_a_notice() {
let root: std::path::PathBuf = fixture("holder-wait-released");
let index: std::path::PathBuf = root.join("index");
let lock: std::path::PathBuf = root.join("index.lock");
std::fs::write(&lock, b"").expect("the other program holds the lock");
let sink: SharedSink = SharedSink::default();
let remover: lock_remover::Remover = lock_remover::Remover::new(lock.clone());
let mut writer = crate::test_support::ActionSink::new(&sink, 1, move || { remover.remove(); });
let outcome: IndexWait = wait_for_index_release(index.as_path(), "merge", Duration::from_secs(60), quick(), &mut writer);
assert!(matches!(outcome, IndexWait::Released(_)), "{outcome:?}");
let text: String = sink.text();
assert!(text.contains("git merge has changed nothing yet and waits for the index lock"), "{text}");
assert!(text.contains("index.lock"), "the lock is named: {text}");
assert!(!lock.exists());
remove(root.as_path());
}
#[test]
fn a_lock_held_at_the_limit_is_left_in_place() {
let root: std::path::PathBuf = fixture("holder-wait-limit");
let index: std::path::PathBuf = root.join("index");
let lock: std::path::PathBuf = root.join("index.lock");
std::fs::write(&lock, b"").expect("the other program holds the lock");
let sink: SharedSink = SharedSink::default();
let outcome: IndexWait = wait_for_index_release(index.as_path(), "merge", Duration::from_millis(200), quick(), &mut sink.clone());
assert!(matches!(outcome, IndexWait::GaveUp(waited) if waited >= Duration::from_millis(200)), "{outcome:?}");
assert!(lock.exists(), "the wait removes no lock");
assert!(sink.text().contains("nothing was changed and the lock was left in place"), "{}", sink.text());
remove(root.as_path());
}
mod lock_remover {
pub(super) struct Remover(std::path::PathBuf);
impl Remover {
pub(super) fn new(path: std::path::PathBuf) -> Self {
return Self(path);
}
pub(super) fn remove(&self) {
std::fs::remove_file(&self.0).expect("the other program lets go");
}
}
}