pets_tools 0.2.0

MusKitty multi-repo source management CLI — one command to clone, build, test, push, and publish 9 independent crate repositories
//! `pets test <project>` — run tests across all crates in topological order.

use crate::cargo_ops;
use crate::config::Config;
use crate::topology::Topology;
use crate::ui;
use crate::TestArgs;
use std::collections::HashSet;

pub fn run(config: Config, args: &TestArgs) {
    let topo = match Topology::compute(&config) {
        Ok(t) => t,
        Err(e) => {
            ui::error(&format!("Topology error: {}", e));
            std::process::exit(1);
        }
    };

    ui::heading(&format!(
        "Testing {} — {} crates",
        config.project_name,
        config.repos.len(),
    ));

    let only_set: Option<HashSet<&str>> = args
        .only
        .as_ref()
        .map(|s| s.split(',').map(|n| n.trim()).collect());

    // Collect results for summary
    #[derive(Default)]
    struct CrateResult {
        name: String,
        lib_passed: u32,
        lib_failed: u32,
        int_passed: u32,
        int_failed: u32,
        lib_ran: bool,
        int_ran: bool,
        error: Option<String>,
    }

    let mut results: Vec<CrateResult> = Vec::new();
    let mut total_passed = 0u32;
    let mut total_failed = 0u32;
    let mut overall_ok = true;

    for level in &topo.levels {
        for &crate_idx in level {
            let repo = &config.repos[crate_idx];

            if let Some(ref only) = only_set {
                if !only.contains(repo.name.as_str()) {
                    continue;
                }
            }

            let mut cr = CrateResult {
                name: repo.name.clone(),
                ..Default::default()
            };

            ui::info(&repo.name);

            // Lib tests
            if !args.no_integration {
                // lib tests always run unless --no-integration? Actually --lib-only means ONLY lib
                // Default: run both lib and integration
            }

            match cargo_ops::test_lib(&repo.abs_dir) {
                Ok(out) => {
                    cr.lib_passed = out.passed;
                    cr.lib_failed = out.failed;
                    cr.lib_ran = true;
                    total_passed += out.passed;
                    total_failed += out.failed;
                    if !out.success {
                        overall_ok = false;
                    }
                }
                Err(e) => {
                    cr.error = Some(e);
                    overall_ok = false;
                }
            }

            // Integration tests (unless --lib-only)
            if !args.lib_only {
                match cargo_ops::test_integration(&repo.abs_dir) {
                    Ok(out) => {
                        cr.int_passed = out.passed;
                        cr.int_failed = out.failed;
                        cr.int_ran = true;
                        total_passed += out.passed;
                        total_failed += out.failed;
                        if !out.success {
                            overall_ok = false;
                        }
                    }
                    Err(e) => {
                        if cr.error.is_none() {
                            cr.error = Some(e);
                        }
                        overall_ok = false;
                    }
                }
            }

            results.push(cr);
        }
    }

    // Summary table
    ui::separator();
    ui::heading("Test Summary");
    println!(
        "  {:<30} {:>8} {:>8} {:>10} {:>12}",
        "CRATE", "LIB OK", "LIB FAIL", "INT OK", "INT FAIL"
    );
    println!("  {}", "-".repeat(75));

    for r in &results {
        let lib_ok = if r.lib_ran {
            r.lib_passed.to_string()
        } else {
            "—".into()
        };
        let lib_fail = if r.lib_ran {
            r.lib_failed.to_string()
        } else {
            "—".into()
        };
        let int_ok = if r.int_ran {
            r.int_passed.to_string()
        } else {
            "—".into()
        };
        let int_fail = if r.int_ran {
            r.int_failed.to_string()
        } else {
            "—".into()
        };

        if let Some(ref err) = r.error {
            println!(
                "  {:<30} {:>8} {:>8} {:>10} {:>12}  \x1b[31mERROR\x1b[0m",
                r.name, lib_ok, lib_fail, int_ok, int_fail,
            );
            println!("    \x1b[31m{}\x1b[0m", err);
        } else {
            println!(
                "  {:<30} {:>8} {:>8} {:>10} {:>12}",
                r.name, lib_ok, lib_fail, int_ok, int_fail,
            );
        }
    }

    ui::separator();
    if overall_ok {
        ui::success(&format!("All tests passed — {} total", total_passed));
    } else {
        ui::error(&format!(
            "Tests: {} passed, {} failed",
            total_passed, total_failed
        ));
        std::process::exit(1);
    }
}