Skip to content

Commit a7e2b75

Browse files
authored
Merge pull request #316 from wp-cli/ci/coverage-shards
Split the Behat tests with code coverage across runners
2 parents 5c1972b + 04d2d0d commit a7e2b75

2 files changed

Lines changed: 160 additions & 20 deletions

File tree

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

Lines changed: 68 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ on:
2424
type: boolean
2525
required: false
2626
default: false
27+
coverage-driver:
28+
description: 'Code coverage driver, either `pcov` or `xdebug`.'
29+
type: string
30+
required: false
31+
default: 'pcov'
32+
shard:
33+
description: 'Share of the scenarios to run, as `<index>/<total>`, for example `2/3`. Empty to run all of them.'
34+
type: string
35+
required: false
36+
default: ''
37+
parallel:
38+
description: 'Number of parallel workers on the runner.'
39+
type: number
40+
required: false
41+
default: 4
2742
os:
2843
description: 'Runner to use. Defaults to ubuntu-22.04, or the RUNNERS_NAME repository variable when set.'
2944
type: string
@@ -40,7 +55,7 @@ jobs:
4055
# anything omitted here is not shown anywhere. The database version is spelled
4156
# out because "MySQL" alone rendered the mysql-8.0 and mysql-8.4 legs
4257
# identically.
43-
name: Behat | PHP ${{ inputs.php }} | WP ${{ inputs.wp }} | ${{ inputs.dbtype == 'sqlite' && 'SQLite' || inputs.mysql || 'MySQL' }}${{ inputs.object_cache == 'sqlite' && ' (Obj Cache)' || '' }}${{ inputs.coverage && ' (with coverage)' || '' }}${{ startsWith( inputs.os, 'windows' ) && ' (Windows)' || '' }}${{ startsWith( inputs.os, 'macos' ) && ' (macOS)' || '' }}
58+
name: Behat | PHP ${{ inputs.php }} | WP ${{ inputs.wp }} | ${{ inputs.dbtype == 'sqlite' && 'SQLite' || inputs.mysql || 'MySQL' }}${{ inputs.object_cache == 'sqlite' && ' (Obj Cache)' || '' }}${{ inputs.coverage && format( ' (with coverage{0})', inputs.shard && format( ', {0}', inputs.shard ) || '' ) || '' }}${{ startsWith( inputs.os, 'windows' ) && ' (Windows)' || '' }}${{ startsWith( inputs.os, 'macos' ) && ' (macOS)' || '' }}
4459
# Repositories with a heavy Behat suite can point the default Linux legs at a
4560
# larger runner by setting the `RUNNERS_NAME` repository variable, without
4661
# having to fork this workflow. Explicit macOS/Windows legs are unaffected.
@@ -166,9 +181,13 @@ jobs:
166181
uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # v2
167182
with:
168183
php-version: '${{ inputs.php }}'
169-
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
184+
# PCOV only instruments the files below pcov.directory. Left unset, it would pick the
185+
# working directory of every `wp` process the scenarios run, which is a temporary
186+
# WordPress install, and collect nothing. Xdebug instead instruments every file it
187+
# loads, all of WordPress included, which makes it several times slower.
188+
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On${{ inputs.coverage && inputs.coverage-driver == 'pcov' && format( ', pcov.directory={0}, "pcov.exclude=~/vendor/~"', github.workspace ) || '' }}
170189
extensions: gd, imagick, mysql, zip, pdo_sqlite, exif, fileinfo
171-
coverage: ${{ inputs.coverage && 'xdebug' || 'none' }}
190+
coverage: ${{ inputs.coverage && inputs.coverage-driver || 'none' }}
172191
tools: composer
173192
env:
174193
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -214,11 +233,31 @@ jobs:
214233
if: ${{ inputs.dbtype != 'sqlite' && !startsWith(inputs.os, 'windows') && !startsWith(inputs.os, 'macos') }}
215234
run: sudo rm -f /usr/bin/mysql /usr/bin/mysqldump
216235

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+
217255
- name: Prepare test database
218-
if: ${{ inputs.dbtype != 'sqlite' }}
256+
if: ${{ inputs.dbtype != 'sqlite' && steps.shard.outputs.skip != 'true' }}
219257
run: composer prepare-tests
220258

