#![cfg(feature = "cli")]
use std::process::Command as StdCommand;
use rusty_figlet::{ColorDepth, FigletBuilder, color_depth::resolve_depth};
fn cargo_bin() -> StdCommand {
let path = env!("CARGO_BIN_EXE_rusty-figlet");
StdCommand::new(path)
}
#[test]
fn env_truecolor_detects_truecolor() {
let mut cmd = cargo_bin();
cmd.env_clear();
cmd.env("COLORTERM", "truecolor");
cmd.env("NO_COLOR", "1"); cmd.args(["--version"]);
let out = cmd.output().expect("binary executes");
assert!(out.status.success());
}
#[test]
fn env_24bit_detects_truecolor() {
let mut cmd = cargo_bin();
cmd.env_clear();
cmd.env("COLORTERM", "24bit");
cmd.env("NO_COLOR", "1");
cmd.args(["--version"]);
let out = cmd.output().expect("binary executes");
assert!(out.status.success());
}
#[test]
fn env_unset_detects_color16() {
let mut cmd = cargo_bin();
cmd.env_clear();
cmd.env("NO_COLOR", "1");
cmd.args(["--version"]);
let out = cmd.output().expect("binary executes");
assert!(out.status.success());
}
#[test]
fn builder_color_depth_cached() {
let f = FigletBuilder::new()
.color_depth(ColorDepth::Truecolor)
.build()
.expect("build");
assert_eq!(f.color_depth(), ColorDepth::Truecolor);
}
#[test]
fn builder_color_depth_set_invalidation() {
let mut f = FigletBuilder::new()
.color_depth(ColorDepth::Truecolor)
.build()
.expect("build");
f.set_color_depth(ColorDepth::Color16);
assert_eq!(f.color_depth(), ColorDepth::Color16);
}
#[test]
fn builder_unset_color_depth_uses_detect() {
let f = FigletBuilder::new().build().expect("build");
let depth = f.color_depth();
assert!(matches!(
depth,
ColorDepth::Truecolor | ColorDepth::Color256 | ColorDepth::Color16
));
}
#[test]
fn truecolor_unsupported_downgrades_with_warning() {
let result = resolve_depth(ColorDepth::Truecolor, ColorDepth::Color16, false);
assert_eq!(result, ColorDepth::Color16);
}
#[test]
fn truecolor_suppresses_warning_when_no_downgrade_warning() {
let result = resolve_depth(ColorDepth::Truecolor, ColorDepth::Color16, true);
assert_eq!(result, ColorDepth::Color16);
}
#[test]
fn no_downgrade_required_returns_requested() {
let result = resolve_depth(ColorDepth::Truecolor, ColorDepth::Truecolor, false);
assert_eq!(result, ColorDepth::Truecolor);
}
#[test]
fn color16_requested_always_color16() {
let result = resolve_depth(ColorDepth::Color16, ColorDepth::Truecolor, false);
assert_eq!(result, ColorDepth::Color16);
}
#[test]
fn downgrade_warning_string_does_not_reference_envvar() {
let adversarial = "EVIL_BYTES_SHOULD_NOT_LEAK_HERE";
let mut cmd = cargo_bin();
cmd.env_clear();
cmd.env("COLORTERM", adversarial);
cmd.args(["--version"]);
let out = cmd.output().expect("binary executes");
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
!stderr.contains(adversarial),
"COLORTERM bytes leaked into stderr: {stderr}"
);
}