use std::io::{Read as _, Write as _};
use super::*;
fn argv(args: &[&str]) -> Vec<String> {
args.iter().map(ToString::to_string).collect()
}
#[test]
fn no_args_defaults_to_background() {
assert_eq!(parse(argv(&[])), Command::Background);
}
#[test]
fn remote_bind_allowed_requires_exact_value_one() {
let _guard = EnvGuard::set("MOADIM_ALLOW_REMOTE", "1");
assert!(remote_bind_allowed());
}
#[test]
fn remote_bind_allowed_false_for_unset_or_other_values() {
let previous = std::env::var_os("MOADIM_ALLOW_REMOTE");
unsafe {
std::env::remove_var("MOADIM_ALLOW_REMOTE");
}
assert!(!remote_bind_allowed());
for bogus in ["true", "yes", "0", ""] {
let _guard = EnvGuard::set("MOADIM_ALLOW_REMOTE", bogus);
assert!(!remote_bind_allowed(), "value {bogus}");
}
if let Some(previous) = previous {
unsafe {
std::env::set_var("MOADIM_ALLOW_REMOTE", previous);
}
}
}
#[test]
fn interactive_flags_select_foreground() {
for flag in ["-i", "--interactive", "-f", "--foreground"] {
assert_eq!(parse(argv(&[flag])), Command::Foreground, "flag {flag}");
}
}
#[test]
fn background_flags_select_background() {
for flag in ["-b", "--background", "-d", "--detach", "--daemon"] {
assert_eq!(parse(argv(&[flag])), Command::Background, "flag {flag}");
}
}
#[test]
fn stop_and_status_commands() {
assert_eq!(
parse(argv(&["stop"])),
Command::Stop {
json: false,
quiet: false
}
);
assert_eq!(
parse(argv(&["status"])),
Command::Status {
json: false,
wait_secs: None
}
);
}
#[test]
fn cleanup_command() {
assert_eq!(parse(argv(&["cleanup"])), Command::Cleanup { json: false });
}
#[test]
fn json_flag_sets_machine_readable_output() {
assert_eq!(
parse(argv(&["status", "--json"])),
Command::Status {
json: true,
wait_secs: None
}
);
assert_eq!(
parse(argv(&["cleanup", "--json"])),
Command::Cleanup { json: true }
);
assert_eq!(
parse(argv(&["stop", "--json"])),
Command::Stop {
json: true,
quiet: false
}
);
}
#[test]
fn quiet_flag_only_applies_to_stop() {
for flag in ["--quiet", "-q"] {
assert_eq!(
parse(argv(&["stop", flag])),
Command::Stop {
json: false,
quiet: true
},
"flag {flag}"
);
}
assert_eq!(
parse(argv(&["stop", "--json", "--quiet"])),
Command::Stop {
json: true,
quiet: true
}
);
assert_eq!(
parse(argv(&["stop", "-q", "--json"])),
Command::Stop {
json: true,
quiet: true
}
);
assert_eq!(parse(argv(&["--quiet"])), Command::Usage("--quiet".into()));
assert_eq!(parse(argv(&["-q"])), Command::Usage("-q".into()));
}
use std::net::TcpListener;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
const UNREACHABLE_ADDR: &str = "127.0.0.1:1";
struct EnvGuard {
name: &'static str,
previous: Option<std::ffi::OsString>,
}
impl EnvGuard {
fn set(name: &'static str, value: &str) -> Self {
let previous = std::env::var_os(name);
unsafe {
std::env::set_var(name, value);
}
Self { name, previous }
}
}
include!("start.rs");