use std::env; use std::process::{Command, exit}; fn main() { let args: Vec = env::args().collect(); if args.len() < 2 { print_usage(); return; } match args[1].as_str() { "unit" => { println!("Running unit tests..."); run_command(&["cargo", "test", "--lib"]); } "integration" => { println!("Running integration tests..."); run_command_allow_failure(&["bundle", "exec", "rspec"]); } "all" => { println!("Running all tests..."); println!("==================="); println!(); println!("1. Running Rust unit tests..."); run_command(&["cargo", "test"]); println!(); println!("2. Running Ruby integration tests..."); run_command_allow_failure(&["bundle", "exec", "rspec"]); } "watch" => { println!("Running tests in watch mode..."); if args.len() > 2 && args[2] == "integration" { run_command(&["bundle", "exec", "guard"]); } else { run_command(&["cargo", "watch", "-x", "test"]); } } "coverage" => { println!("Running tests with coverage..."); run_command(&["cargo", "tarpaulin", "--out", "Html"]); } "check" => { println!("Running cargo check..."); run_command(&["cargo", "check"]); } "lint" => { println!("Running linting..."); run_command(&["cargo", "clippy", "--", "-D", "warnings"]); } "fmt" => { println!("Running code formatting..."); run_command(&["cargo", "fmt"]); } "clean" => { println!("Cleaning test artifacts..."); run_command(&["cargo", "clean"]); run_command(&["rm", "-f", "oauth.db"]); run_command(&["rm", "-f", "test.db"]); } "server" => { println!("Starting test server..."); run_command(&["cargo", "run", "--bin", "sts"]); } "migrate" => { println!("Running migrations for tests..."); run_command(&["cargo", "run", "--bin", "migrate", "up"]); } "reset" => { println!("Resetting test environment..."); run_command(&["rm", "-f", "oauth.db"]); run_command(&["cargo", "run", "--bin", "migrate", "up"]); } _ => { eprintln!("Error: Unknown command '{}'", args[1]); print_usage(); exit(1); } } } fn run_command(cmd: &[&str]) { let mut command = Command::new(cmd[0]); if cmd.len() > 1 { command.args(&cmd[1..]); } let status = command.status().unwrap_or_else(|err| { eprintln!("Failed to execute command '{:?}': {}", cmd, err); exit(1); }); if !status.success() { eprintln!( "Command '{:?}' failed with exit code: {:?}", cmd, status.code() ); exit(1); } } fn run_command_allow_failure(cmd: &[&str]) { let mut command = Command::new(cmd[0]); if cmd.len() > 1 { command.args(&cmd[1..]); } let status = command.status().unwrap_or_else(|err| { eprintln!("Failed to execute command '{:?}': {}", cmd, err); exit(1); }); if !status.success() { eprintln!( "Command '{:?}' completed with exit code: {:?}", cmd, status.code() ); // Don't exit, just report the failure } } fn print_usage() { println!("OAuth2 STS Test Runner"); println!("====================="); println!(); println!("Usage:"); println!(" cargo run --bin test "); println!(); println!("Commands:"); println!(" unit Run Rust unit tests only"); println!(" integration Run Ruby integration tests only"); println!(" all Run all tests (unit + integration)"); println!(" watch Run tests in watch mode"); println!(" watch integration Run integration tests in watch mode"); println!(" coverage Run tests with coverage report"); println!(" check Run cargo check"); println!(" lint Run clippy linting"); println!(" fmt Run code formatting"); println!(" clean Clean test artifacts and databases"); println!(" server Start test server"); println!(" migrate Run database migrations"); println!(" reset Reset test environment (clean DB + migrate)"); println!(); println!("Examples:"); println!(" cargo run --bin test unit"); println!(" cargo run --bin test all"); println!(" cargo run --bin test watch"); println!(" cargo run --bin test coverage"); println!(" cargo run --bin test reset"); }