Skip to content

Commit cdeeb95

Browse files
committed
Build/Test Tools: Flag slow PHPUnit tests in GitHub Actions.
The canonical PHP 8.5 reporting job now emits warning annotations and a run-summary table for tests that exceed one second. The advisory step runs after successful or failed test runs and skips cancelled jobs. The annotation pass reuses the streaming JUnit parser introduced in [63410] and passes its six aggregate metrics to CodeVitals through a temporary file. This keeps the workflow to one traversal of the report. Developed in: WordPress#13212 Props adrianmoldovanwp. See #65887. git-svn-id: https://develop.svn.wordpress.org/trunk@63412 602fd350-edb4-49c9-b593-d223f7449a82
1 parent bbd9fc3 commit cdeeb95

5 files changed

Lines changed: 452 additions & 39 deletions

File tree

‎.github/workflows/reusable-phpunit-tests-v3.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ jobs:
126126
# - Logs debug information about what's installed within the WordPress Docker containers.
127127
# - Install WordPress within the Docker container.
128128
# - Run the PHPUnit tests.
129+
# - Flags slow PHPUnit tests with GitHub Actions annotations and a run summary.
129130
# - Publish PHPUnit timing metrics to CodeVitals.
130131
# - Upload the code coverage report to Codecov.io.
131132
# - Ensures version-controlled files are not modified or deleted.
@@ -273,6 +274,28 @@ jobs:
273274
TEST_GROUPS: ${{ inputs.phpunit-test-groups }}
274275
MULTISITE_FLAG: ${{ inputs.multisite && 'multisite' || 'single' }}
275276

277+
- name: Flag slow PHPUnit tests
278+
# Runs when the test step succeeds or fails, but skips cancelled jobs.
279+
# This step never fails the job itself (continue-on-error), because the
280+
# slow-test signal is advisory only.
281+
if: >-
282+
${{
283+
! cancelled() && inputs.report && inputs.php == '8.5' &&
284+
( github.event_name == 'pull_request' ||
285+
( github.event_name == 'push' && github.ref == 'refs/heads/trunk' ) )
286+
}}
287+
continue-on-error: true
288+
run: |
289+
if [ -f tests/phpunit/build/logs/junit.xml ]; then
290+
php tests/phpunit/prepare-slow-test-annotations.php \
291+
tests/phpunit/build/logs/junit.xml \
292+
1.0 \
293+
20 \
294+
"$RUNNER_TEMP/phpunit-timing-metrics.json"
295+
else
296+
echo 'PHPUnit JUnit report not found; skipping slow-test annotations.'
297+
fi
298+
276299
- name: Publish PHPUnit timing metrics
277300
continue-on-error: true
278301
if: |
@@ -295,6 +318,7 @@ jobs:
295318
trunk \
296319
"$GITHUB_SHA" \
297320
"$COMMITTED_AT" \
321+
"$RUNNER_TEMP/phpunit-timing-metrics.json" \
298322
| curl --fail-with-body --silent --show-error \
299323
--request POST \
300324
--header 'Content-Type: application/json' \

