summaryrefslogtreecommitdiff
path: root/src/bin/test.rs
blob: d2704c435ecfd362eb4928c0165fc1cee9325ed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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");
}