Skip to content

Commit 35e86e4

Browse files
committed
Another attempt at properly setting the directory for code coverage
Assume that the folder that `composer behat` is run in will be the project level folder in all situations, which avoids long chained dirname calls that don't work with the wp-cli-dev environment. Update itterator to use a call back filter as well, just to keep the logic of what files we want all in a single place.
1 parent 9fabc0a commit 35e86e4

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

src/Context/FeatureContext.php

Lines changed: 28 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,24 @@ 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+
$part .= ' --require={TEST_RUN_DIR}/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php';
710+
}
711+
return $part;
712+
};
713+
714+
if ( strpos( $cmd, '|' ) !== false ) {
715+
$parts = explode( '|', $cmd );
716+
$parts = array_map( $modify_command, $parts );
717+
$cmd = implode( '|', $parts );
718+
} else {
719+
$cmd = $modify_command( $cmd );
720+
}
698721
}
699722

700723
return $cmd;

utils/generate-coverage.php

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,47 @@
1212
use SebastianBergmann\CodeCoverage\Report\Clover;
1313

1414

15-
// The wp-cli-tests directory.
16-
$package_folder = realpath( dirname( __DIR__ ) );
17-
18-
// If installed as a dependency in `<somedir>/vendor/wp-cli/wp-cli-tests, this is <somedir>.
19-
$project_dir = (string) getenv( 'BEHAT_PROJECT_DIR' );
20-
21-
// If we're not in a Behat environment.
22-
if ( ! $project_dir ) {
23-
$project_dir = realpath( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) );
24-
}
25-
26-
if ( ! file_exists( $project_dir . '/vendor/autoload.php' ) ) {
27-
$project_dir = $package_folder;
28-
}
15+
$project_dir = (string) getenv( 'TEST_RUN_DIR' );
2916

3017
if ( ! class_exists( 'SebastianBergmann\CodeCoverage\Filter' ) ) {
3118
if ( ! file_exists( $project_dir . '/vendor/autoload.php' ) ) {
3219
die( 'Could not load dependencies for generating code coverage' );
3320
}
34-
3521
require "{$project_dir}/vendor/autoload.php";
3622
}
3723

38-
$files = [];
39-
40-
$dir_to_search = null;
41-
42-
// In wp-cli/wp-cli, all source code is in the "php" folder.
43-
// In commands, all source code is in the "src" folder.
44-
if ( is_dir( "{$project_dir}/php" ) ) {
45-
$dir_to_search = "{$project_dir}/php";
46-
} elseif ( is_dir( "{$project_dir}/src" ) ) {
47-
$dir_to_search = "{$project_dir}/src";
48-
}
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+
}
4931

50-
if ( $dir_to_search ) {
51-
foreach (
52-
new RecursiveIteratorIterator(
53-
new RecursiveDirectoryIterator( $dir_to_search, RecursiveDirectoryIterator::SKIP_DOTS | RecursiveDirectoryIterator::FOLLOW_SYMLINKS )
54-
)
55-
as $file
56-
) {
57-
if ( $file->isFile() && 'php' === $file->getExtension() ) {
58-
$files[] = $file->getPathname();
32+
// Allow top-level files ending in "-command.php"
33+
if ( $file->isFile() && false !== strpos( $file->getFilename(), '-command.php' ) ) {
34+
return true;
5935
}
36+
37+
return false;
6038
}
61-
}
39+
);
40+
41+
$files = [];
6242

63-
// There is also a "*-command.php" file.
64-
foreach (
65-
new IteratorIterator(
66-
new DirectoryIterator( $project_dir )
67-
) as $file ) {
68-
if ( $file->isFile() && false !== strpos( $file->getFilename(), '-command.php' ) ) {
69-
$files[] = $file->getPathname();
70-
break;
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();
7156
}
7257
}
7358

0 commit comments

Comments
 (0)