mod spawned;
use super::*;
use std::ffi::OsString;
use tempfile::tempdir;
#[test]
fn new_stores_binary_path() {
let cli = Cli::new("/usr/local/bin/litany");
assert_eq!(cli.binary(), Path::new("/usr/local/bin/litany"));
}
fn resolve_injected(binary: Binary, set: Option<&str>) -> Cli {
let value = set.map(OsString::from);
Cli::resolve_with(binary, move |_| value.clone(), None)
}
#[test]
fn resolve_reads_ambient_env() {
let cli = Cli::resolve(Binary::Litany);
assert!(!cli.binary().as_os_str().is_empty());
}
#[test]
fn resolve_litany_uses_env_var_when_set() {
let cli = resolve_injected(Binary::Litany, Some("/opt/litany-test"));
assert_eq!(cli.binary(), Path::new("/opt/litany-test"));
}
#[test]
fn resolve_litany_falls_back_to_default_when_env_empty() {
let cli = resolve_injected(Binary::Litany, Some(""));
assert_eq!(cli.binary(), Path::new("litany"));
}
#[test]
fn resolve_litany_falls_back_to_default_when_env_unset() {
let cli = resolve_injected(Binary::Litany, None);
assert_eq!(cli.binary(), Path::new("litany"));
}
#[test]
fn resolve_bl_uses_env_var_when_set() {
let cli = resolve_injected(Binary::Bl, Some("/opt/bl-test"));
assert_eq!(cli.binary(), Path::new("/opt/bl-test"));
}
#[test]
fn resolve_bl_falls_back_to_default_when_env_unset() {
let cli = resolve_injected(Binary::Bl, None);
assert_eq!(cli.binary(), Path::new("bl"));
}
#[test]
fn resolve_bz_uses_env_var_when_set() {
let cli = resolve_injected(Binary::Bz, Some("/opt/bz-test"));
assert_eq!(cli.binary(), Path::new("/opt/bz-test"));
}
#[test]
fn resolve_bz_falls_back_to_default_when_env_unset() {
let cli = resolve_injected(Binary::Bz, None);
assert_eq!(cli.binary(), Path::new("bz"));
}
#[test]
fn every_namespace_is_self_multiplexed() {
let exe = PathBuf::from("/proc/self/yog");
for (embedded, name) in [
(Binary::Bl, "bl"),
(Binary::Bz, "bz"),
(Binary::Litany, "litany"),
] {
let cli = Cli::resolve_with(embedded, |_| None, Some(exe.clone()));
assert_eq!(cli.program(), exe, "{name} must exec yog itself");
assert_eq!(cli.prefix(), [name.to_string()]);
assert_eq!(cli.binary(), Path::new(name));
}
}
#[test]
fn self_multiplex_execs_current_exe_under_the_namespace_prefix() {
let cli = Cli::default_target(
"litany",
crate::cli_outbound::resolve::Target::Namespace("litany"),
Some(PathBuf::from("/proc/self/yog")),
);
assert_eq!(cli.program(), Path::new("/proc/self/yog"));
assert_eq!(cli.prefix(), ["litany".to_string()]);
assert_eq!(cli.binary(), Path::new("litany"));
}
#[test]
fn self_multiplex_with_no_current_exe_falls_back_to_the_host_name() {
let cli = Cli::default_target(
"litany",
crate::cli_outbound::resolve::Target::Namespace("litany"),
None,
);
assert_eq!(cli.program(), Path::new("litany"));
assert!(cli.prefix().is_empty());
assert_eq!(cli.binary(), Path::new("litany"));
}
#[test]
fn yogs_own_shim_execs_the_running_exe_with_no_verb_word() {
let exe = PathBuf::from("/proc/self/yog");
let cli = Cli::default_target(
"yog",
crate::cli_outbound::resolve::Target::SelfBare,
Some(exe.clone()),
);
assert_eq!(cli.program(), exe);
assert!(
cli.prefix().is_empty(),
"no namespace word: {:?}",
cli.prefix()
);
let resolved = Cli::resolve_with(Binary::Yog, |_| None, Some(exe.clone()));
assert_eq!(resolved.program(), exe);
assert!(resolved.prefix().is_empty());
}
#[test]
fn yogs_shim_honors_its_binary_override() {
let cli = Cli::resolve_with(
Binary::Yog,
|k| (k == "YOG_BINARY").then(|| std::ffi::OsString::from("/opt/yog")),
Some(PathBuf::from("/proc/self/yog")),
);
assert_eq!(cli.program(), Path::new("/opt/yog"));
}
#[test]
fn override_wins_over_the_self_multiplex_switch() {
let cli = Cli::resolve_with(
Binary::Bl,
|_| Some(OsString::from("/opt/bl-test")),
Some(PathBuf::from("/proc/self/yog")),
);
assert_eq!(cli.program(), Path::new("/opt/bl-test"));
assert!(cli.prefix().is_empty());
}
#[test]
fn self_multiplex_spawn_prepends_the_namespace_to_the_childs_argv() {
let dir = tempdir().unwrap();
let script = write_script(
dir.path(),
"echo_argv",
"#!/bin/sh\nprintf '%s\\n' \"$@\"\n",
);
let cli = Cli::default_target(
"litany",
crate::cli_outbound::resolve::Target::Namespace("litany"),
Some(script),
);
let (out, err, exit) = collect(cli.run(&["prompt", "goal"]).unwrap());
assert_eq!(exit, ExitInfo::Code(0));
assert!(err.is_empty());
assert_eq!(String::from_utf8(out).unwrap(), "litany\nprompt\ngoal\n");
}