forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
Documentation
//! Coverage tests: check.rs — cmd_check with filters, cmd_test with filters.
//! Also covers doctor.rs — cmd_doctor edge cases, cmd_doctor_network edge cases.

#![allow(unused_imports)]
use super::check::*;
use super::check_test_runners::RunnerOpts;
use super::dispatch_notify::*;
use super::dispatch_notify_custom::*;
use super::doctor::*;
use super::secrets::*;
use super::test_fixtures::*;
use std::path::Path;

#[cfg(test)]
mod tests {
    use super::*;

    // ─── check.rs — cmd_check with filters ────────────────────────

    fn write_check_config(dir: &std::path::Path) -> std::path::PathBuf {
        let file = dir.join("forjar.yaml");
        std::fs::write(
            &file,
            r#"
version: "1.0"
name: check-test
machines:
  local:
    hostname: localhost
    addr: 127.0.0.1
resources:
  pkg1:
    type: package
    machine: local
    name: coreutils
    tags: [base, system]
  cfg1:
    type: file
    machine: local
    path: /tmp/forjar-check-test.txt
    content: "hello"
    tags: [config]
"#,
        )
        .unwrap();
        file
    }

    #[test]
    fn test_cmd_check_with_tag_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        // Filter by tag "config" — should skip "pkg1" (base,system tags)
        let result = cmd_check(&config, None, None, Some("config"), false, false);
        // May fail because check script might not exist for file, but exercises filter code
        let _ = result;
    }

    #[test]
    fn test_cmd_check_with_tag_filter_json() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, None, None, Some("config"), true, false);
    }

    #[test]
    fn test_cmd_check_with_resource_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, None, Some("pkg1"), None, false, false);
    }

    #[test]
    fn test_cmd_check_with_resource_filter_nonexistent() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, None, Some("nonexistent"), None, false, false);
    }

    #[test]
    fn test_cmd_check_with_machine_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, Some("local"), None, None, false, false);
    }

    #[test]
    fn test_cmd_check_with_machine_filter_nonexistent() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, Some("nonexistent"), None, None, false, false);
    }

    #[test]
    fn test_cmd_check_verbose() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, None, None, None, false, true);
    }

    #[test]
    fn test_cmd_check_json_output() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(&config, None, None, None, true, false);
    }

    #[test]
    fn test_cmd_check_combined_filters() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_check(
            &config,
            Some("local"),
            Some("pkg1"),
            Some("base"),
            false,
            false,
        );
    }

    #[test]
    fn test_cmd_check_invalid_config() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("bad.yaml");
        std::fs::write(&file, "invalid: [[[").unwrap();
        let result = cmd_check(&file, None, None, None, false, false);
        assert!(result.is_err());
    }

    // ─── check.rs — cmd_test ──────────────────────────────────────

    #[test]
    fn test_cmd_test_basic() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, None, None, None, false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_json() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, None, None, None, true, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_verbose() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, None, None, None, false, true, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_with_tag_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, None, Some("base"), None, false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_with_resource_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, Some("cfg1"), None, None, false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_with_machine_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, Some("local"), None, None, None, false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_with_group_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        // No resources have resource_group set, so all skip
        let _ = cmd_test(&config, None, None, None, Some("web"), false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_with_nonexistent_resource_filter() {
        let dir = tempfile::tempdir().unwrap();
        let config = write_check_config(dir.path());
        let _ = cmd_test(&config, None, Some("nonexistent"), None, None, false, false, &RunnerOpts::default());
    }

    #[test]
    fn test_cmd_test_invalid_config() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("bad.yaml");
        std::fs::write(&file, "invalid: [[[").unwrap();
        let result = cmd_test(&file, None, None, None, None, false, false, &RunnerOpts::default());
        assert!(result.is_err());
    }
}