luff 0.2.1

Print files with formatting
Documentation
//! Environment-dependent tests for Colors
//!
//! These tests manipulate environment variables and require unsafe code,
//! so they're isolated in integration tests (which don't inherit the
//! forbid(unsafe_code) from lib.rs).

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");
    // SAFETY: marked with `#[serial]` to prevent concurrent execution.
    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 {
        // SAFETY: marked with `#[serial]` to prevent concurrent execution
        unsafe {
            env::set_var("NO_COLOR", val);
        }
    }
}

#[test]
#[serial]
fn test_colorize_when_disabled() {
    // SAFETY: marked with `#[serial]` to prevent concurrent execution
    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");

    // SAFETY: marked with `#[serial]` to prevent concurrent execution
    unsafe {
        env::remove_var("NO_COLOR");
    }
}

#[test]
#[serial]
fn test_colorize_returns_display_impl_when_disabled() {
    // Force colors off
    unsafe {
        env::set_var("NO_COLOR", "1");
    }

    let colors = Colors::new();
    let result = colors.colorize("test.txt", std::path::Path::new("/tmp/test.txt"));

    // Should be convertible to string
    assert_eq!(result.to_string(), "test.txt");

    // SAFETY: marked with `#[serial]` to prevent concurrent execution
    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();

    // SAFETY: marked with `#[serial]` to prevent concurrent execution
    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["));
    }
}