#![cfg(feature = "fancy")]
use std::process::Command;
const TRIGGER_ENV: &str = "OOPSIE_COLOR_ENV_TEST_TRIGGER";
#[test]
#[expect(
clippy::print_stdout,
reason = "the parent reads the probe result back off the child's stdout"
)]
fn color_probe_child() {
if std::env::var_os(TRIGGER_ENV).is_none() {
return;
}
println!("has_color={}", oopsie::ColorMode::Auto.should_colorize());
}
fn probe(env: &[(&str, &str)]) -> bool {
let exe = std::env::current_exe().expect("locate test binary");
let mut cmd = Command::new(exe);
cmd.arg("color_probe_child")
.args(["--exact", "--nocapture", "--test-threads=1"])
.env(TRIGGER_ENV, "1")
.env_remove("NO_COLOR")
.env_remove("FORCE_COLOR");
for (key, value) in env {
cmd.env(key, value);
}
let output = cmd.output().expect("spawn child test process");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
output.status.success(),
"child process failed\n--- stdout ---\n{stdout}\n--- stderr ---\n{}",
String::from_utf8_lossy(&output.stderr)
);
if stdout.contains("has_color=true") {
true
} else if stdout.contains("has_color=false") {
false
} else {
panic!("child did not report a color result\n--- stdout ---\n{stdout}");
}
}
#[test]
fn detect_env_color_support_matrix() {
assert!(!probe(&[]), "piped baseline must not colorize");
assert!(probe(&[("FORCE_COLOR", "1")]), "FORCE_COLOR=1 forces color");
assert!(
!probe(&[("FORCE_COLOR", "0")]),
"FORCE_COLOR=0 is an explicit disable"
);
assert!(
!probe(&[("FORCE_COLOR", "false")]),
"FORCE_COLOR=false is an explicit disable"
);
assert!(
!probe(&[("FORCE_COLOR", "")]),
"empty FORCE_COLOR is ignored, falling back to the piped (false) terminal check"
);
assert!(
!probe(&[("NO_COLOR", "1"), ("FORCE_COLOR", "1")]),
"non-empty NO_COLOR wins over FORCE_COLOR"
);
assert!(
probe(&[("NO_COLOR", ""), ("FORCE_COLOR", "1")]),
"empty NO_COLOR is ignored, so FORCE_COLOR=1 still forces color"
);
}