use std::ffi::OsString;
use std::path::PathBuf;
use openmoat_core::ProjectHookOff;
use openmoat_hosts::{Host, SessionOverride};
use super::{Off, Preflight, clashing, decide, notice, pre_flighted};
#[test]
fn only_hosts_with_a_hook_of_their_own_are_checked() {
let mut skipped = 0;
for host in Host::EVERY {
let own = host.spec().install.is_some();
assert_eq!(pre_flighted(host), own, "{host:?}");
if !own {
assert!(host.spec().speaks.is_some(), "{host:?}");
skipped += 1;
}
}
assert!(skipped >= 1, "a host that runs another's hook exists");
for host in Host::ALL {
assert!(pre_flighted(host), "{host:?}");
}
}
fn with_override() -> (Host, &'static SessionOverride) {
Host::ALL
.into_iter()
.find_map(|h| Some((h, h.spec().hook_off.session_override.as_ref()?)))
.unwrap()
}
fn without_override() -> Host {
Host::ALL
.into_iter()
.find(|h| {
h.spec().hook_off.session_override.is_none() && h.spec().project_settings.is_some()
})
.unwrap()
}
fn off(overridden: bool) -> Off {
Off {
path: PathBuf::from("/p/settings.json"),
what: vec!["`x` turns every hook off".to_owned()],
overridden,
}
}
fn args(list: &[&str]) -> Vec<OsString> {
list.iter().map(OsString::from).collect()
}
fn message(found: &Preflight) -> String {
match found {
Preflight::Refused { message, .. } => message.clone(),
other => panic!("not refused: {other:?}"),
}
}
#[test]
fn nothing_found_is_clear_for_a_host_without_an_override() {
let host = without_override();
let found = decide(host, &[], None, &[], ProjectHookOff::Refuse);
assert_eq!(found, Preflight::Clear);
assert_eq!(notice(Some(host), &found), None);
}
#[test]
fn a_file_that_turns_the_hook_off_is_refused_with_the_way_out() {
let host = without_override();
let shown = message(&decide(
host,
&[off(false)],
None,
&[],
ProjectHookOff::Refuse,
));
assert!(shown.contains("does not start"), "{shown}");
assert!(
shown.contains("/p/settings.json: `x` turns every hook off"),
"{shown}"
);
assert!(shown.contains("sandbox.project_hook_off: start"), "{shown}");
}
#[test]
fn the_persons_policy_starts_it_anyway_and_says_so() {
let host = without_override();
let found = decide(host, &[off(false)], None, &[], ProjectHookOff::Start);
assert!(matches!(found, Preflight::Started { .. }), "{found:?}");
let said = notice(Some(host), &found).unwrap();
assert!(said.contains("without OpenMoat's hook"), "{said}");
}
#[test]
fn the_hosts_own_option_goes_on_every_run() {
let (host, over) = with_override();
for files in [vec![], vec![off(true)]] {
let found = decide(host, &files, Some(over), &[], ProjectHookOff::Refuse);
let Preflight::Override { args, .. } = &found else {
panic!("{found:?}");
};
assert_eq!(*args, over.args);
assert!(notice(Some(host), &found).is_some());
}
let refused = decide(
host,
&[off(true), off(false)],
Some(over),
&[],
ProjectHookOff::Refuse,
);
assert!(matches!(refused, Preflight::Refused { .. }), "{refused:?}");
}
#[test]
fn a_persons_own_clashing_option_is_never_overridden() {
let (host, over) = with_override();
let clash = over.clashes[0];
for given in [
args(&[clash, "{}"]),
args(&[&format!("{clash}=x.json"), "-p", "hi"]),
] {
let shown = message(&decide(
host,
&[off(true)],
Some(over),
&given,
ProjectHookOff::Refuse,
));
assert!(shown.contains(&format!("your own `{clash}`")), "{shown}");
let clear = decide(host, &[], Some(over), &given, ProjectHookOff::Refuse);
assert_eq!(clear, Preflight::Clear);
}
let started = decide(
host,
&[off(true)],
Some(over),
&args(&[clash, "{}"]),
ProjectHookOff::Start,
);
assert!(matches!(started, Preflight::Started { .. }), "{started:?}");
}
#[test]
fn only_option_words_before_a_double_dash_clash() {
let (_, over) = with_override();
let clash = over.clashes[0];
assert_eq!(
clashing(over, &args(&[&format!("explain {clash} please")])),
None
);
assert_eq!(clashing(over, &args(&["-p", "--", clash, "{}"])), None);
assert_eq!(
clashing(over, &args(&["-p", clash, "{}"])),
Some(clash.to_owned())
);
}