guardy 0.2.4

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

#[tokio::test]
async fn test_dump_hooks_config_json() -> Result<()> {
    // Test dumping configuration in JSON format
    let result = guardy::hooks::dump::dump_hooks_config(Some("json".to_string())).await;

    assert!(result.is_ok(), "Dump JSON should succeed: {result:?}");

    Ok(())
}

#[tokio::test]
async fn test_dump_hooks_config_yaml() -> Result<()> {
    // Test dumping configuration in YAML format
    let result = guardy::hooks::dump::dump_hooks_config(Some("yaml".to_string())).await;

    assert!(result.is_ok(), "Dump YAML should succeed: {result:?}");

    Ok(())
}

#[tokio::test]
async fn test_dump_hooks_config_toml() -> Result<()> {
    // Test dumping configuration in TOML format
    let result = guardy::hooks::dump::dump_hooks_config(Some("toml".to_string())).await;

    assert!(result.is_ok(), "Dump TOML should succeed: {result:?}");

    Ok(())
}

#[tokio::test]
async fn test_dump_hooks_config_default_format() -> Result<()> {
    // Test dumping configuration with default format (should be yaml)
    let result = guardy::hooks::dump::dump_hooks_config(None).await;

    assert!(
        result.is_ok(),
        "Dump default format should succeed: {result:?}"
    );

    Ok(())
}

#[tokio::test]
async fn test_dump_hooks_config_invalid_format() -> Result<()> {
    // Test dumping configuration with invalid format
    let result = guardy::hooks::dump::dump_hooks_config(Some("xml".to_string())).await;

    assert!(result.is_err(), "Dump invalid format should fail");

    let error_msg = result.unwrap_err().to_string();
    assert!(
        error_msg.contains("Unsupported output format"),
        "Error should mention unsupported format"
    );

    Ok(())
}

#[tokio::test]
async fn test_dump_lefthook_config() -> Result<()> {
    // Test dumping configuration in lefthook-compatible format
    let result = guardy::hooks::dump::dump_lefthook_config().await;

    assert!(
        result.is_ok(),
        "Dump lefthook config should succeed: {result:?}"
    );

    Ok(())
}

#[tokio::test]
async fn test_dump_json_is_valid() -> Result<()> {
    // Test that JSON output is actually valid JSON
    // We'll test the serialization logic directly
    let hooks_config = &guardy::config::CONFIG.hooks;

    let json_output = serde_json::to_string_pretty(&**hooks_config)?;

    // Verify it's valid JSON by parsing it back
    let parsed: serde_json::Value = serde_json::from_str(&json_output)?;

    // Basic structure checks
    assert!(parsed.is_object(), "JSON should be an object");

    Ok(())
}

#[tokio::test]
async fn test_dump_yaml_is_valid() -> Result<()> {
    // Test that YAML output is actually valid YAML
    let hooks_config = &guardy::config::CONFIG.hooks;

    let yaml_output = serde_yaml_bw::to_string(&**hooks_config)?;

    // Verify it's valid YAML by parsing it back
    let parsed: serde_yaml_bw::Value = serde_yaml_bw::from_str(&yaml_output)?;

    // Basic structure checks
    assert!(parsed.is_mapping(), "YAML should be a mapping");

    Ok(())
}

#[tokio::test]
async fn test_dump_toml_is_valid() -> Result<()> {
    // Test that TOML output is actually valid TOML
    let hooks_config = &guardy::config::CONFIG.hooks;

    let toml_output = toml::to_string_pretty(&**hooks_config)?;

    // Verify it's valid TOML by parsing it back
    let parsed: toml::Value = toml::from_str(&toml_output)?;

    // Basic structure checks
    assert!(parsed.is_table(), "TOML should be a table");

    Ok(())
}