guardy 0.2.4

Fast, secure git hooks in Rust with secret scanning and protected file synchronization
Documentation
use std::fs;

use tempfile::TempDir;

#[tokio::test]
async fn test_recursive_config_behavior_with_cli() {
    // Test that the CLI argument parsing works correctly
    // We can't easily test the global CONFIG static, but we can test
    // that the CLI flag structure is properly defined

    // Create a test CLI args structure to verify the field exists
    use clap::Parser;
    use guardy::cli::commands::Cli;

    // Test parsing with recursive_config flag
    let args = Cli::try_parse_from(["guardy", "--recursive-config=false", "config", "show"]);

    assert!(args.is_ok());
    let cli = args.unwrap();
    assert_eq!(cli.recursive_config, Some(false));

    println!("✅ CLI recursive_config flag parsing works correctly");
}

#[tokio::test]
async fn test_recursive_config_env_var_parsing() {
    // Test the environment variable parsing logic without using unsafe
    // We test the parsing function directly rather than global state

    // Test with various values that should parse as false
    let test_cases = vec![
        ("0", false),
        ("false", false),
        ("FALSE", false),
        ("no", false),
        ("1", true),
        ("true", true),
        ("TRUE", true),
        ("yes", true),
    ];

    for (input, expected) in test_cases {
        // Test the parsing logic matches what we expect
        let result = match input {
            "1" | "true" | "TRUE" | "yes" | "YES" | "on" | "ON" => true,
            "0" | "false" | "FALSE" | "no" | "NO" | "off" | "OFF" => false,
            _ => true, // default
        };
        assert_eq!(result, expected, "Failed for input: {input}");
    }

    println!("✅ Environment variable parsing logic works correctly");
}

#[tokio::test]
async fn test_recursive_config_file_structure() {
    // Test that we can create the expected config file structure
    // for testing recursive behavior (without actually testing global state)

    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let parent_dir = temp_dir.path();
    let child_dir = parent_dir.join("child");
    fs::create_dir_all(&child_dir).expect("Failed to create child directory");

    // Create config files
    fs::write(
        parent_dir.join("guardy.yaml"),
        "general:\n  debug: true\n  recursive_config: true\n",
    )
    .expect("Failed to write parent config");

    fs::write(
        child_dir.join("guardy.yaml"),
        "general:\n  debug: false\n  recursive_config: false\n",
    )
    .expect("Failed to write child config");

    // Verify files were created correctly
    assert!(parent_dir.join("guardy.yaml").exists());
    assert!(child_dir.join("guardy.yaml").exists());

    // Read back and verify content contains our setting
    let child_config =
        fs::read_to_string(child_dir.join("guardy.yaml")).expect("Failed to read child config");
    assert!(child_config.contains("recursive_config: false"));

    let parent_config =
        fs::read_to_string(parent_dir.join("guardy.yaml")).expect("Failed to read parent config");
    assert!(parent_config.contains("recursive_config: true"));

    println!("✅ Config file structure for recursive testing created successfully");
}