#![allow(
clippy::missing_docs_in_private_items,
reason = "test helpers and fixtures do not need doc comments"
)]
use super::*;
fn restore(prev: Option<std::ffi::OsString>) {
unsafe {
match prev {
Some(value) => std::env::set_var(MAX_CONCURRENT_RUNS_ENV, value),
None => std::env::remove_var(MAX_CONCURRENT_RUNS_ENV),
}
}
}
struct HomeGuard {
dir: std::path::PathBuf,
}
impl HomeGuard {
fn new(tag: &str) -> Self {
let dir = std::env::temp_dir().join(format!(
"moadim-concurrency-cap-{tag}-{}",
uuid::Uuid::new_v4()
));
std::fs::create_dir_all(&dir).expect("create temp home");
unsafe { std::env::set_var("MOADIM_HOME_OVERRIDE", &dir) }
Self { dir }
}
}
impl Drop for HomeGuard {
fn drop(&mut self) {
unsafe { std::env::remove_var("MOADIM_HOME_OVERRIDE") }
let _ = std::fs::remove_dir_all(&self.dir);
}
}
#[test]
fn max_concurrent_runs_defaults_when_unset() {
let _home = HomeGuard::new("defaults");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
unsafe {
std::env::remove_var(MAX_CONCURRENT_RUNS_ENV);
}
assert_eq!(max_concurrent_runs(), DEFAULT_MAX_CONCURRENT_RUNS);
restore(prev);
}
#[test]
fn max_concurrent_runs_parses_a_valid_value() {
let _home = HomeGuard::new("valid");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
unsafe {
std::env::set_var(MAX_CONCURRENT_RUNS_ENV, "9");
}
assert_eq!(max_concurrent_runs(), 9);
restore(prev);
}
#[test]
fn max_concurrent_runs_falls_back_to_default_on_garbage() {
let _home = HomeGuard::new("garbage");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
unsafe {
std::env::set_var(MAX_CONCURRENT_RUNS_ENV, "not-a-number");
}
assert_eq!(max_concurrent_runs(), DEFAULT_MAX_CONCURRENT_RUNS);
restore(prev);
}
#[test]
fn max_concurrent_runs_parses_zero_as_unlimited() {
let _home = HomeGuard::new("zero");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
unsafe {
std::env::set_var(MAX_CONCURRENT_RUNS_ENV, "0");
}
assert_eq!(max_concurrent_runs(), 0);
restore(prev);
}
#[test]
fn max_concurrent_runs_uses_file_override_when_env_unset() {
let _home = HomeGuard::new("file-override");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
unsafe {
std::env::remove_var(MAX_CONCURRENT_RUNS_ENV);
}
crate::machine::set_max_concurrent_runs_override(Some(4)).expect("write cap override");
assert_eq!(max_concurrent_runs(), 4);
restore(prev);
}
#[test]
fn max_concurrent_runs_prefers_env_over_file_override() {
let _home = HomeGuard::new("env-over-file");
let prev = std::env::var_os(MAX_CONCURRENT_RUNS_ENV);
crate::machine::set_max_concurrent_runs_override(Some(4)).expect("write cap override");
unsafe {
std::env::set_var(MAX_CONCURRENT_RUNS_ENV, "9");
}
assert_eq!(max_concurrent_runs(), 9);
restore(prev);
}