hive-priv: test seams are traits, not closure bags
This commit is contained in:
parent
5318459c9e
commit
6d04be6c62
1 changed files with 213 additions and 113 deletions
|
|
@ -7,6 +7,7 @@ use std::path::{Path, PathBuf};
|
|||
|
||||
use anyhow::{Context as _, Result, bail};
|
||||
use hive_priv_sock::AGENT_PREFIX;
|
||||
use tokio::process::Command;
|
||||
|
||||
/// Return the system container name for a logical agent name.
|
||||
/// All agents (including the manager) use the `h-` prefix.
|
||||
|
|
@ -210,13 +211,60 @@ impl Drop for StagedFile<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Scratch-directory helpers shared by every module's tests. One counter for
|
||||
/// How an op starts a host program. [`RealSpawn`] in production; tests
|
||||
/// substitute stand-ins, so no root, container or btrfs filesystem is needed.
|
||||
pub(crate) trait Spawn {
|
||||
/// The `program` command, ready for arguments.
|
||||
fn command(&self, program: &str) -> Command;
|
||||
}
|
||||
|
||||
/// Starts the program `program` names on `PATH`.
|
||||
pub(crate) struct RealSpawn;
|
||||
|
||||
impl Spawn for RealSpawn {
|
||||
fn command(&self, program: &str) -> Command {
|
||||
Command::new(program)
|
||||
}
|
||||
}
|
||||
|
||||
/// Scratch directories and stand-in tools shared by every module's tests. One counter for
|
||||
/// the whole crate, so no two tests get the same directory.
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_support {
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use tokio::process::Command;
|
||||
|
||||
use super::Spawn;
|
||||
|
||||
/// Runs every program as `sh -c <script> <program> <args…>`.
|
||||
pub(crate) struct ScriptSpawn(pub(crate) String);
|
||||
|
||||
impl Spawn for ScriptSpawn {
|
||||
fn command(&self, program: &str) -> Command {
|
||||
let mut cmd = Command::new("sh");
|
||||
cmd.arg("-c").arg(&self.0).arg(program);
|
||||
cmd
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts a program that does not exist, whatever is asked for.
|
||||
pub(crate) struct MissingTool;
|
||||
|
||||
impl Spawn for MissingTool {
|
||||
fn command(&self, _: &str) -> Command {
|
||||
Command::new("/nonexistent/hive-priv-test-tool")
|
||||
}
|
||||
}
|
||||
|
||||
/// A borrowed stand-in, so one stub can back several fixtures.
|
||||
impl<T: Spawn> Spawn for &T {
|
||||
fn command(&self, program: &str) -> Command {
|
||||
(**self).command(program)
|
||||
}
|
||||
}
|
||||
|
||||
/// Unique scratch dir per test, no external tempfile dep.
|
||||
pub(crate) fn scratch() -> PathBuf {
|
||||
static CTR: AtomicU32 = AtomicU32::new(0);
|
||||
|
|
|
|||
Loading…
Reference in a new issue