use super::*;
use std::fs;
use tempfile::TempDir;
fn fixture() -> (TempDir, Registry) {
let tmp = TempDir::new().unwrap();
let reg = Registry::at(tmp.path());
(tmp, reg)
}
#[test]
fn bind_writes_an_absolute_symlink_to_the_machine_binary() {
let (tmp, reg) = fixture();
let bin = tmp.path().join("real-binary");
fs::write(&bin, "#!/bin/sh\n").unwrap();
reg.bind("tracker", &bin).unwrap();
let link = tmp.path().join("config/plugins/bin/tracker");
assert_eq!(fs::read_link(&link).unwrap(), bin);
}
#[test]
fn resolve_bin_reads_a_bound_name_through_to_its_binary() {
let (tmp, reg) = fixture();
let bin = tmp.path().join("real-binary");
fs::write(&bin, "x").unwrap();
reg.bind("tracker", &bin).unwrap();
assert_eq!(reg.resolve_bin("tracker"), Some(fs::canonicalize(&bin).unwrap()));
}
#[test]
fn resolve_bin_is_none_for_an_unbound_or_dangling_name() {
let (tmp, reg) = fixture();
assert_eq!(reg.resolve_bin("ghost"), None);
let bin = tmp.path().join("gone");
fs::write(&bin, "x").unwrap();
reg.bind("tracker", &bin).unwrap();
fs::remove_file(&bin).unwrap();
assert_eq!(reg.resolve_bin("tracker"), None);
}
#[test]
fn resolve_bin_is_none_when_the_name_resolves_to_a_non_file() {
let (tmp, reg) = fixture();
fs::create_dir_all(tmp.path().join("config/plugins/bin")).unwrap();
assert_eq!(reg.resolve_bin(""), None);
reg.bind("dir", tmp.path()).unwrap();
assert_eq!(reg.resolve_bin("dir"), None);
}
#[test]
fn binding_is_idempotent_and_re_bind_wins() {
let (tmp, reg) = fixture();
let first = tmp.path().join("one");
let second = tmp.path().join("two");
fs::write(&first, "1").unwrap();
fs::write(&second, "2").unwrap();
reg.bind("tracker", &first).unwrap();
reg.bind("tracker", &second).unwrap();
let link = tmp.path().join("config/plugins/bin/tracker");
assert_eq!(fs::read_link(&link).unwrap(), second);
}