use luff::printer::Colors;
use serial_test::serial;
use std::env;
use std::fs;
use tempfile::TempDir;
#[test]
#[serial]
fn test_colors_respects_no_color() {
unsafe {
env::set_var("NO_COLOR", "1");
}
let colors = Colors::new();
assert!(!colors.enabled());
unsafe {
env::remove_var("NO_COLOR");
}
}
#[test]
#[serial]
fn test_colors_respects_clicolor_zero() {
unsafe {
env::set_var("CLICOLOR", "0");
}
let colors = Colors::new();
assert!(!colors.enabled());
unsafe {
env::remove_var("CLICOLOR");
}
}
#[test]
#[serial]
fn test_colorize_directory() {
let temp = TempDir::new().unwrap();
let dir = temp.path().join("testdir");
fs::create_dir(&dir).unwrap();
let old_no_color = env::var("NO_COLOR");
unsafe {
env::remove_var("NO_COLOR");
env::remove_var("CLICOLOR");
}
let colors = Colors::new();
if colors.enabled() {
let result = colors.colorize("testdir", &dir);
let result_str = result.to_string();
assert!(result_str.contains("testdir"));
assert!(result_str.contains("\x1b["));
}
if let Ok(val) = old_no_color {
unsafe {
env::set_var("NO_COLOR", val);
}
}
}
#[test]
#[serial]
fn test_colorize_when_disabled() {
unsafe {
env::set_var("NO_COLOR", "1");
}
let colors = Colors::new();
let temp = TempDir::new().unwrap();
let dir = temp.path().join("testdir");
fs::create_dir(&dir).unwrap();
let result = colors.colorize("testdir", &dir);
assert_eq!(result.to_string(), "testdir");
unsafe {
env::remove_var("NO_COLOR");
}
}
#[test]
#[serial]
fn test_colorize_returns_display_impl_when_disabled() {
unsafe {
env::set_var("NO_COLOR", "1");
}
let colors = Colors::new();
let result = colors.colorize("test.txt", std::path::Path::new("/tmp/test.txt"));
assert_eq!(result.to_string(), "test.txt");
unsafe {
env::remove_var("NO_COLOR");
}
}
#[cfg(unix)]
#[test]
#[serial]
fn test_colorize_executable() {
use std::os::unix::fs::PermissionsExt;
let temp = TempDir::new().unwrap();
let file = temp.path().join("script");
fs::write(&file, "#!/bin/sh\necho test").unwrap();
let mut perms = fs::metadata(&file).unwrap().permissions();
perms.set_mode(0o755);
fs::set_permissions(&file, perms).unwrap();
unsafe {
env::remove_var("NO_COLOR");
env::remove_var("CLICOLOR");
}
let colors = Colors::new();
if colors.enabled() {
let result = colors.colorize("script", &file);
let result_str = result.to_string();
assert!(result_str.contains("script"));
assert!(result_str.contains("\x1b["));
}
}