mnemo_cards/tools/scripts/test_ci_locally.sh

215 lines
6.3 KiB
Bash
Raw Permalink Normal View History

2025-11-16 13:54:50 +00:00
#!/bin/bash
# Script for local testing of CI/CD pipeline
# Run this before pushing to ensure everything works
set -e
# Change to project root
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
cd "$PROJECT_ROOT"
echo "🚀 Testing CI/CD pipeline locally..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check prerequisites
print_status "Checking prerequisites..."
if ! command_exists flutter; then
print_error "Flutter not found. Please install Flutter SDK."
exit 1
fi
if ! command_exists dart; then
print_error "Dart not found. Please install Dart SDK."
exit 1
fi
print_success "Prerequisites check passed"
# Test backend
print_status "Testing backend..."
cd mnemo_cards_backend
if [ -f "pubspec.yaml" ]; then
print_status "Installing backend dependencies..."
dart pub get
print_status "Running backend code generation..."
dart run build_runner build --delete-conflicting-outputs || true
print_status "Running backend lints..."
dart analyze lib/ || print_warning "Backend linting failed"
dart format --set-exit-if-changed lib/ || print_warning "Backend formatting check failed"
print_status "Running backend tests..."
dart test --coverage || print_warning "Backend tests failed"
print_success "Backend tests completed"
else
print_warning "Backend pubspec.yaml not found, skipping..."
fi
cd ..
# Test web app
print_status "Testing web app..."
cd mnemo_cards_web_v2
if [ -f "pubspec.yaml" ]; then
print_status "Installing web app dependencies..."
flutter pub get
print_status "Running web app code generation..."
flutter pub run build_runner build --delete-conflicting-outputs || true
print_status "Running web app lints..."
flutter analyze lib/ || print_warning "Web app linting failed"
flutter format --set-exit-if-changed lib/ || print_warning "Web app formatting check failed"
print_status "Running web app tests..."
flutter test --coverage || print_warning "Web app tests failed"
print_success "Web app tests completed"
else
print_warning "Web app pubspec.yaml not found, skipping..."
fi
cd ..
# # Test mobile app
# print_status "Testing mobile app..."
# cd mnemo_cards
# if [ -f "pubspec.yaml" ]; then
# print_status "Installing mobile app dependencies..."
# flutter pub get
# print_status "Running mobile app code generation..."
# flutter pub run build_runner build --delete-conflicting-outputs || true
# print_status "Running mobile app lints..."
# flutter analyze lib/ || print_warning "Mobile app linting failed"
# flutter format --set-exit-if-changed lib/ || print_warning "Mobile app formatting check failed"
# print_status "Running mobile app tests..."
# flutter test --coverage || print_warning "Mobile app tests failed"
# print_success "Mobile app tests completed"
# else
# print_warning "Mobile app pubspec.yaml not found, skipping..."
# fi
# cd ..
# Test common packages
print_status "Testing common packages..."
common_projects=("mnemo_cards_common" "mnemo_cards_common_backend" "mnemo_cards_frontend_common")
for project in "${common_projects[@]}"; do
if [ -d "$project" ]; then
print_status "Testing $project..."
cd $project
if [ -f "pubspec.yaml" ]; then
flutter pub get || dart pub get || print_warning "Failed to get dependencies for $project"
flutter analyze lib/ 2>/dev/null || dart analyze lib/ 2>/dev/null || print_warning "Linting failed for $project"
fi
cd ..
fi
done
print_success "Common packages tests completed"
# Test CORS configuration
print_status "Testing CORS configuration..."
# Check backend CORS config
if [ -f "mnemo_cards_backend/lib/api/mnemo_shelf.dart" ]; then
if grep -q "Access-Control-Allow-Origin.*\*" "mnemo_cards_backend/lib/api/mnemo_shelf.dart"; then
print_success "Backend CORS configured correctly (allows all origins)"
else
print_warning "Backend CORS configuration may be too restrictive"
fi
fi
# Check web app nginx config for conflicting headers
if [ -f "mnemo_cards_web_v2/deploy/nginx.conf" ]; then
if grep -q "^[[:space:]]*add_header Cross-Origin-Embedder-Policy.*require-corp" "mnemo_cards_web_v2/deploy/nginx.conf" || grep -q "^[[:space:]]*add_header Cross-Origin-Opener-Policy.*same-origin" "mnemo_cards_web_v2/deploy/nginx.conf"; then
print_warning "Web app nginx has active COEP/COOP headers that may conflict with CORS"
else
print_success "Web app nginx CORS configuration looks good (COEP/COOP headers disabled)"
fi
fi
print_success "CORS configuration check completed"
# Check for uncommitted changes
print_status "Checking for uncommitted changes..."
if [ -n "$(git status --porcelain)" ]; then
print_warning "You have uncommitted changes:"
git status --short
print_warning "Please commit or stash them before pushing"
else
print_success "No uncommitted changes"
fi
# Check workflow files
print_status "Validating workflow files..."
if [ -d ".forgejo/workflows" ]; then
for workflow in .forgejo/workflows/*.yml; do
if [ -f "$workflow" ]; then
print_status "Validating $workflow..."
# Basic YAML syntax check
if command_exists yamllint; then
yamllint "$workflow" || print_warning "YAML syntax issue in $workflow"
else
python3 -c "import yaml; yaml.safe_load(open('$workflow'))" || print_warning "YAML syntax issue in $workflow"
fi
fi
done
print_success "Workflow validation completed"
else
print_warning "No .forgejo/workflows directory found"
fi
# Summary
print_success "Local CI/CD testing completed!"
print_status "If all tests passed, you can safely push your changes."
print_status "Don't forget to:"
echo " 1. Set up secrets in Forgejo if not done yet"
echo " 2. Test deployment scripts locally if possible"
echo " 3. Check that all required servers are accessible"
exit 0