221259
- name: Check Behat environment
260+
if: ${{ steps.shard.outputs.skip != 'true' }}
222261
env:
223262
WP_VERSION: '${{ inputs.wp }}'
224263
WP_CLI_TEST_DBTYPE: ${{ inputs.dbtype || 'mysql' }}
@@ -227,6 +266,7 @@ jobs:
227266
run: composer behat
228267

229268
- name: Run Behat
269+
if: ${{ steps.shard.outputs.skip != 'true' }}
230270
env:
231271
WP_VERSION: '${{ inputs.wp }}'
232272
WP_CLI_TEST_DBTYPE: ${{ inputs.dbtype || 'mysql' }}
@@ -236,23 +276,33 @@ jobs:
236276
# Spread the scenarios over parallel workers, each in its own throwaway containers.
237277
# Where Docker cannot be used, such as on Windows and macOS or when collecting code
238278
# coverage, wp-cli/wp-cli-tests falls back on its own. Older versions ignore these.
239-
WP_CLI_TEST_PARALLEL: 4
279+
WP_CLI_TEST_PARALLEL: ${{ inputs.parallel }}
280+
# The test framework spreads the scenarios over the shards by their expected cost,
281+
# the same way on every runner, so that together they run every scenario once.
282+
WP_CLI_TEST_SHARD: ${{ steps.shard.outputs.shard }}
240283
WP_CLI_TEST_DOCKER: auto
241284
run: |
242285
composer behat -- $BEHAT_ARGS || composer behat-rerun -- $BEHAT_ARGS
243286
244-
- name: Retrieve list of coverage files
245-
id: coverage_files
246-
if: ${{ inputs.coverage }}
247-
run: |
248-
FILES=$(find "$GITHUB_WORKSPACE/build/logs" -path '*.*' | paste -s -d "," -)
249-
echo "files=$FILES" >> $GITHUB_OUTPUT
287+
# The calling workflow uploads the reports of all shards together, once all of them
288+
# finished, so that Codecov receives them as one upload and never reports on a part.
289+
- name: Name the code coverage report
290+
id: coverage_artifact
291+
if: ${{ inputs.coverage && steps.shard.outputs.skip != 'true' }}
292+
env:
293+
SHARD: ${{ inputs.shard || 'all' }}
294+
PHP: ${{ inputs.php }}
295+
DRIVER: ${{ inputs.coverage-driver }}
296+
PARALLEL: ${{ inputs.parallel }}
297+
run: echo "name=behat-coverage-${PHP}-${DRIVER}-p${PARALLEL}-${SHARD//\//-of-}" >> "$GITHUB_OUTPUT"
250298

