Skip to content

Commit

Permalink
ci: Initial code to process coverage data (#40016)
Browse files Browse the repository at this point in the history
To be able to inform the author about the coverage on their PR, we'll
need to extract a summary of coverage changes from the raw data we're
collecting. This is a first step towards that, combining the raw
coverage data into combined files and producing a summary of covered
lines in each file.

Also, since it annoyed me, I added `WordPress.WP.GlobalVariablesOverride`
to our `Jetpack-NoWP` ruleset so non-WordPress code doesn't get
useless complaints about common variable names like `$path`.

Also, fix Boost coverage that was missing some config settings.
  • Loading branch information
anomiex authored Nov 5, 2024
1 parent 4b83bb9 commit ce35f15
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/files/coverage-munger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/node_modules
11 changes: 11 additions & 0 deletions .github/files/coverage-munger/.phan/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Phan config.
*
* @package automattic/jetpack-monorepo
*/

// Require base config.
require dirname( __DIR__, 4 ) . '/.phan/config.base.php';

return make_phan_config( dirname( __DIR__ ), array( 'stubs' => array() ) );
6 changes: 6 additions & 0 deletions .github/files/coverage-munger/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require-dev": {
"yoast/phpunit-polyfills": "^1.1.1",
"phpunit/phpcov": "^8.2"
}
}
36 changes: 36 additions & 0 deletions .github/files/coverage-munger/extract-php-summary-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
/**
* Script to output summary data from a PHPUnit raw config.
*
* @package automattic/jetpack
*/

// @phan-file-suppress PhanAccessMethodInternal -- Nothing much we can do to avoid it.

use SebastianBergmann\CodeCoverage\Node\File;

if ( $argc < 2 ) {
fprintf( STDERR, "USAGE: $0 <php.cov> <monorepo-root-path>\n" );
exit( 1 );
}

require __DIR__ . '/vendor/autoload.php';
$cov = require $argv[1];
$report = $cov->getReport();

foreach ( $report as $item ) {
if ( ! $item instanceof File ) {
continue;
}

$path = $item->pathAsString();

fputcsv(
STDOUT,
array( $path, $item->numberOfExecutableLines() + $item->numberOfExecutableBranches(), $item->numberOfExecutedLines() + $item->numberOfExecutedBranches() ),
"\t",
'"',
''
);
}
8 changes: 8 additions & 0 deletions .github/files/coverage-munger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"private": true,
"name": "jetpack-gh-config-munger",
"devDependencies": {
"istanbul-merge": "^2.0.0",
"nyc": "^17.1.0"
}
}
44 changes: 44 additions & 0 deletions .github/files/coverage-munger/process-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

set -eo pipefail

BASE=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)

[[ -d coverage ]] && find coverage -type d -empty -delete
if [[ ! -d coverage ]]; then
echo 'No coverage was generated.'
exit 0
fi

echo '::group::Copy coverage into artifacts'
tar --owner=0 --group=0 --xz -cvvf artifacts/coverage.tar.xz coverage
echo '::endgroup::'

TMP_DIR=$( mktemp -d )
trap 'rm -rf "$TMP_DIR"' exit

echo "::group::Combining PHP coverage"
composer --working-dir="$BASE" update
"$BASE"/vendor/bin/phpcov merge --php artifacts/php-combined.cov coverage
perl -i -pwe 'BEGIN { $prefix = shift; $prefix=~s!/*$!/!; $re = qr/\Q$prefix\E/; $l = length( $prefix ); } s!s:(\d+):"$re! sprintf( qq(s:%d:"), $1 - $l ) !ge' "$GITHUB_WORKSPACE" artifacts/php-combined.cov
echo '::endgroup::'

echo "::group::Combining JS coverage"
pnpm --filter=jetpack-gh-config-munger exec istanbul-merge --out "$PWD"/artifacts/js-combined.json $( find "$PWD/coverage" -name '*.json' )
perl -i -pwe 'BEGIN { $prefix = shift; $prefix=~s!/*$!/!; $re = qr/\Q$prefix\E/; } s!"$re!"!g' "$GITHUB_WORKSPACE" artifacts/js-combined.json
echo '::endgroup::'

