A Rust application to showcase badge awards in the AT Protocol ecosystem.
at main 376 lines 9.8 kB view raw
1#!/bin/bash 2# Comprehensive Test Runner 3# This script runs tests against both SQLite and PostgreSQL backends 4 5set -e 6 7# Colors for output 8RED='\033[0;31m' 9GREEN='\033[0;32m' 10YELLOW='\033[1;33m' 11BLUE='\033[0;34m' 12CYAN='\033[0;36m' 13NC='\033[0m' # No Color 14 15# Configuration 16SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 17PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" 18 19# Test results tracking 20SQLITE_RESULT=0 21POSTGRES_RESULT=0 22TOTAL_START_TIME=$(date +%s) 23 24# Function to print colored output 25print_info() { 26 echo -e "${BLUE}[INFO]${NC} $1" 27} 28 29print_success() { 30 echo -e "${GREEN}[SUCCESS]${NC} $1" 31} 32 33print_warning() { 34 echo -e "${YELLOW}[WARNING]${NC} $1" 35} 36 37print_error() { 38 echo -e "${RED}[ERROR]${NC} $1" 39} 40 41print_header() { 42 echo -e "${CYAN}============================================${NC}" 43 echo -e "${CYAN} $1${NC}" 44 echo -e "${CYAN}============================================${NC}" 45} 46 47# Function to format time duration 48format_duration() { 49 local duration=$1 50 local minutes=$((duration / 60)) 51 local seconds=$((duration % 60)) 52 53 if [[ $minutes -gt 0 ]]; then 54 echo "${minutes}m ${seconds}s" 55 else 56 echo "${seconds}s" 57 fi 58} 59 60# Function to show usage 61usage() { 62 echo "Usage: $0 [OPTIONS] [TEST_ARGS]" 63 echo "" 64 echo "Options:" 65 echo " -h, --help Show this help message" 66 echo " -s, --sqlite Run only SQLite tests" 67 echo " -p, --postgres Run only PostgreSQL tests" 68 echo " -c, --clean Clean start for all backends" 69 echo " -k, --keep Keep PostgreSQL container running" 70 echo " -f, --fail-fast Stop on first failure" 71 echo " -v, --verbose Enable verbose output" 72 echo " --stats Show database statistics" 73 echo " --parallel Run tests in parallel (experimental)" 74 echo "" 75 echo "Examples:" 76 echo " $0 # Run tests on both backends" 77 echo " $0 --sqlite # Run only SQLite tests" 78 echo " $0 --postgres --keep # Run PostgreSQL tests, keep container" 79 echo " $0 --clean storage::tests # Clean start, test only storage module" 80 echo " $0 --fail-fast # Stop on first backend failure" 81} 82 83# Function to run SQLite tests 84run_sqlite_tests() { 85 print_header "RUNNING SQLITE TESTS" 86 87 local start_time=$(date +%s) 88 local args=() 89 90 if [[ "$CLEAN_START" == "true" ]]; then 91 args+=("--clean") 92 fi 93 94 if [[ "$SHOW_STATS" == "true" ]]; then 95 args+=("--stats" "--optimize") 96 fi 97 98 if [[ "$VERBOSE" == "true" ]]; then 99 args+=("--verbose") 100 fi 101 102 # Add test arguments 103 if [[ ${#TEST_ARGS[@]} -gt 0 ]]; then 104 args+=("--" "${TEST_ARGS[@]}") 105 fi 106 107 # Run SQLite tests 108 if "$SCRIPT_DIR/test-sqlite.sh" "${args[@]}"; then 109 local end_time=$(date +%s) 110 local duration=$((end_time - start_time)) 111 print_success "SQLite tests completed in $(format_duration $duration)" 112 SQLITE_RESULT=0 113 else 114 local end_time=$(date +%s) 115 local duration=$((end_time - start_time)) 116 print_error "SQLite tests failed after $(format_duration $duration)" 117 SQLITE_RESULT=1 118 119 if [[ "$FAIL_FAST" == "true" ]]; then 120 print_error "Fail-fast enabled, stopping execution" 121 exit 1 122 fi 123 fi 124} 125 126# Function to run PostgreSQL tests 127run_postgres_tests() { 128 print_header "RUNNING POSTGRESQL TESTS" 129 130 local start_time=$(date +%s) 131 local args=() 132 133 if [[ "$CLEAN_START" == "true" ]]; then 134 args+=("--clean") 135 fi 136 137 if [[ "$KEEP_CONTAINER" == "true" ]]; then 138 args+=("--keep") 139 fi 140 141 if [[ "$VERBOSE" == "true" ]]; then 142 args+=("--verbose") 143 fi 144 145 # Add test arguments 146 if [[ ${#TEST_ARGS[@]} -gt 0 ]]; then 147 args+=("--" "${TEST_ARGS[@]}") 148 fi 149 150 # Run PostgreSQL tests 151 if "$SCRIPT_DIR/test-postgres.sh" "${args[@]}"; then 152 local end_time=$(date +%s) 153 local duration=$((end_time - start_time)) 154 print_success "PostgreSQL tests completed in $(format_duration $duration)" 155 POSTGRES_RESULT=0 156 else 157 local end_time=$(date +%s) 158 local duration=$((end_time - start_time)) 159 print_error "PostgreSQL tests failed after $(format_duration $duration)" 160 POSTGRES_RESULT=1 161 162 if [[ "$FAIL_FAST" == "true" ]]; then 163 print_error "Fail-fast enabled, stopping execution" 164 exit 1 165 fi 166 fi 167} 168 169# Function to run tests in parallel 170run_parallel_tests() { 171 print_header "RUNNING TESTS IN PARALLEL" 172 print_warning "Parallel testing is experimental and may cause resource conflicts" 173 174 local sqlite_pid="" 175 local postgres_pid="" 176 177 # Start SQLite tests in background 178 if [[ "$RUN_SQLITE" == "true" ]]; then 179 print_info "Starting SQLite tests in background..." 180 run_sqlite_tests & 181 sqlite_pid=$! 182 fi 183 184 # Start PostgreSQL tests in background 185 if [[ "$RUN_POSTGRES" == "true" ]]; then 186 print_info "Starting PostgreSQL tests in background..." 187 run_postgres_tests & 188 postgres_pid=$! 189 fi 190 191 # Wait for both to complete 192 if [[ -n "$sqlite_pid" ]]; then 193 wait $sqlite_pid 194 SQLITE_RESULT=$? 195 fi 196 197 if [[ -n "$postgres_pid" ]]; then 198 wait $postgres_pid 199 POSTGRES_RESULT=$? 200 fi 201} 202 203# Function to show test summary 204show_summary() { 205 local total_end_time=$(date +%s) 206 local total_duration=$((total_end_time - TOTAL_START_TIME)) 207 208 print_header "TEST SUMMARY" 209 210 echo -e "Total execution time: $(format_duration $total_duration)" 211 echo "" 212 213 if [[ "$RUN_SQLITE" == "true" ]]; then 214 if [[ $SQLITE_RESULT -eq 0 ]]; then 215 echo -e "SQLite tests: ${GREEN}✓ PASSED${NC}" 216 else 217 echo -e "SQLite tests: ${RED}✗ FAILED${NC}" 218 fi 219 fi 220 221 if [[ "$RUN_POSTGRES" == "true" ]]; then 222 if [[ $POSTGRES_RESULT -eq 0 ]]; then 223 echo -e "PostgreSQL tests: ${GREEN}✓ PASSED${NC}" 224 else 225 echo -e "PostgreSQL tests: ${RED}✗ FAILED${NC}" 226 fi 227 fi 228 229 echo "" 230 231 local total_failures=$((SQLITE_RESULT + POSTGRES_RESULT)) 232 if [[ $total_failures -eq 0 ]]; then 233 print_success "All tests passed!" 234 return 0 235 else 236 print_error "$total_failures backend(s) failed" 237 return 1 238 fi 239} 240 241# Function to check prerequisites 242check_prerequisites() { 243 # Check if we're in the right directory 244 if [[ ! -f "Cargo.toml" ]] || [[ ! -d "src" ]]; then 245 print_error "This script must be run from the project root directory" 246 exit 1 247 fi 248 249 # Check if test scripts exist 250 if [[ ! -f "$SCRIPT_DIR/test-sqlite.sh" ]]; then 251 print_error "SQLite test script not found: $SCRIPT_DIR/test-sqlite.sh" 252 exit 1 253 fi 254 255 if [[ ! -f "$SCRIPT_DIR/test-postgres.sh" ]]; then 256 print_error "PostgreSQL test script not found: $SCRIPT_DIR/test-postgres.sh" 257 exit 1 258 fi 259} 260 261# Parse command line arguments 262RUN_SQLITE=true 263RUN_POSTGRES=true 264CLEAN_START=false 265KEEP_CONTAINER=false 266FAIL_FAST=false 267VERBOSE=false 268SHOW_STATS=false 269PARALLEL=false 270TEST_ARGS=() 271 272while [[ $# -gt 0 ]]; do 273 case $1 in 274 -h|--help) 275 usage 276 exit 0 277 ;; 278 -s|--sqlite) 279 RUN_SQLITE=true 280 RUN_POSTGRES=false 281 shift 282 ;; 283 -p|--postgres) 284 RUN_SQLITE=false 285 RUN_POSTGRES=true 286 shift 287 ;; 288 -c|--clean) 289 CLEAN_START=true 290 shift 291 ;; 292 -k|--keep) 293 KEEP_CONTAINER=true 294 shift 295 ;; 296 -f|--fail-fast) 297 FAIL_FAST=true 298 shift 299 ;; 300 -v|--verbose) 301 VERBOSE=true 302 shift 303 ;; 304 --stats) 305 SHOW_STATS=true 306 shift 307 ;; 308 --parallel) 309 PARALLEL=true 310 shift 311 ;; 312 --) 313 shift 314 TEST_ARGS+=("$@") 315 break 316 ;; 317 *) 318 TEST_ARGS+=("$1") 319 shift 320 ;; 321 esac 322done 323 324# Enable verbose output if requested 325if [[ "$VERBOSE" == "true" ]]; then 326 set -x 327fi 328 329# Main execution 330main() { 331 print_header "SHOWCASE TEST RUNNER" 332 333 # Pre-flight checks 334 check_prerequisites 335 336 # Show configuration 337 print_info "Test configuration:" 338 echo " SQLite tests: $(if [[ "$RUN_SQLITE" == "true" ]]; then echo "enabled"; else echo "disabled"; fi)" 339 echo " PostgreSQL tests: $(if [[ "$RUN_POSTGRES" == "true" ]]; then echo "enabled"; else echo "disabled"; fi)" 340 echo " Clean start: $(if [[ "$CLEAN_START" == "true" ]]; then echo "yes"; else echo "no"; fi)" 341 echo " Fail fast: $(if [[ "$FAIL_FAST" == "true" ]]; then echo "yes"; else echo "no"; fi)" 342 echo " Parallel mode: $(if [[ "$PARALLEL" == "true" ]]; then echo "yes"; else echo "no"; fi)" 343 344 if [[ ${#TEST_ARGS[@]} -gt 0 ]]; then 345 echo " Test arguments: ${TEST_ARGS[*]}" 346 fi 347 348 echo "" 349 350 # Run tests 351 if [[ "$PARALLEL" == "true" ]]; then 352 run_parallel_tests 353 else 354 # Sequential execution 355 if [[ "$RUN_SQLITE" == "true" ]]; then 356 run_sqlite_tests 357 fi 358 359 if [[ "$RUN_POSTGRES" == "true" ]]; then 360 run_postgres_tests 361 fi 362 fi 363 364 # Show summary and exit with appropriate code 365 if show_summary; then 366 exit 0 367 else 368 exit 1 369 fi 370} 371 372# Change to project root directory 373cd "$PROJECT_ROOT" 374 375# Run main function 376main "$@"