xacli 0.2.1

A modern, developer-friendly CLI framework for Rust
Documentation
//! Test report command

use std::path::Path;
use xacli::testing::report::{junit::TestSuites, terminal, xacli::TestSuitesResult};
use xacli::Context;

/// Handler for `test report` command
pub fn report_command(ctx: &mut dyn Context) -> xacli::Result<()> {
    let info = ctx.info();

    let format: String = info
        .args
        .get("format")
        .and_then(|v| v.clone().try_into().ok())
        .unwrap_or_else(|| "terminal".to_string());

    let input: String = info
        .args
        .get("input")
        .and_then(|v| v.clone().try_into().ok())
        .unwrap_or_else(|| ".xacli/reports/report.json".to_string());

    let input_path = Path::new(&input);

    if !input_path.exists() {
        return Err(xacli::Error::custom(format!(
            "Report file does not exist: {}\n\nRun your test suite first to generate a report.",
            input
        )));
    }

    // Read the report
    let report = TestSuitesResult::from_file(input_path)
        .map_err(|e| xacli::Error::custom(format!("Failed to read report: {}", e)))?;

    // Display based on format
    match format.as_str() {
        "json" => {
            println!(
                "{}",
                report
                    .to_json()
                    .map_err(|e| xacli::Error::custom(e.to_string()))?
            );
        }
        "junit" => {
            let junit: TestSuites = (&report).into();
            let xml = junit
                .to_xml_pretty()
                .map_err(|e| xacli::Error::custom(format!("Failed to generate XML: {}", e)))?;
            println!("{}", xml);
        }
        _ => {
            terminal::print_report(&report);
        }
    }

    // Exit with appropriate code
    if report.failed() > 0 {
        std::process::exit(1);
    }

    Ok(())
}