use std::process::Command;
use csaf_core::update_cli::UpdateFlags;
use csaf_core::updater::{self, NO_SELF_UPDATE_ENV};
const PROBE_ENV: &str = "CSAF_UPDATER_ENV_PROBE";
const CASES: [(Option<&str>, bool); 17] = [
(None, false),
(Some(""), false),
(Some("0"), false),
(Some("false"), false),
(Some("no"), false),
(Some("off"), false),
(Some("maybe"), false),
(Some("2"), false),
(Some("yes please"), false),
(Some("1"), true),
(Some(" 1 "), true),
(Some("true"), true),
(Some("TRUE"), true),
(Some("yes"), true),
(Some("Yes"), true),
(Some("on"), true),
(Some("ON"), true),
];
#[test]
fn env_var_controls_the_self_update_opt_out() {
if let Ok(expectation) = std::env::var(PROBE_ENV) {
run_probe(&expectation);
return;
}
let exe = std::env::current_exe().expect("the test binary must have a path");
for (value, expect_opt_out) in CASES {
let mut cmd = Command::new(&exe);
cmd.args(["--exact", "env_var_controls_the_self_update_opt_out"]);
cmd.env(PROBE_ENV, if expect_opt_out { "1" } else { "0" });
match value {
Some(v) => cmd.env(NO_SELF_UPDATE_ENV, v),
None => cmd.env_remove(NO_SELF_UPDATE_ENV),
};
let status = cmd
.status()
.expect("re-executing the test binary must work");
assert!(
status.success(),
"{NO_SELF_UPDATE_ENV}={value:?} should give opted_out={expect_opt_out}, \
but the probe child failed (exit {status})",
);
}
}
fn run_probe(expectation: &str) {
let want = expectation == "1";
let raw = std::env::var(NO_SELF_UPDATE_ENV).ok();
assert_eq!(
updater::env_opt_out(),
want,
"env_opt_out() wrong for {NO_SELF_UPDATE_ENV}={raw:?}",
);
assert_eq!(
UpdateFlags::default().is_opted_out(),
want,
"UpdateFlags::is_opted_out() wrong for {NO_SELF_UPDATE_ENV}={raw:?}",
);
let flagged = UpdateFlags {
no_self_update: true,
..UpdateFlags::default()
};
assert!(
flagged.is_opted_out(),
"--no-self-update must refuse even with {NO_SELF_UPDATE_ENV}={raw:?}",
);
}