|
| 1 | +name: Regression Test Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + |
| 6 | +jobs: |
| 7 | + regression-test: |
| 8 | + name: Regression test enforcement |
| 9 | + runs-on: ubuntu-latest |
| 10 | + steps: |
| 11 | + - name: Checkout repository |
| 12 | + uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + fetch-depth: 0 |
| 15 | + |
| 16 | + - name: Check for regression tests |
| 17 | + env: |
| 18 | + PR_TITLE: ${{ github.event.pull_request.title }} |
| 19 | + PR_LABELS: ${{ join(github.event.pull_request.labels.*.name, ',') }} |
| 20 | + run: | |
| 21 | + set -euo pipefail |
| 22 | +
|
| 23 | + BASE_REF="origin/${{ github.event.pull_request.base.ref }}" |
| 24 | +
|
| 25 | + # --- 1. Is this a fix PR? Check title first, then commit messages --- |
| 26 | + IS_FIX=false |
| 27 | +
|
| 28 | + if grep -qiE '^(fix(\(.*\))?|hotfix|bugfix):' <<< "$PR_TITLE"; then |
| 29 | + IS_FIX=true |
| 30 | + fi |
| 31 | +
|
| 32 | + if [ "$IS_FIX" = false ]; then |
| 33 | + COMMITS=$(git log --format='%s' "${BASE_REF}..HEAD") |
| 34 | + if grep -qiE '^(fix(\(.*\))?|hotfix|bugfix):' <<< "$COMMITS"; then |
| 35 | + IS_FIX=true |
| 36 | + fi |
| 37 | + fi |
| 38 | +
|
| 39 | + if [ "$IS_FIX" = false ]; then |
| 40 | + echo "Not a fix PR — skipping regression test check." |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | +
|
| 44 | + echo "Fix PR detected." |
| 45 | +
|
| 46 | + # --- 2. Skip label or commit message marker --- |
| 47 | + if grep -qF ',skip-regression-check,' <<< ",$PR_LABELS,"; then |
| 48 | + echo "skip-regression-check label present — skipping." |
| 49 | + exit 0 |
| 50 | + fi |
| 51 | +
|
| 52 | + COMMIT_BODIES=$(git log --format='%B' "${BASE_REF}..HEAD") |
| 53 | + if grep -qF '[skip-regression-check]' <<< "$COMMIT_BODIES"; then |
| 54 | + echo "[skip-regression-check] found in commit message — skipping." |
| 55 | + exit 0 |
| 56 | + fi |
| 57 | +
|
| 58 | + # --- 3. Exempt static-only / docs-only changes --- |
| 59 | + CHANGED_FILES=$(git diff --name-only "${BASE_REF}...HEAD") |
| 60 | +
|
| 61 | + if [ -z "$CHANGED_FILES" ]; then |
| 62 | + echo "No changed files — skipping." |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | +
|
| 66 | + ALL_EXEMPT=true |
| 67 | + while IFS= read -r file; do |
| 68 | + case "$file" in |
| 69 | + src/channels/web/static/*) ;; |
| 70 | + *.md) ;; |
| 71 | + *) ALL_EXEMPT=false; break ;; |
| 72 | + esac |
| 73 | + done <<< "$CHANGED_FILES" |
| 74 | +
|
| 75 | + if [ "$ALL_EXEMPT" = true ]; then |
| 76 | + echo "All changes are static assets or docs — skipping." |
| 77 | + exit 0 |
| 78 | + fi |
| 79 | +
|
| 80 | + # --- 4. Look for test changes --- |
| 81 | +
|
| 82 | + # Fast path: new test attributes or test modules in added lines. |
| 83 | + if git diff "${BASE_REF}...HEAD" -U0 -- '*.rs' | grep -qE '^\+.*(#\[test\]|#\[tokio::test\]|#\[cfg\(test\)\]|mod tests)'; then |
| 84 | + echo "Test changes found in .rs files." |
| 85 | + exit 0 |
| 86 | + fi |
| 87 | +
|
| 88 | + # Whole-function context: detect edits inside existing test functions. |
| 89 | + if git diff "${BASE_REF}...HEAD" -W -- '*.rs' | awk ' |
| 90 | + /^@@/ { if (has_test && has_add) { found=1; exit } has_test=0; has_add=0 } |
| 91 | + /^ .*#\[test\]/ || /^ .*#\[tokio::test\]/ || /^ .*#\[cfg\(test\)\]/ || /^ .*mod tests/ { has_test=1 } |
| 92 | + /^\+.*#\[test\]/ || /^\+.*#\[tokio::test\]/ || /^\+.*#\[cfg\(test\)\]/ || /^\+.*mod tests/ { has_test=1 } |
| 93 | + /^\+[^+]/ { has_add=1 } |
| 94 | + END { if (has_test && has_add) found=1; exit !found } |
| 95 | + '; then |
| 96 | + echo "Test changes found in existing test functions." |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | +
|
| 100 | + if grep -qE '^tests/' <<< "$CHANGED_FILES"; then |
| 101 | + echo "Test file changes found under tests/." |
| 102 | + exit 0 |
| 103 | + fi |
| 104 | +
|
| 105 | + # --- 5. No tests found --- |
| 106 | + echo "::warning::This PR looks like a bug fix but contains no test changes. Every fix should include a regression test. Add a #[test] or #[tokio::test], or apply the 'skip-regression-check' label if not feasible." |
| 107 | + exit 1 |
0 commit comments