use crate::core::error::DecapodError;
use clap::Parser;
use std::process::Command;
#[derive(Parser, Debug)]
#[clap(
name = "gatling",
about = "Run gatling regression test across all CLI code paths"
)]
pub struct GatlingCli {
#[clap(long, default_value = "text")]
pub format: String,
#[clap(long)]
pub filter: Option<String>,
#[clap(long)]
pub nocapture: bool,
}
pub fn run_gatling_cli(cli: &GatlingCli) -> Result<(), DecapodError> {
let manifest_dir = find_manifest_dir()?;
let mut cmd = Command::new("cargo");
cmd.arg("test")
.arg("--test")
.arg("gatling")
.arg("--manifest-path")
.arg(manifest_dir.join("Cargo.toml"))
.arg("--");
if let Some(ref filter) = cli.filter {
cmd.arg(filter);
}
if cli.nocapture {
cmd.arg("--nocapture");
}
if cli.format == "json" {
cmd.args(["--format", "json", "-Z", "unstable-options"]);
}
let status = cmd.status().map_err(DecapodError::IoError)?;
if !status.success() {
return Err(DecapodError::ValidationError(format!(
"Gatling tests failed (exit {})",
status.code().unwrap_or(-1)
)));
}
Ok(())
}
fn find_manifest_dir() -> Result<std::path::PathBuf, DecapodError> {
if let Ok(exe) = std::env::current_exe() {
let mut dir = exe.parent().map(|p| p.to_path_buf());
while let Some(d) = dir {
if d.join("Cargo.toml").exists() {
return Ok(d);
}
dir = d.parent().map(|p| p.to_path_buf());
}
}
let mut cwd = std::env::current_dir().map_err(DecapodError::IoError)?;
loop {
if cwd.join("Cargo.toml").exists() {
return Ok(cwd);
}
if !cwd.pop() {
break;
}
}
Err(DecapodError::NotFound(
"Cannot find Cargo.toml — gatling tests require the decapod source checkout.".into(),
))
}