echo "::group::Creating PHP coverage summary"
"$BASE"/extract-php-summary-data.php artifacts/php-combined.cov > "$TMP_DIR/php-summary.tsv"
echo '::endgroup::'

echo "::group::Creating JS coverage summary"
mkdir "$TMP_DIR/js"
cp artifacts/js-combined.json "$TMP_DIR/js"
pnpm --filter=jetpack-gh-config-munger exec nyc report --no-exclude-after-remap --report-dir="$TMP_DIR" --temp-dir="$TMP_DIR/js" --reporter=json-summary
jq -r 'to_entries[] | select( .key != "total" ) | [ .key, .value.lines.total, .value.lines.covered ] | @tsv' "$TMP_DIR/coverage-summary.json" > "$TMP_DIR/js-summary.tsv"
echo '::endgroup::'

echo "::group::Combining coverage summaries"
sort "$TMP_DIR/php-summary.tsv" "$TMP_DIR/js-summary.tsv" > artifacts/summary.tsv
echo '::endgroup::'
2 changes: 0 additions & 2 deletions .github/files/generate-ci-matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @package automattic/jetpack
*/

// phpcs:disable WordPress.WP.GlobalVariablesOverride

chdir( __DIR__ . '/../../' );

// Default versions for PHP and Node.
Expand Down
15 changes: 0 additions & 15 deletions .github/files/process-coverage.sh

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ jobs:
env:
CHANGED: ${{ steps.changed.outputs.projects }}
if: matrix.script == 'test-coverage'
run: .github/files/process-coverage.sh
run: .github/files/coverage-munger/process-coverage.sh

- name: Check for artifacts
id: check-artifacts
Expand Down
1 change: 1 addition & 0 deletions .phan/monorepo-pseudo-projects.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Monorepo pseudo-projects. These are things that have their own composer.json but aren't actual projects.
{
"monorepo/actions-tool-setup-composer-plugin": ".github/actions/tool-setup/composer-plugin/",
"monorepo/coverage-munger": ".github/files/coverage-munger/",
"monorepo/cli-doc-parser": "tools/cli/helpers/doc-parser/",
"monorepo/e2e-commons": "tools/e2e-commons/"
}
68 changes: 68 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ packages:
- 'tools/cli'
- 'tools/e2e-commons'
- 'tools/js-tools'
- '.github/files/coverage-munger'
3 changes: 3 additions & 0 deletions projects/packages/codesniffer/Jetpack-NoWP/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
<rule ref="WordPress.WP.AlternativeFunctions">
<severity>0</severity>
</rule>
<rule ref="WordPress.WP.GlobalVariablesOverride">
<severity>0</severity>
</rule>

</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Add `WordPress.WP.GlobalVariablesOverride` to `Jetpack-NoWP` ruleset.
5 changes: 5 additions & 0 deletions projects/plugins/boost/changelog/add-coverage-processing
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix Boost coverage


1 change: 1 addition & 0 deletions projects/plugins/boost/tests/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.join( __dirname, '..' ),
testEnvironment: 'jsdom',
collectCoverageFrom: [
Expand Down
2 changes: 0 additions & 2 deletions tools/check-changelogger-use.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @package automattic/jetpack
*/

// phpcs:disable WordPress.WP.GlobalVariablesOverride

chdir( __DIR__ . '/../' );

/**
Expand Down
5 changes: 4 additions & 1 deletion tools/cli/commands/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ infrastructureFileSets.base = new Set( [
infrastructureFileSets.test = new Set( [
...infrastructureFileSets.base,
'.github/files/generate-ci-matrix.php',
'.github/files/process-coverage.sh',
'.github/files/coverage-munger/composer.json',
'.github/files/coverage-munger/package.json',
'.github/files/coverage-munger/extract-php-summary-data.php',
'.github/files/coverage-munger/process-coverage.sh',
'.github/files/setup-wordpress-env.sh',
'.github/workflows/tests.yml',
] );
Expand Down
2 changes: 1 addition & 1 deletion tools/docker/config/wp-tests-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );

// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$table_prefix = 'wptests_'; // Only numbers, letters, and underscores please!

define( 'WP_TESTS_DOMAIN', 'example.org' );
Expand Down

0 comments on commit ce35f15

Please sign in to comment.