@@ -269,11 +269,56 @@ jobs:
269269 - name : " Tests"
270270 run : " make tests"
271271
272+ mutation-testing-changed-files :
273+ name : " Mutation Testing: changed files"
274+ runs-on : " ubuntu-latest"
275+ if : github.event_name == 'pull_request'
276+
277+ permissions :
278+ contents : read
279+
280+ env :
281+ # The branch the diff is taken against - the branch this file lives on.
282+ # Flipped by the "Open X.Y.x" commit, like the push branch above.
283+ MUTATION_TESTING_BASE_BRANCH : " 2.3.x"
284+
285+ outputs :
286+ base-branch : ${{ env.MUTATION_TESTING_BASE_BRANCH }}
287+ any : ${{ steps.changed-files.outputs.any }}
288+ files : ${{ steps.changed-files.outputs.files }}
289+
290+ steps :
291+ - name : Harden the runner (Audit all outbound calls)
292+ uses : step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0
293+ with :
294+ egress-policy : audit
295+
296+ - name : " Checkout"
297+ uses : actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
298+ with :
299+ persist-credentials : false
300+
301+ # The same files Infection picks with --git-diff-base and its default AM
302+ # filter over its source directories
303+ - name : " Find changed source files"
304+ id : changed-files
305+ run : |
306+ git fetch --depth=1 origin "$MUTATION_TESTING_BASE_BRANCH"
307+ base=$(git merge-base "origin/$MUTATION_TESTING_BASE_BRANCH" HEAD || echo "origin/$MUTATION_TESTING_BASE_BRANCH")
308+ files=$(git diff "$base" --diff-filter=AM --name-only -- src build/PHPStan/Build | grep '\.php$' || true)
309+ echo "$files"
310+ {
311+ if [ -n "$files" ]; then echo "any=true"; else echo "any=false"; fi
312+ echo 'files<<EOF'
313+ echo "$files"
314+ echo 'EOF'
315+ } >> "$GITHUB_OUTPUT"
316+
272317 mutation-testing :
273318 name : " Mutation Testing"
274319 runs-on : " ubuntu-latest"
275- needs : ["tests"]
276- if : github.event_name == 'pull_request'
320+ needs : ["tests", "mutation-testing-changed-files" ]
321+ if : github.event_name == 'pull_request' && needs.mutation-testing-changed-files.outputs.any == 'true'
277322
278323 strategy :
279324 fail-fast : false
@@ -328,11 +373,6 @@ jobs:
328373 > infection.json5
329374 jq < infection.json5
330375
331- - name : " Determine default branch"
332- id : default-branch
333- run : |
334- echo "name=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p')" >> "$GITHUB_OUTPUT"
335-
336376 - name : " Restore result cache"
337377 uses : actions/cache/restore@caa296126883cff596d87d8935842f9db880ef25 # v5.1.0
338378 with :
@@ -341,10 +381,34 @@ jobs:
341381 restore-keys : |
342382 result-cache-v1-${{ matrix.php-version }}-
343383
384+ # Infection reads the coverage report of every source file it lists, and
385+ # the whole of src/ outgrew the runner's memory - only the mutated files
386+ # need it
387+ - name : " Restrict coverage to the changed source files"
388+ env :
389+ CHANGED_FILES : ${{ needs.mutation-testing-changed-files.outputs.files }}
390+ run : |
391+ echo "$CHANGED_FILES" > "$RUNNER_TEMP/changed-files.txt"
392+ php -- "$RUNNER_TEMP/changed-files.txt" <<'PHP'
393+ <?php
394+ $document = new DOMDocument();
395+ $document->load('phpunit.xml');
396+ $include = $document->getElementsByTagName('source')->item(0)->getElementsByTagName('include')->item(0);
397+ while ($include->firstChild !== null) {
398+ $include->removeChild($include->firstChild);
399+ }
400+ foreach (file($argv[1], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $file) {
401+ $include->appendChild($document->createElement('file', $file));
402+ }
403+ $document->save('phpunit-mutation-testing.xml');
404+ PHP
405+ grep -A5 "<source" phpunit-mutation-testing.xml
406+
344407 # see https://infection.github.io/guide/command-line-options.html#coverage
345408 - name : " Create coverage in parallel"
346409 run : |
347410 php -d pcov.enabled=1 tests/vendor/bin/paratest \
411+ --configuration=phpunit-mutation-testing.xml \
348412 --passthru-php="'-d' 'pcov.enabled=1'" \
349413 --coverage-xml=tmp/coverage/coverage-xml \
350414 --coverage-clover=tmp/coverage/clover.xml \
@@ -357,10 +421,12 @@ jobs:
357421 path : tmp/coverage
358422
359423 - name : " Run infection"
424+ env :
425+ BASE_BRANCH : ${{ needs.mutation-testing-changed-files.outputs.base-branch }}
360426 run : |
361- git fetch --depth=1 origin ${{ steps.default-branch.outputs.name }}
427+ git fetch --depth=1 origin "$BASE_BRANCH"
362428 infection \
363- --git-diff-base=origin/${{ steps.default-branch.outputs.name }} \
429+ --git-diff-base=" origin/$BASE_BRANCH" \
364430 --git-diff-lines \
365431 --coverage=tmp/coverage \
366432 --skip-initial-tests \
0 commit comments