251-
- name: Upload code coverage report
252-
if: ${{ inputs.coverage }}
253-
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
299+
- name: Keep the code coverage report for the upload
300+
if: ${{ inputs.coverage && steps.shard.outputs.skip != 'true' }}
301+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
254302
with:
255-
# Because somehow providing `directory: build/logs` doesn't work for these files
256-
files: ${{ steps.coverage_files.outputs.files }}
257-
flags: feature
258-
token: ${{ secrets.CODECOV_TOKEN }}
303+
name: ${{ steps.coverage_artifact.outputs.name }}
304+
path: build/logs/*.xml
305+
if-no-files-found: error
306+
retention-days: 1
307+
# A re-run of a failed shard replaces the report of its previous attempt.
308+
overwrite: true

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

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ on:
1818
type: boolean
1919
required: false
2020
default: true
21+
coverage-shards:
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.'
23+
type: number
24+
required: false
25+
default: 4
2126
matrix:
2227
description: 'Additional matrix entries to include or exclude.'
2328
type: string
@@ -44,6 +49,7 @@ jobs:
4449
outputs:
4550
unit: ${{ steps.unit.outputs.matrix }}
4651
functional: ${{ steps.functional.outputs.matrix }}
52+
coverage: ${{ steps.functional.outputs.coverage }}
4753
steps:
4854
- name: Check out source code
4955
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -475,9 +481,11 @@ jobs:
475481
INPUTS_MINIMUM_PHP: ${{ inputs.minimum-php }}
476482
INPUTS_MINIMUM_WP: ${{ inputs.minimum-wp }}
477483
WITH_COVERAGE: ${{ inputs.with-coverage }}
484+
COVERAGE_SHARDS: ${{ inputs.coverage-shards }}
485+
SCENARIOS_PER_SHARD: 50
478486
run: |
479487
if [[ $FILE_EXISTS == 'true' && $DOCS_ONLY != 'true' ]]; then
480-
echo "matrix=$(jq -c \
488+
MATRIX=$(jq -c \
481489
--argjson with_coverage_flag "${WITH_COVERAGE}" \
482490
--arg minimum_php "${INPUTS_MINIMUM_PHP}" \
483491
--arg minimum_wp "${INPUTS_MINIMUM_WP}" \
@@ -505,9 +513,33 @@ jobs:
505513
).wp |= $minimum_wp
506514
)
507515
)
508-
' <<< "$BASE_MATRIX" )" >> "$GITHUB_OUTPUT"
516+
' <<< "$BASE_MATRIX" )
517+
518+
# Collecting code coverage makes the Behat tests several times slower, so these
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}" '
527+
[
528+
.include[] | select(.coverage == true) | . as $entry |
529+
($entry.coverage_shards // $shards) as $total |
530+
if $total > 1 then
531+
range(1; $total + 1) | $entry + { shard: "\(.)/\($total)" }
532+
else
533+
$entry + { shard: "" }
534+
end
535+
]
536+
' <<< "$MATRIX")
537+
538+
echo "matrix=$(jq -c '.include |= map(select(.coverage != true)) | if (.include | length) > 0 then . else empty end' <<< "$MATRIX")" >> "$GITHUB_OUTPUT"
539+
echo "coverage=$(jq -c 'if length > 0 then { include: . } else empty end' <<< "$COVERAGE")" >> "$GITHUB_OUTPUT"
509540
else
510541
echo "matrix=" >> "$GITHUB_OUTPUT"
542+
echo "coverage=" >> "$GITHUB_OUTPUT"
511543
fi
512544
513545
unit:
@@ -547,3 +579,61 @@ jobs:
547579
object_cache: ${{ matrix.object_cache }}
548580
coverage: ${{ matrix.coverage == true }}
549581
os: ${{ matrix.os || '' }}
582+
583+
functional-coverage:
584+
needs: prepare
585+
if: ${{ needs.prepare.outputs.coverage != '' }}
586+
# Static on purpose, see above.
587+
name: Behat coverage
588+
strategy:
589+
fail-fast: false
590+
matrix: ${{ fromJson(needs.prepare.outputs.coverage) }}
591+
uses: ./.github/workflows/reusable-functional.yml
592+
secrets: inherit
593+
with:
594+
php: ${{ matrix.php }}
595+
wp: ${{ matrix.wp }}
596+
dbtype: ${{ matrix.dbtype || 'mysql' }}
597+
mysql: ${{ matrix.mysql || '' }}
598+
object_cache: ${{ matrix.object_cache }}
599+
coverage: true
600+
coverage-driver: ${{ matrix.coverage_driver || 'pcov' }}
601+
shard: ${{ matrix.shard }}
602+
parallel: ${{ matrix.parallel || 4 }}
603+
os: ${{ matrix.os || '' }}
604+
605+
# The reports of all shards are uploaded together, so that Codecov receives the coverage
606+
# of the Behat tests as one upload however many runners collected it, and never reports
607+
# on a part of it. Only when every shard passed, as one that failed has no report.
608+
coverage-upload:
609+
needs: functional-coverage
610+
if: ${{ needs.functional-coverage.result == 'success' }}
611+
name: Upload Behat code coverage
612+
runs-on: ubuntu-22.04
613+
timeout-minutes: 10
614+
steps:
615+
- name: Check out source code
616+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
617+
with:
618+
persist-credentials: false
619+
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
620+
621+
- name: Download the code coverage reports of all shards
622+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
623+
with:
624+
pattern: behat-coverage-*
625+
path: build/coverage
626+
627+
- name: Retrieve list of coverage files
628+
id: coverage_files
629+
run: |
630+
FILES=$(find "$GITHUB_WORKSPACE/build/coverage" -name '*.xml' | sort | paste -s -d "," -)
631+
echo "Found $(tr ',' '\n' <<< "$FILES" | grep -c .) coverage reports."
632+
echo "files=$FILES" >> "$GITHUB_OUTPUT"
633+
634+
- name: Upload code coverage report
635+
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
636+
with:
637+
files: ${{ steps.coverage_files.outputs.files }}
638+
flags: feature
639+
token: ${{ secrets.CODECOV_TOKEN }}

0 commit comments

Comments
 (0)