#!/usr/bin/env bash # Baktainer Test Runner Script set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Default values RUN_UNIT=true RUN_INTEGRATION=false RUN_COVERAGE=false SETUP_CONTAINERS=false CLEANUP_CONTAINERS=false # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -u|--unit) RUN_UNIT=true RUN_INTEGRATION=false shift ;; -i|--integration) RUN_INTEGRATION=true RUN_UNIT=false shift ;; -a|--all) RUN_UNIT=true RUN_INTEGRATION=true shift ;; -c|--coverage) RUN_COVERAGE=true shift ;; -s|--setup) SETUP_CONTAINERS=true shift ;; --cleanup) CLEANUP_CONTAINERS=true shift ;; -h|--help) echo "Baktainer Test Runner" echo "" echo "Usage: $0 [OPTIONS]" echo "" echo "Options:" echo " -u, --unit Run unit tests only (default)" echo " -i, --integration Run integration tests only" echo " -a, --all Run all tests" echo " -c, --coverage Enable test coverage reporting" echo " -s, --setup Setup test containers before running" echo " --cleanup Cleanup test containers after running" echo " -h, --help Show this help message" echo "" echo "Examples:" echo " $0 # Run unit tests" echo " $0 -a -c # Run all tests with coverage" echo " $0 -i -s --cleanup # Run integration tests with container setup/cleanup" exit 0 ;; *) print_error "Unknown option: $1" echo "Use -h or --help for usage information" exit 1 ;; esac done # Check if we're in the correct directory if [[ ! -f "Gemfile" ]] || [[ ! -d "spec" ]]; then print_error "This script must be run from the app directory containing Gemfile and spec/" exit 1 fi # Check if bundle is available if ! command -v bundle &> /dev/null; then print_error "Bundler is not installed. Please install with: gem install bundler" exit 1 fi # Install dependencies if needed if [[ ! -d "vendor/bundle" ]] && [[ ! -f "Gemfile.lock" ]]; then print_status "Installing dependencies..." bundle install fi # Setup test containers if requested if [[ "$SETUP_CONTAINERS" = true ]] || [[ "$RUN_INTEGRATION" = true ]]; then print_status "Setting up test containers..." if [[ -f "spec/fixtures/docker-compose.test.yml" ]]; then docker-compose -f spec/fixtures/docker-compose.test.yml up -d print_status "Waiting for containers to be ready..." sleep 15 print_status "Test containers are ready" else print_warning "Test compose file not found, skipping container setup" fi fi # Function to cleanup containers cleanup_containers() { if [[ "$CLEANUP_CONTAINERS" = true ]] || [[ "$RUN_INTEGRATION" = true ]]; then print_status "Cleaning up test containers..." if [[ -f "spec/fixtures/docker-compose.test.yml" ]]; then docker-compose -f spec/fixtures/docker-compose.test.yml down -v print_status "Test containers cleaned up" fi fi } # Setup trap to cleanup on exit trap cleanup_containers EXIT # Set coverage environment variable if requested if [[ "$RUN_COVERAGE" = true ]]; then export COVERAGE=true print_status "Test coverage enabled" fi # Run tests based on options if [[ "$RUN_UNIT" = true ]] && [[ "$RUN_INTEGRATION" = true ]]; then print_status "Running all tests..." bundle exec rspec spec/ --format documentation --color elif [[ "$RUN_INTEGRATION" = true ]]; then print_status "Running integration tests..." bundle exec rspec spec/integration/ --format documentation --color --tag integration elif [[ "$RUN_UNIT" = true ]]; then print_status "Running unit tests..." bundle exec rspec spec/unit/ --format documentation --color fi # Show coverage report if enabled if [[ "$RUN_COVERAGE" = true ]] && [[ -f "coverage/index.html" ]]; then print_status "Test coverage report generated at: coverage/index.html" # Try to open coverage report if command -v xdg-open &> /dev/null; then print_status "Opening coverage report..." xdg-open coverage/index.html & elif command -v open &> /dev/null; then print_status "Opening coverage report..." open coverage/index.html & fi fi print_status "Tests completed successfully!"