use crate::utils::{check_cargo, current_dir};
use anyhow::{Context, Result};
use colored::*;
use std::path::Path;
use std::process::Command;
pub async fn run_tests(path: Option<&Path>, test_pattern: Option<&str>) -> Result<()> {
check_cargo().context("Cargo not found")?;
let project_dir = if let Some(p) = path {
p.to_path_buf()
} else {
current_dir()?
};
std::env::set_current_dir(&project_dir)
.with_context(|| format!("Failed to change to directory {}", project_dir.display()))?;
println!("{}", "Running plugin tests...".cyan().bold());
if let Some(pattern) = test_pattern {
println!(" {} {}", "Pattern:".bold(), pattern);
}
println!();
let mut cmd = Command::new("cargo");
cmd.arg("test");
if let Some(pattern) = test_pattern {
cmd.arg(pattern);
}
cmd.arg("--").arg("--nocapture");
let status = cmd.status().context("Failed to execute cargo test")?;
if !status.success() {
anyhow::bail!("Tests failed");
}
println!();
println!("{}", "All tests passed!".green().bold());
Ok(())
}