Skip to content

Commit 23dd5d0

Browse files
authored
Merge pull request #242 from wp-cli/fix/coverage
Fix code coverage for main framework repo
2 parents 16ee1c9 + 5944bbd commit 23dd5d0

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

src/Context/FeatureContext.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class FeatureContext implements SnippetAcceptingContext {
3838
*/
3939
private static $run_dir;
4040

41+
/**
42+
* The Directory that 'composer behat' is run from, assumed to always be the top level project folder
43+
*/
44+
private static $behat_run_dir;
45+
4146
/**
4247
* Where WordPress core is downloaded to for caching, and which is copied to RUN_DIR during a "Given a WP installation" step. Lives until manually deleted.
4348
*/
@@ -286,9 +291,10 @@ private static function get_process_env_variables() {
286291

287292
$path_separator = Utils\is_windows() ? ';' : ':';
288293
$env = [
289-
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
290-
'BEHAT_RUN' => 1,
291-
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
294+
'PATH' => $bin_path . $path_separator . getenv( 'PATH' ),
295+
'BEHAT_RUN' => 1,
296+
'HOME' => sys_get_temp_dir() . '/wp-cli-home',
297+
'TEST_RUN_DIR' => self::$behat_run_dir,
292298
];
293299

294300
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
@@ -371,6 +377,7 @@ private static function get_behat_internal_variables() {
371377
'FRAMEWORK_ROOT' => realpath( $framework_root ),
372378
'SRC_DIR' => realpath( dirname( dirname( __DIR__ ) ) ),
373379
'PROJECT_DIR' => realpath( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ),
380+
'TEST_RUN_DIR' => self::$behat_run_dir,
374381
];
375382

376383
return $variables;
@@ -475,6 +482,7 @@ public static function prepare( BeforeSuiteScope $scope ) {
475482
if ( false !== self::$log_run_times ) {
476483
self::log_run_times_before_suite( $scope );
477484
}
485+
self::$behat_run_dir = getcwd();
478486

479487
$result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check();
480488
echo "{$result->stdout}\n";
@@ -521,7 +529,6 @@ public function beforeScenario( BeforeScenarioScope $scope ) {
521529
if ( self::$log_run_times ) {
522530
self::log_run_times_before_scenario( $scope );
523531
}
524-
525532
$this->variables = array_merge(
526533
$this->variables,
527534
self::get_behat_internal_variables()
@@ -693,8 +700,31 @@ public function __construct() {
693700
*/
694701
public function get_command_with_coverage( $cmd ) {
695702
$with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' );
703+
696704
if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) {
697-
return preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3--require={SRC_DIR}/utils/generate-coverage.php ', $cmd );
705+
706+
$modify_command = function ( $part ) {
707+
if ( preg_match( '/(^wp )|( wp )|(\/wp )/', $part ) ) {
708+
$part = preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3', $part );
709+
710+
$require_path = '{TEST_RUN_DIR}/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php';
711+
if ( ! file_exists( $this->variables['TEST_RUN_DIR'] . '/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php' ) ) {
712+
// This file is not vendored inside the wp-cli-tests project
713+
$require_path = '{TEST_RUN_DIR}/utils/generate-coverage.php';
714+
}
715+
$part .= " --require={$require_path}";
716+
717+
}
718+
return $part;
719+
};
720+
721+
if ( strpos( $cmd, '|' ) !== false ) {
722+
$parts = explode( '|', $cmd );
723+
$parts = array_map( $modify_command, $parts );
724+
$cmd = implode( '|', $parts );
725+
} else {
726+
$cmd = $modify_command( $cmd );
727+
}
698728
}
699729

700730
return $cmd;

utils/generate-coverage.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,54 @@
1111
use SebastianBergmann\CodeCoverage\Filter;
1212
use SebastianBergmann\CodeCoverage\Report\Clover;
1313

14-
$root_folder = realpath( dirname( __DIR__ ) );
14+
15+
$project_dir = (string) getenv( 'TEST_RUN_DIR' );
1516

1617
if ( ! class_exists( 'SebastianBergmann\CodeCoverage\Filter' ) ) {
17-
require "{$root_folder}/vendor/autoload.php";
18+
if ( ! file_exists( $project_dir . '/vendor/autoload.php' ) ) {
19+
die( 'Could not load dependencies for generating code coverage' );
20+
}
21+
require "{$project_dir}/vendor/autoload.php";
22+
}
23+
24+
$filtered_items = new CallbackFilterIterator(
25+
new DirectoryIterator( $project_dir ),
26+
function ( $file ) {
27+
// Allow directories named "php" or "src"
28+
if ( $file->isDir() && in_array( $file->getFilename(), [ 'php', 'src' ], true ) ) {
29+
return true;
30+
}
31+
32+
// Allow top-level files ending in "-command.php"
33+
if ( $file->isFile() && false !== strpos( $file->getFilename(), '-command.php' ) ) {
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
);
40+
41+
$files = [];
42+
43+
foreach ( $filtered_items as $item ) {
44+
if ( $item->isDir() ) {
45+
foreach (
46+
new RecursiveIteratorIterator(
47+
new RecursiveDirectoryIterator( $item->getPathname(), RecursiveDirectoryIterator::SKIP_DOTS )
48+
) as $file
49+
) {
50+
if ( $file->isFile() && $file->getExtension() === 'php' ) {
51+
$files[] = $file->getPathname();
52+
}
53+
}
54+
} else {
55+
$files[] = $item->getPathname();
56+
}
1857
}
1958

2059
$filter = new Filter();
21-
$filter->includeDirectory( "{$root_folder}/includes" );
22-
$filter->includeFiles( array( "{$root_folder}/plugin.php" ) );
60+
61+
$filter->includeFiles( $files );
2362

2463
$coverage = new CodeCoverage(
2564
( new Selector() )->forLineCoverage( $filter ),
@@ -37,11 +76,9 @@
3776
$coverage->start( $name );
3877

3978
register_shutdown_function(
40-
static function () use ( $coverage, $feature, $scenario, $name ) {
79+
static function () use ( $coverage, $feature, $scenario, $name, $project_dir ) {
4180
$coverage->stop();
4281

43-
$project_dir = (string) getenv( 'BEHAT_PROJECT_DIR' );
44-
4582
$feature_suffix = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $feature ) );
4683
$scenario_suffix = preg_replace( '/[^a-z0-9]+/', '-', strtolower( $scenario ) );
4784
$db_type = strtolower( getenv( 'WP_CLI_TEST_DBTYPE' ) );

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy