diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-11 20:20:04 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-11 20:20:04 -0600 |
| commit | c28b7088b6fad045060a52b6e1a2249e876090e3 (patch) | |
| tree | a8fc26fd5365d4988d9206b32d94f51047cf0bcc /src/bin/test.rs | |
| parent | 19ca22e604f9bcdf6b25f973f81b2486b0dcb789 (diff) | |
refactor: extract domain model
Diffstat (limited to 'src/bin/test.rs')
| -rw-r--r-- | src/bin/test.rs | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/bin/test.rs b/src/bin/test.rs new file mode 100644 index 0000000..d2704c4 --- /dev/null +++ b/src/bin/test.rs @@ -0,0 +1,154 @@ +use std::env; +use std::process::{Command, exit}; + +fn main() { + let args: Vec<String> = 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 <command>"); + 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"); +} |
