mvp-factory-openhands/test-scripts/advanced_build_test.sh

244 lines
6.7 KiB
Bash

#!/bin/bash
# Advanced Build and Test Automation Script
# Repository: gitadmin/test-repo-REAL-WORKING
# Branch: main
# Latest Commit: Testing
set -e # Exit on any error
echo "============================================"
echo "Build and Test Automation for Test Repository"
echo "============================================"
echo "Repository: gitadmin/test-repo-REAL-WORKING"
echo "Branch: main"
echo "Latest Commit: Testing"
echo "Timestamp: $(date)"
echo "============================================"
echo
# Function to print colored output
print_status() {
echo -e "\033[1;34m[INFO]\033[0m $1"
}
print_success() {
echo -e "\033[1;32m[SUCCESS]\033[0m $1"
}
print_error() {
echo -e "\033[1;31m[ERROR]\033[0m $1"
}
print_warning() {
echo -e "\033[1;33m[WARNING]\033[0m $1"
}
# Function to detect project type
detect_project_type() {
if [ -f "package.json" ]; then
echo "nodejs"
elif [ -f "requirements.txt" ] || [ -f "pyproject.toml" ]; then
echo "python"
elif [ -f "Dockerfile" ]; then
echo "docker"
elif [ -f "Makefile" ]; then
echo "make"
elif [ -f "pom.xml" ]; then
echo "maven"
elif [ -f "build.gradle" ] || [ -f "build.gradle.kts" ]; then
echo "gradle"
elif [ -f "go.mod" ]; then
echo "go"
else
echo "unknown"
fi
}
# Function to install dependencies
install_dependencies() {
local project_type=$1
case $project_type in
"nodejs")
print_status "Installing Node.js dependencies..."
if command -v npm &> /dev/null; then
npm install
else
print_error "npm not found. Installing Node.js..."
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
npm install
fi
;;
"python")
print_status "Installing Python dependencies..."
if command -v pip3 &> /dev/null; then
pip3 install -r requirements.txt
else
print_error "pip3 not found. Installing Python..."
sudo apt-get update
sudo apt-get install -y python3 python3-pip
pip3 install -r requirements.txt
fi
;;
"docker")
print_status "Docker project detected. No dependency installation needed."
;;
*)
print_warning "Unknown project type. Skipping dependency installation."
;;
esac
}
# Function to build project
build_project() {
local project_type=$1
print_status "Building project..."
case $project_type in
"nodejs")
if grep -q '"build"' package.json; then
npm run build
else
print_warning "No build script found in package.json"
fi
;;
"python")
print_status "Python projects typically don't need explicit build steps"
# Could add setup.py build or similar
;;
"docker")
print_status "Building Docker image..."
docker build -t test-repo-real-working .
;;
"maven")
mvn clean compile
;;
"gradle")
./gradlew build
;;
"go")
go build .
;;
*)
print_warning "No build process defined for this project type"
;;
esac
}
# Function to run tests
run_tests() {
local project_type=$1
print_status "Running tests..."
case $project_type in
"nodejs")
if grep -q '"test"' package.json; then
npm test
else
print_warning "No test script found in package.json"
fi
;;
"python")
if command -v pytest &> /dev/null; then
pytest
elif command -v python3 &> /dev/null; then
python3 -m unittest discover
else
print_warning "No Python testing framework found"
fi
;;
"docker")
if [ -f "docker-compose.yml" ]; then
docker-compose run test
else
print_warning "No docker-compose test configuration found"
fi
;;
"maven")
mvn test
;;
"gradle")
./gradlew test
;;
*)
# Try to find test files manually
if find . -name "*test*" -o -name "*spec*" | grep -q .; then
print_status "Found test files, but no standard test runner configured"
find . -name "*test*" -o -name "*spec*"
else
print_warning "No tests found"
fi
;;
esac
}
# Function to generate test report
generate_report() {
local exit_code=$1
echo
echo "============================================"
echo "Build and Test Report"
echo "============================================"
echo "Repository: gitadmin/test-repo-REAL-WORKING"
echo "Branch: main"
echo "Latest Commit: Testing"
echo "Project Type: $PROJECT_TYPE"
echo "Build Status: $([ $exit_code -eq 0 ] && echo "SUCCESS" || echo "FAILED")"
echo "Exit Code: $exit_code"
echo "Timestamp: $(date)"
echo "============================================"
if [ $exit_code -eq 0 ]; then
print_success "Build and test completed successfully!"
else
print_error "Build and test failed with exit code $exit_code"
fi
return $exit_code
}
# Main execution
main() {
# Step 1: Clone or update repository
if [ ! -d "test-repo-REAL-WORKING" ]; then
print_status "Cloning repository gitadmin/test-repo-REAL-WORKING..."
git clone -b main https://github.com/gitadmin/test-repo-REAL-WORKING.git
cd test-repo-REAL-WORKING
else
print_status "Repository exists. Updating..."
cd test-repo-REAL-WORKING
git pull origin main
fi
# Step 2: Show repository information
print_status "Repository information:"
echo "Current directory: $(pwd)"
echo "Git branch: $(git branch --show-current)"
echo "Latest commit: $(git log -1 --oneline)"
echo "Repository status:"
git status --porcelain | head -10
# Step 3: Detect project type
PROJECT_TYPE=$(detect_project_type)
print_status "Detected project type: $PROJECT_TYPE"
# Step 4: Install dependencies
install_dependencies $PROJECT_TYPE
# Step 5: Build project
build_project $PROJECT_TYPE
# Step 6: Run tests
run_tests $PROJECT_TYPE
# Step 7: Generate report
generate_report $?
}
# Run main function
main "$@"