Files
FTC-Project-Gen/tests/run-tests.sh
2026-01-24 12:39:32 -06:00

271 lines
7.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# FTC Project Generator - Test Runner
# Copyright (c) 2026 Nexus Workshops LLC
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test results
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Helper functions
pass() {
echo -e "${GREEN}${NC} $1"
((TESTS_PASSED++))
((TESTS_RUN++))
}
fail() {
echo -e "${RED}${NC} $1"
((TESTS_FAILED++))
((TESTS_RUN++))
}
info() {
echo -e "${YELLOW}${NC} $1"
}
# Test suite selection
RUN_UNIT=true
RUN_SYSTEM=true
if [ "$1" = "unit" ]; then
RUN_SYSTEM=false
elif [ "$1" = "system" ]; then
RUN_UNIT=false
fi
echo "════════════════════════════════════════════════════════════════"
echo " FTC Project Generator - Test Suite"
echo "════════════════════════════════════════════════════════════════"
echo ""
# Unit Tests
if [ "$RUN_UNIT" = true ]; then
echo "Running Unit Tests..."
echo "────────────────────────────────────────────────────────────────"
# Source the library for testing
source "$PROJECT_ROOT/linux/lib.sh"
# Test: Template processing
TEST_DIR=$(mktemp -d)
echo "Hello {{PROJECT_NAME}}" > "$TEST_DIR/test.template"
export PROJECT_NAME="TestProject"
export FTC_SDK_DIR="/tmp/sdk"
export FTC_VERSION="v1.0.0"
export GENERATOR_VERSION="1.0.0-test"
process_template "$TEST_DIR/test.template" "$TEST_DIR/output.txt"
if grep -q "Hello TestProject" "$TEST_DIR/output.txt"; then
pass "Template processing replaces PROJECT_NAME"
else
fail "Template processing failed"
fi
# Test: Version extraction
VERS=$(get_generator_version)
if [ -n "$VERS" ]; then
pass "Version extraction works (got: $VERS)"
else
fail "Version extraction failed"
fi
# Test: Template files exist
TEMPLATE_DIR="$PROJECT_ROOT/linux/templates"
REQUIRED_TEMPLATES=(
"Pose2d.java"
"Drive.java"
"DriveTest.java"
"MecanumDrive.java"
"TeleOp.java"
"build.gradle.kts"
"settings.gradle.kts.template"
"gitignore.template"
"README.md.template"
"build.sh"
)
for template in "${REQUIRED_TEMPLATES[@]}"; do
if [ -f "$TEMPLATE_DIR/$template" ]; then
pass "Template exists: $template"
else
fail "Template missing: $template"
fi
done
rm -rf "$TEST_DIR"
echo ""
fi
# System Tests
if [ "$RUN_SYSTEM" = true ]; then
echo "Running System Tests..."
echo "────────────────────────────────────────────────────────────────"
# Create temporary directory for test projects
TEST_WORKSPACE=$(mktemp -d)
cd "$TEST_WORKSPACE"
export FTC_SDK_DIR="$TEST_WORKSPACE/test-sdk"
# Test: Create minimal mock SDK
info "Creating mock FTC SDK..."
mkdir -p "$FTC_SDK_DIR/TeamCode/src/main/java"
cd "$FTC_SDK_DIR"
git init > /dev/null 2>&1
git config user.email "test@test.com"
git config user.name "Test"
# Create minimal build.gradle.kts for SDK
cat > build.gradle.kts << 'EOF'
plugins {
java
}
EOF
git add . > /dev/null 2>&1
git commit -m "Initial commit" > /dev/null 2>&1
git tag -a v10.1.1 -m "Test version" > /dev/null 2>&1
cd "$TEST_WORKSPACE"
# Test: Project creation
info "Creating test project..."
"$PROJECT_ROOT/ftc-new-project" test-robot > /dev/null 2>&1
if [ -d "test-robot" ]; then
pass "Project directory created"
else
fail "Project directory not created"
fi
# Test: Project structure
REQUIRED_DIRS=(
"test-robot/src/main/java/robot/subsystems"
"test-robot/src/main/java/robot/hardware"
"test-robot/src/main/java/robot/opmodes"
"test-robot/src/test/java/robot/subsystems"
"test-robot/gradle/wrapper"
)
for dir in "${REQUIRED_DIRS[@]}"; do
if [ -d "$dir" ]; then
pass "Directory exists: $(basename $dir)"
else
fail "Directory missing: $(basename $dir)"
fi
done
# Test: Required files
REQUIRED_FILES=(
"test-robot/build.gradle.kts"
"test-robot/settings.gradle.kts"
"test-robot/.gitignore"
"test-robot/README.md"
"test-robot/build.sh"
"test-robot/deploy-to-robot.sh"
"test-robot/.ftc-generator-version"
"test-robot/src/main/java/robot/Pose2d.java"
"test-robot/src/main/java/robot/subsystems/Drive.java"
"test-robot/src/test/java/robot/subsystems/DriveTest.java"
)
for file in "${REQUIRED_FILES[@]}"; do
if [ -f "$file" ]; then
pass "File exists: $(basename $file)"
else
fail "File missing: $(basename $file)"
fi
done
# Test: Git repository initialized
if [ -d "test-robot/.git" ]; then
pass "Git repository initialized"
else
fail "Git repository not initialized"
fi
# Test: Version marker
if [ -f "test-robot/.ftc-generator-version" ]; then
VERSION=$(cat "test-robot/.ftc-generator-version")
pass "Version marker exists (v$VERSION)"
else
fail "Version marker missing"
fi
# Test: Project builds
cd test-robot
if ./gradlew build > /tmp/gradle-build.log 2>&1; then
pass "Project builds successfully"
else
fail "Project build failed (see /tmp/gradle-build.log)"
fi
# Test: Tests pass
if ./gradlew test > /tmp/gradle-test.log 2>&1; then
pass "Project tests pass"
else
fail "Project tests failed (see /tmp/gradle-test.log)"
fi
cd "$TEST_WORKSPACE"
# Test: Upgrade functionality
info "Testing project upgrade..."
# Modify a file that should be upgraded
echo "# Modified" >> test-robot/.gitignore
"$PROJECT_ROOT/ftc-new-project" test-robot --upgrade > /dev/null 2>&1
if [ -f "test-robot/.ftc-generator-version" ]; then
pass "Project upgraded successfully"
else
fail "Project upgrade failed"
fi
# Test: Duplicate project detection
if "$PROJECT_ROOT/ftc-new-project" test-robot 2>&1 | grep -q "already exists"; then
pass "Duplicate project detection works"
else
fail "Duplicate project detection failed"
fi
# Cleanup
cd /
rm -rf "$TEST_WORKSPACE"
echo ""
fi
# Summary
echo "════════════════════════════════════════════════════════════════"
echo " Test Summary"
echo "════════════════════════════════════════════════════════════════"
echo ""
echo "Total: $TESTS_RUN"
echo -e "${GREEN}Passed: $TESTS_PASSED${NC}"
echo -e "${RED}Failed: $TESTS_FAILED${NC}"
echo ""
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}✓ All tests passed!${NC}"
echo ""
exit 0
else
echo -e "${RED}✗ Some tests failed${NC}"
echo ""
exit 1
fi