oakum 0.12.1

A polyglot release tool that derives dependent version bumps from the dependency graph
Documentation
//! An in-process stand-in for the git child.
//!
//! Two things need it. The rules in [`super`] classify answers a real repository
//! is awkward to produce on demand — a look that warns while printing nothing, a
//! wrapper that exits 1 with a diagnostic, a child a signal killed — and the
//! shell shims that produced them in `tests/it/check/` run on unix only. And a
//! caller that gates one read on another is only shown to gate by observing that
//! the second child never ran, which a real repository cannot report.
//!
//! It stands in at the spawn, so what [`super::Git::child`] decides before one
//! — refusing an unreadable transport, reading a remote's listing, saying its
//! notes — runs against the script. The notes are heard here rather than
//! written to stderr, where a test could not read them back.

use std::sync::Mutex;

use super::env::{BatchSsh, RemoteFailure, TransportUnknown};
use super::Reply;

/// An answer and the operation that earns it, named by [`super::OpShape::name`].
/// An `Err` is a child that produced no reply, classified as a real one is.
struct Scripted {
    op: &'static str,
    outcome: Result<Reply, RemoteFailure>,
}

/// Answers keyed by operation rather than by position.
///
/// Keying matters twice over. A positional queue answers whatever is asked, so
/// a caller that spawns the wrong child still gets a reply and the test still
/// passes. And position makes every answer an obligation: a test cannot script
/// the child a gate is meant to prevent, because the gate holding would shift
/// every later answer onto the wrong read. Keyed, an unclaimed answer is one
/// the caller was right not to ask for — which is why nothing here complains
/// about leftovers. What a caller did ask for is [`Fake::asked`], which a test
/// asserts on directly.
///
/// The key is the operation's name, not its argv. An argv key would put git's
/// flags in every caller's test, where a changed flag breaks a test that has
/// nothing to do with it, and matching one would need rules about how much of a
/// command line counts. The names are distinct, stable, and already the phrase
/// a failure quotes, so a script reads as the diagnostics it stands for.
///
/// `Mutex` rather than `RefCell` so a scripted [`super::Git`] stays `Sync` like
/// the shipping one, instead of changing shape under `cfg(test)`.
pub(super) struct Fake {
    /// What the probe would have resolved. Held here rather than seeded into
    /// [`super::Git`]'s cache so no scripted `Git` can exist without one and
    /// fall through to a real `git config` probe.
    transport: Result<BatchSsh, TransportUnknown>,
    scripted: Mutex<Vec<Option<Scripted>>>,
    asked: Mutex<Vec<String>>,
    heard: Mutex<Vec<String>>,
}

impl Fake {
    pub(super) fn answering(
        transport: Result<BatchSsh, TransportUnknown>,
        outcomes: impl IntoIterator<Item = (&'static str, Result<Reply, RemoteFailure>)>,
    ) -> Self {
        Self {
            transport,
            scripted: Mutex::new(
                outcomes
                    .into_iter()
                    .map(|(op, outcome)| Some(Scripted { op, outcome }))
                    .collect(),
            ),
            asked: Mutex::new(Vec::new()),
            heard: Mutex::new(Vec::new()),
        }
    }

    pub(super) fn transport(&self) -> Result<&BatchSsh, &TransportUnknown> {
        self.transport.as_ref()
    }

    /// The first unclaimed answer scripted for this operation. Repeating one
    /// scripts successive answers for it, first written answered first.
    ///
    /// # Panics
    ///
    /// When nothing scripted matches. A caller asking for a child the test never
    /// named is the finding, so it is reported rather than defaulted away.
    pub(super) fn answer(&self, op: &'static str) -> Result<Reply, RemoteFailure> {
        self.asked
            .lock()
            .expect("the fake is not shared across threads")
            .push(String::from(op));
        let mut scripted = self
            .scripted
            .lock()
            .expect("the fake is not shared across threads");
        let found = scripted
            .iter_mut()
            .find(|entry| entry.as_ref().is_some_and(|entry| entry.op == op))
            .and_then(Option::take);
        // Released before the panic below: unwinding while it is held poisons
        // the mutex, and every later `lock` then reports that instead of this.
        drop(scripted);
        match found {
            Some(entry) => entry.outcome,
            None => panic!("nothing scripted answers `git {op}`"),
        }
    }

    /// Each operation the caller asked for, in order.
    pub(super) fn asked(&self) -> Vec<String> {
        self.asked
            .lock()
            .expect("the fake is not shared across threads")
            .clone()
    }

    /// Takes a note in stderr's place. Always lands, so the rollback for a
    /// refused write stays with the sayer that can refuse.
    pub(super) fn hear(&self, note: &str) -> bool {
        self.heard
            .lock()
            .expect("the fake is not shared across threads")
            .push(String::from(note));
        true
    }

    /// Each note said, in order.
    pub(super) fn heard(&self) -> Vec<String> {
        self.heard
            .lock()
            .expect("the fake is not shared across threads")
            .clone()
    }
}