use super::GracefulMechanism;
#[test]
fn graceful_mechanism_display_is_stable_and_distinct() {
let all = [
(GracefulMechanism::Process, "process"),
(GracefulMechanism::ConsoleGroup, "own console group"),
(
GracefulMechanism::OtherConsoleGroup,
"own console group in another console",
),
(GracefulMechanism::Unknown, "unknown"),
(GracefulMechanism::None, "none"),
];
for (mechanism, expected) in all {
assert_eq!(mechanism.to_string(), expected, "Display for {mechanism:?}");
}
for (i, (_, a)) in all.iter().enumerate() {
for (_, b) in all.iter().skip(i + 1) {
assert_ne!(a, b, "two variants share a Display string");
}
}
}
#[test]
fn signal_refuses_a_child_with_no_mechanism() {
let id = crate::identity::ProcessId::current();
let err = super::signal(GracefulMechanism::None, id).expect_err("no group to address");
let crate::error::Error::Unsupported { detail, .. } = &err else {
panic!("expected Unsupported, got {err:?}");
};
assert!(detail.contains("contain"), "the refusal must name the remedy: {detail}");
}
#[cfg(windows)]
#[test]
fn signal_attempts_a_child_whose_group_may_be_in_another_console() {
let mut cmd = crate::Command::new();
cmd.args(["ping", "-n", "30", "127.0.0.1"]);
cmd.contain();
let child = cmd.spawn().expect("spawn");
let other = super::signal(GracefulMechanism::OtherConsoleGroup, child.id());
let group = super::signal(GracefulMechanism::ConsoleGroup, child.id());
assert!(
other.is_ok(),
"OtherConsoleGroup must be attempted, not refused: {other:?}"
);
assert_eq!(
other.is_ok(),
group.is_ok(),
"the two console mechanisms must take one path: {other:?} vs {group:?}"
);
let _ = child.kill_tree();
let _ = child.wait();
}
#[cfg(windows)]
#[test]
fn signal_refuses_a_child_cosca_did_not_create() {
let mut cmd = crate::Command::new();
cmd.args(["ping", "-n", "30", "127.0.0.1"]);
cmd.contain();
let child = cmd.spawn().expect("spawn");
let mechanism = crate::containment::Attachment::uac_elevated().graceful;
let err = super::signal(mechanism, child.id()).expect_err("a child cosca did not create must not be signalled");
let crate::error::Error::Unsupported { detail, .. } = &err else {
panic!("expected Unsupported, got {err:?}");
};
assert!(
detail.contains("did not create this child"),
"the refusal must say whose child this is not: {detail}"
);
assert!(detail.contains("kill()"), "the refusal must name the remedy: {detail}");
let _ = child.kill_tree();
let _ = child.wait();
}