|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Validate that testkit is a git submodule with correct commit |
| 4 | +# This prevents running tests with mismatched testkit versions |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +PROJECT_ROOT="$SCRIPT_DIR/.." |
| 10 | + |
| 11 | +# Docker environment: configure git safe directory |
| 12 | +if [ -f "/.dockerenv" ]; then |
| 13 | + git config --global --add safe.directory "$PROJECT_ROOT" 2>/dev/null || true |
| 14 | + git config --global --add safe.directory "$PROJECT_ROOT/testkit" 2>/dev/null || true |
| 15 | +fi |
| 16 | + |
| 17 | +if [ ! -d "$PROJECT_ROOT/testkit" ]; then |
| 18 | + echo " ERROR: testkit directory not found" |
| 19 | + echo " Initialize with: cd testkit-backend && ./testkit.sh" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +EXPECTED_COMMIT=$(cd "$PROJECT_ROOT" && git ls-tree HEAD testkit 2>/dev/null | awk '{print $3}' || echo "") |
| 24 | + |
| 25 | +if [ -z "$EXPECTED_COMMIT" ]; then |
| 26 | + echo " ERROR: Could not determine expected testkit commit" |
| 27 | + echo " testkit must be configured as a git submodule" |
| 28 | + echo " Ensure .gitmodules is properly configured and run:" |
| 29 | + echo " cd $PROJECT_ROOT && git submodule add https://github.com/neo4j-drivers/testkit.git testkit" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +ACTUAL_COMMIT=$(cd "$PROJECT_ROOT/testkit" && git rev-parse HEAD 2>/dev/null || echo "UNKNOWN") |
| 34 | + |
| 35 | +if [ "$ACTUAL_COMMIT" = "UNKNOWN" ]; then |
| 36 | + echo " ERROR: Could not read testkit commit (git rev-parse failed)" |
| 37 | + echo " Testkit may be corrupted. Try re-initializing:" |
| 38 | + echo " cd testkit-backend && ./testkit.sh --clean" |
| 39 | + exit 1 |
| 40 | +fi |
| 41 | + |
| 42 | +TESTKIT_STATUS=$(cd "$PROJECT_ROOT/testkit" && git status --porcelain 2>/dev/null | wc -l) |
| 43 | +if [ "$TESTKIT_STATUS" -gt 0 ]; then |
| 44 | + echo " WARNING: testkit has uncommitted changes!" |
| 45 | + echo "" |
| 46 | + echo " Status of testkit:" |
| 47 | + cd "$PROJECT_ROOT/testkit" && git status --short | sed 's/^/ /' |
| 48 | + echo "" |
| 49 | + echo " This may cause inconsistent test results. Consider:" |
| 50 | + echo " 1. Committing changes if they're intentional:" |
| 51 | + echo " cd $PROJECT_ROOT/testkit && git add . && git commit -m 'message'" |
| 52 | + echo " 2. Discarding changes if they're temporary:" |
| 53 | + echo " cd $PROJECT_ROOT/testkit && git checkout . && git clean -fd" |
| 54 | + echo "" |
| 55 | +fi |
| 56 | + |
| 57 | +if [ "$EXPECTED_COMMIT" != "$ACTUAL_COMMIT" ]; then |
| 58 | + echo " ERROR: Testkit commit mismatch!" |
| 59 | + echo " Expected commit: $EXPECTED_COMMIT" |
| 60 | + echo " Actual commit: $ACTUAL_COMMIT" |
| 61 | + echo "" |
| 62 | + |
| 63 | + if [ -n "$CI" ] || [ -n "$GITHUB_ACTIONS" ] || [ -n "$GITLAB_CI" ] || [ -n "$CIRCLECI" ]; then |
| 64 | + echo " Running in CI environment - attempting automatic submodule update..." |
| 65 | + git submodule update --checkout 2>/dev/null || true |
| 66 | + |
| 67 | + ACTUAL_COMMIT=$(cd "$PROJECT_ROOT/testkit" && git rev-parse HEAD 2>/dev/null || echo "UNKNOWN") |
| 68 | + if [ "$EXPECTED_COMMIT" = "$ACTUAL_COMMIT" ]; then |
| 69 | + echo " Submodule updated successfully!" |
| 70 | + echo " Testkit is now at: ${ACTUAL_COMMIT:0:7}" |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | + fi |
| 74 | + |
| 75 | + echo " Fix this by running one of:" |
| 76 | + echo "" |
| 77 | + echo " 1. Update the submodule to the pinned commit:" |
| 78 | + echo " cd $PROJECT_ROOT && git submodule update --checkout" |
| 79 | + echo "" |
| 80 | + echo " 2. Re-initialize testkit completely:" |
| 81 | + echo " cd testkit-backend && ./testkit.sh --clean" |
| 82 | + echo "" |
| 83 | + echo " 3. Manually update to the expected commit:" |
| 84 | + echo " cd $PROJECT_ROOT/testkit && git fetch && git checkout $EXPECTED_COMMIT && cd -" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +echo " Testkit commit validated (${ACTUAL_COMMIT:0:7})" |
| 89 | +exit 0 |
0 commit comments