#!/bin/bash # Feature-Specific Test Runner Script # Runs BDD tests for a specific feature with proper isolation set -e # Check if feature name is provided if [ $# -eq 0 ]; then echo "โŒ Usage: $0 " echo "Available features: auth, config, greet, health, jwt" exit 1 fi FEATURE=$1 SCRIPTS_DIR=$(dirname `realpath ${BASH_SOURCE[0]}`) cd $SCRIPTS_DIR/.. # Validate feature name case $FEATURE in auth|config|greet|health|jwt) echo "๐Ÿงช Setting up ${FEATURE} feature tests..." ;; *) echo "โŒ Invalid feature: $FEATURE" echo "Available features: auth, config, greet, health, jwt" exit 1 ;; esac # Feature-specific configuration DATABASE="dance_lessons_coach_${FEATURE}_test" CONFIG="features/${FEATURE}/${FEATURE}-test-config.yaml" PORT=$(grep "port:" "$CONFIG" | awk '{print $2}') # Setup function setup_feature_environment() { echo "๐Ÿงช Setting up ${FEATURE} feature tests..." # Check if we're in CI environment if [ -n "$GITHUB_ACTIONS" ] || [ -n "$GITEA_ACTIONS" ]; then # CI environment - PostgreSQL is already running as a service echo "๐Ÿ—๏ธ CI environment detected" # Create database if it doesn't exist if ! psql -h postgres -p 5432 -U postgres -lqt | cut -d \| -f 1 | grep -qw "${DATABASE}"; then echo "๐Ÿ“ฆ Creating ${FEATURE} test database..." createdb -h postgres -p 5432 -U postgres "${DATABASE}" echo "โœ… ${FEATURE} test database created successfully!" else echo "โœ… ${FEATURE} test database already exists" fi else # Local environment - use docker compose echo "๐Ÿ’ป Local environment detected" # Check if PostgreSQL container is running, start it if not if ! docker ps --format '{{.Names}}' | grep -q "^dance-lessons-coach-postgres$"; then echo "๐Ÿ‹ Starting PostgreSQL container..." docker compose up -d postgres # Wait for PostgreSQL to be ready echo "โณ Waiting for PostgreSQL to be ready..." max_attempts=30 attempt=0 while [ $attempt -lt $max_attempts ]; do if docker exec dance-lessons-coach-postgres pg_isready -U postgres 2>/dev/null; then echo "โœ… PostgreSQL is ready!" break fi attempt=$((attempt + 1)) sleep 1 done if [ $attempt -eq $max_attempts ]; then echo "โŒ PostgreSQL failed to start" exit 1 fi else echo "โœ… PostgreSQL container is already running" fi # Create feature-specific database if docker exec dance-lessons-coach-postgres psql -U postgres -lqt | cut -d \| -f 1 | grep -qw "${DATABASE}"; then echo "โœ… ${FEATURE} test database already exists" else echo "๐Ÿ“ฆ Creating ${FEATURE} test database..." if docker exec dance-lessons-coach-postgres createdb -U postgres "${DATABASE}"; then echo "โœ… ${FEATURE} test database created successfully!" else echo "โŒ Failed to create ${FEATURE} test database" exit 1 fi fi fi } # Run tests function run_feature_tests() { echo "๐Ÿš€ Running ${FEATURE} feature tests..." # Set feature-specific environment variables export DLC_DATABASE_HOST="localhost" export DLC_DATABASE_PORT="5432" export DLC_DATABASE_USER="postgres" export DLC_DATABASE_PASSWORD="postgres" export DLC_DATABASE_NAME="${DATABASE}" export DLC_DATABASE_SSL_MODE="disable" export DLC_CONFIG_FILE="${CONFIG}" # Run tests with proper coverage measurement and tag exclusion set +e test_output=$(go test ./features/${FEATURE}/... -tags=~@flaky,~@todo,~@skip,@wip -v -cover -coverpkg=./... -coverprofile=coverage-${FEATURE}.out 2>&1) test_exit_code=$? set -e echo "$test_output" # Check for undefined steps if echo "$test_output" | grep -q "undefined"; then echo "โŒ FAILED: Found undefined steps in ${FEATURE} tests" exit 1 fi # Check for pending steps if echo "$test_output" | grep -q "pending"; then echo "โŒ FAILED: Found pending steps in ${FEATURE} tests" exit 1 fi # Check for skipped steps if echo "$test_output" | grep -q "skipped"; then echo "โŒ FAILED: Found skipped steps in ${FEATURE} tests" exit 1 fi # Check if tests passed if [ $test_exit_code -eq 0 ]; then echo "โœ… All ${FEATURE} feature tests passed successfully!" return 0 else echo "โŒ ${FEATURE} feature tests failed" return 1 fi } # Cleanup function cleanup_feature_environment() { echo "๐Ÿงน Cleaning up ${FEATURE} feature tests..." # Check if we're in CI environment if [ -n "$GITHUB_ACTIONS" ] || [ -n "$GITEA_ACTIONS" ]; then # CI environment - drop database echo "๐Ÿ—‘๏ธ Dropping ${FEATURE} test database..." dropdb -h postgres -p 5432 -U postgres "${DATABASE}" 2>/dev/null || true echo "โœ… ${FEATURE} test database cleaned up" else # Local environment - drop database echo "๐Ÿ—‘๏ธ Dropping ${FEATURE} test database..." docker exec dance-lessons-coach-postgres dropdb -U postgres "${DATABASE}" 2>/dev/null || true echo "โœ… ${FEATURE} test database cleaned up" fi } # Main execution setup_feature_environment run_feature_tests cleanup_feature_environment