‎tests/phpunit/includes/class-wp-phpunit-timing-metrics.php‎

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,93 @@ final class WP_PHPUnit_Timing_Metrics {
88
/**
99
* Extracts timing metrics from a JUnit XML file.
1010
*
11-
* @param string $file Path to the JUnit XML file.
11+
* @param string $file Path to the JUnit XML file.
12+
* @param callable|null $testcase_callback Optional callback invoked for each timed testcase.
1213
* @return array<string, float|int> Timing metrics keyed for CodeVitals.
13-
* @throws RuntimeException If the file cannot be read or contains invalid timing data.
14+
* @throws RuntimeException If the file cannot be read or contains invalid XML.
1415
*/
15-
public static function from_file( $file ) {
16+
public static function from_file( $file, $testcase_callback = null ) {
1617
if ( ! is_readable( $file ) ) {
1718
throw new RuntimeException( 'The JUnit timing report could not be read.' );
1819
}
1920

20-
$reader = new XMLReader();
21-
if ( ! $reader->open( $file, null, LIBXML_NONET | LIBXML_COMPACT ) ) {
22-
throw new RuntimeException( 'The JUnit timing report could not be opened.' );
21+
if ( null !== $testcase_callback && ! is_callable( $testcase_callback ) ) {
22+
throw new InvalidArgumentException( 'The testcase callback must be callable.' );
2323
}
2424

25-
$suite_time = null;
26-
$test_times = array();
25+
$reader = new XMLReader();
26+
$previous_libxml_state = libxml_use_internal_errors( true );
27+
$reader_is_open = false;
28+
$suite_time = null;
29+
$test_times = array();
30+
$xml_errors = array();
2731

28-
while ( $reader->read() ) {
29-
if ( XMLReader::ELEMENT !== $reader->nodeType ) {
30-
continue;
32+
libxml_clear_errors();
33+
34+
try {
35+
if ( ! $reader->open( $file, null, LIBXML_NONET | LIBXML_COMPACT ) ) {
36+
throw new RuntimeException( 'The JUnit timing report could not be opened.' );
3137
}
3238

33-
if ( null === $suite_time && 'testsuite' === $reader->name ) {
39+
$reader_is_open = true;
40+
41+
while ( $reader->read() ) {
42+
if ( XMLReader::ELEMENT !== $reader->nodeType ) {
43+
continue;
44+
}
45+
46+
if ( null === $suite_time && 'testsuite' === $reader->name ) {
47+
$time = $reader->getAttribute( 'time' );
48+
if ( is_numeric( $time ) ) {
49+
$suite_time = (float) $time;
50+
}
51+
continue;
52+
}
53+
54+
if ( 'testcase' !== $reader->name ) {
55+
continue;
56+
}
57+
3458
$time = $reader->getAttribute( 'time' );
35-
if ( is_numeric( $time ) ) {
36-
$suite_time = (float) $time;
59+
60+
// A testcase without numeric timing, such as a skipped test, carries
61+
// no timing signal and is excluded from the aggregate metrics.
62+
if ( ! is_numeric( $time ) ) {
63+
continue;
3764
}
38-
continue;
39-
}
4065

41-
if ( 'testcase' !== $reader->name ) {
42-
continue;
66+
$test_time = (float) $time;
67+
$test_times[] = $test_time;
68+
69+
if ( null !== $testcase_callback ) {
70+
$testcase_callback(
71+
array(
72+
'name' => (string) $reader->getAttribute( 'name' ),
73+
'class' => (string) $reader->getAttribute( 'class' ),
74+
'file' => (string) $reader->getAttribute( 'file' ),
75+
'line' => (string) $reader->getAttribute( 'line' ),
76+
'time' => $test_time,
77+
'time_display' => (string) $time,
78+
)
79+
);
80+
}
4381
}
4482

45-
$time = $reader->getAttribute( 'time' );
46-
if ( ! is_numeric( $time ) ) {
83+
$xml_errors = libxml_get_errors();
84+
} finally {
85+
if ( $reader_is_open ) {
4786
$reader->close();
48-
throw new RuntimeException( 'A JUnit testcase is missing numeric timing data.' );
4987
}
5088

51-
$test_times[] = (float) $time;
89+
libxml_clear_errors();
90+
libxml_use_internal_errors( $previous_libxml_state );
5291
}
5392

54-
$reader->close();
93+
foreach ( $xml_errors as $xml_error ) {
94+
if ( LIBXML_ERR_WARNING < $xml_error->level ) {
95+
throw new RuntimeException( 'The JUnit timing report contains invalid XML.' );
96+
}
97+
}
5598

5699
if ( ! $test_times ) {
57100
throw new RuntimeException( 'The JUnit timing report contains no testcases.' );

0 commit comments

Comments
 (0)