Skip to content

Commit 04d2d0d

Browse files
schlesseraclaude
andcommitted
Size the coverage shards to the package, and cope without sharding support
Every runner spends about a minute on its setup, so packages now get one coverage runner per 50 scenarios, up to the coverage-shards input. Versions of wp-cli/wp-cli-tests without sharding support ignore WP_CLI_TEST_SHARD and would run every scenario on every shard. With those, only the first shard runs the scenarios, so that each still runs once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0df7772 commit 04d2d0d

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎.github/workflows/reusable-functional.yml‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,31 @@ jobs:
233233
if: ${{ inputs.dbtype != 'sqlite' && !startsWith(inputs.os, 'windows') && !startsWith(inputs.os, 'macos') }}
234234
run: sudo rm -f /usr/bin/mysql /usr/bin/mysqldump
235235

236+
# Versions of wp-cli/wp-cli-tests before the sharding support run every scenario on
237+
# every shard. Then only the first shard runs them, so that they run once.
238+
- name: Check whether wp-cli/wp-cli-tests can split the scenarios
239+
id: shard
240+
if: ${{ inputs.shard != '' }}
241+
env:
242+
SHARD: ${{ inputs.shard }}
243+
run: |
244+
RUNNER=vendor/wp-cli/wp-cli-tests/bin/run-behat-tests
245+
[ -f "$RUNNER" ] || RUNNER=bin/run-behat-tests
246+
if [ -f "$RUNNER" ] && grep -q WP_CLI_TEST_SHARD "$RUNNER"; then
247+
echo "shard=${SHARD}" >> "$GITHUB_OUTPUT"
248+
elif [ "${SHARD%%/*}" = 1 ]; then
249+
echo "::warning::This version of wp-cli/wp-cli-tests cannot split the scenarios across runners, so shard ${SHARD} runs all of them."
250+
else
251+
echo "This version of wp-cli/wp-cli-tests cannot split the scenarios across runners, so shard 1 runs all of them and this one none."
252+
echo "skip=true" >> "$GITHUB_OUTPUT"
253+
fi
254+
236255
- name: Prepare test database
237-
if: ${{ inputs.dbtype != 'sqlite' }}
256+
if: ${{ inputs.dbtype != 'sqlite' && steps.shard.outputs.skip != 'true' }}
238257
run: composer prepare-tests
239258

240259
- name: Check Behat environment
260+
if: ${{ steps.shard.outputs.skip != 'true' }}
241261
env:
242262
WP_VERSION: '${{ inputs.wp }}'
243263
WP_CLI_TEST_DBTYPE: ${{ inputs.dbtype || 'mysql' }}
@@ -246,6 +266,7 @@ jobs:
246266
run: composer behat
247267

248268
- name: Run Behat
269+
if: ${{ steps.shard.outputs.skip != 'true' }}
249270
env:
250271
WP_VERSION: '${{ inputs.wp }}'
251272
WP_CLI_TEST_DBTYPE: ${{ inputs.dbtype || 'mysql' }}
@@ -258,7 +279,7 @@ jobs:
258279
WP_CLI_TEST_PARALLEL: ${{ inputs.parallel }}
259280
# The test framework spreads the scenarios over the shards by their expected cost,
260281
# the same way on every runner, so that together they run every scenario once.
261-
WP_CLI_TEST_SHARD: ${{ inputs.shard }}
282+
WP_CLI_TEST_SHARD: ${{ steps.shard.outputs.shard }}
262283
WP_CLI_TEST_DOCKER: auto
263284
run: |
264285
composer behat -- $BEHAT_ARGS || composer behat-rerun -- $BEHAT_ARGS
@@ -267,7 +288,7 @@ jobs:
267288
# finished, so that Codecov receives them as one upload and never reports on a part.
268289
- name: Name the code coverage report
269290
id: coverage_artifact
270-
if: ${{ inputs.coverage }}
291+
if: ${{ inputs.coverage && steps.shard.outputs.skip != 'true' }}
271292
env:
272293
SHARD: ${{ inputs.shard || 'all' }}
273294
PHP: ${{ inputs.php }}
@@ -276,7 +297,7 @@ jobs:
276297
run: echo "name=behat-coverage-${PHP}-${DRIVER}-p${PARALLEL}-${SHARD//\//-of-}" >> "$GITHUB_OUTPUT"
277298

278299
- name: Keep the code coverage report for the upload
279-
if: ${{ inputs.coverage }}
300+
if: ${{ inputs.coverage && steps.shard.outputs.skip != 'true' }}
280301
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
281302
with:
282303
name: ${{ steps.coverage_artifact.outputs.name }}

‎.github/workflows/reusable-testing.yml‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ on:
1919
required: false
2020
default: true
2121
coverage-shards:
22-
description: 'Number of runners to split the Behat tests with code coverage across.'
22+
description: 'Maximum number of runners to split the Behat tests with code coverage across. Packages get one runner per 50 scenarios, up to this number.'
2323
type: number
2424
required: false
2525
default: 4
@@ -482,6 +482,7 @@ jobs:
482482
INPUTS_MINIMUM_WP: ${{ inputs.minimum-wp }}
483483
WITH_COVERAGE: ${{ inputs.with-coverage }}
484484
COVERAGE_SHARDS: ${{ inputs.coverage-shards }}
485+
SCENARIOS_PER_SHARD: 50
485486
run: |
486487
if [[ $FILE_EXISTS == 'true' && $DOCS_ONLY != 'true' ]]; then
487488
MATRIX=$(jq -c \
@@ -515,9 +516,14 @@ jobs:
515516
' <<< "$BASE_MATRIX" )
516517
517518
# Collecting code coverage makes the Behat tests several times slower, so these
518-
# run as a job of their own, split across runners. An entry can set its own
519-
# number of shards with "coverage_shards".
520-
COVERAGE=$(jq -c --argjson shards "${COVERAGE_SHARDS}" '
519+
# run as a job of their own, split across runners. Every runner spends about a
520+
# minute on its setup, so small packages use fewer of them. An entry can set its
521+
# own number of shards with "coverage_shards".
522+
SCENARIOS=$(find features -name '*.feature' -exec cat {} + 2>/dev/null | grep -cE '^[[:space:]]*Scenario( Outline)?:' || true)
523+
SHARDS=$(( (SCENARIOS + SCENARIOS_PER_SHARD - 1) / SCENARIOS_PER_SHARD ))
524+
SHARDS=$(( SHARDS < 1 ? 1 : SHARDS > COVERAGE_SHARDS ? COVERAGE_SHARDS : SHARDS ))
525+
echo "Splitting the Behat tests with code coverage across ${SHARDS} runners, for ${SCENARIOS} scenarios."
526+
COVERAGE=$(jq -c --argjson shards "${SHARDS}" '
521527
[
522528
.include[] | select(.coverage == true) | . as $entry |
523529
($entry.coverage_shards // $shards) as $total |

0 commit comments

Comments
 (0)