use super::*;
use crate::test_support::{spawn_guard, world_under};
use tempfile::TempDir;
#[test]
fn an_unstated_plan_is_the_worlds_own_loopback() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let plan = plan(&world, None, None, None, None);
assert_eq!(plan.dir, super::super::super::material::dir(&world));
assert_eq!(plan.address, format!("{LOOPBACK}:{PORT}"));
assert!(!plan.force);
}
#[test]
fn a_stated_plan_is_taken_and_an_empty_statement_is_not() {
let tmp = TempDir::new().expect("tmp");
let world = world_under(tmp.path());
let stated = plan(
&world,
Some("/elsewhere/wire".to_owned()),
Some("engine.example.com".to_owned()),
Some("7000".to_owned()),
Some("1".to_owned()),
);
assert_eq!(stated, stated.clone(), "a plan is a value, and says so");
assert_eq!(stated.dir, PathBuf::from("/elsewhere/wire"));
assert_eq!(stated.address, "engine.example.com:7000");
assert!(stated.force);
let blank = plan(
&world,
Some(String::new()),
Some(String::new()),
Some(String::new()),
Some(String::new()),
);
assert_eq!(blank.dir, super::super::super::material::dir(&world));
assert_eq!(blank.address, format!("{LOOPBACK}:{PORT}"));
assert!(!blank.force, "an empty FORCE is not a rotation");
}
#[test]
fn it_mints_then_refuses_then_rotates() {
let _guard = spawn_guard();
let tmp = TempDir::new().expect("tmp");
let dir = tmp.path().join("wire");
let mut plan = Plan {
dir: dir.clone(),
address: "127.0.0.1:0".to_owned(),
force: false,
};
assert_eq!(perform(&plan), 0, "minted");
let first = std::fs::read(dir.join(ANCHORS)).expect("ca");
assert_eq!(perform(&plan), 1, "refused: material is already here");
assert_eq!(
std::fs::read(dir.join(ANCHORS)).expect("ca"),
first,
"and refused without touching it"
);
plan.force = true;
assert_eq!(perform(&plan), 0, "rotated");
assert_ne!(std::fs::read(dir.join(ANCHORS)).expect("ca"), first);
}
#[test]
fn a_mint_that_cannot_run_exits_one() {
let _guard = spawn_guard();
let tmp = TempDir::new().expect("tmp");
let blocked = tmp.path().join("file");
std::fs::write(&blocked, b"not a directory").expect("file");
assert_eq!(
perform(&Plan {
dir: blocked,
address: "127.0.0.1:0".to_owned(),
force: false,
}),
1
);
}
#[test]
fn the_reads_are_named_once() {
assert_eq!(READS, ["WIRE_DIR", "WIRE_HOST", "WIRE_PORT", "FORCE"]);
}