@@ -269,12 +269,21 @@ public static function forget_feature( AfterFeatureScope $scope ): void {
269
269
}
270
270
271
271
/**
272
- * @AfterSuite
272
+ * Whether tests are currently running with code coverage collection.
273
+ *
274
+ * @return bool
273
275
*/
274
- public static function merge_coverage_reports (): void {
276
+ private static function running_with_code_coverage () {
275
277
$ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
276
278
277
- if ( ! \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
279
+ return \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true );
280
+ }
281
+
282
+ /**
283
+ * @AfterSuite
284
+ */
285
+ public static function merge_coverage_reports (): void {
286
+ if ( ! self ::running_with_code_coverage () ) {
278
287
return ;
279
288
}
280
289
@@ -436,9 +445,7 @@ private static function get_process_env_variables(): array {
436
445
'TEST_RUN_DIR ' => self ::$ behat_run_dir ,
437
446
];
438
447
439
- $ with_code_coverage = (string ) getenv ( 'WP_CLI_TEST_COVERAGE ' );
440
-
441
- if ( \in_array ( $ with_code_coverage , [ 'true ' , '1 ' ], true ) ) {
448
+ if ( self ::running_with_code_coverage () ) {
442
449
$ has_coverage_driver = ( new Runtime () )->hasXdebug () || ( new Runtime () )->hasPCOV ();
443
450
444
451
if ( ! $ has_coverage_driver ) {
@@ -1021,14 +1028,27 @@ public function create_run_dir(): void {
1021
1028
public function build_phar ( $ version = 'same ' ): void {
1022
1029
$ this ->variables ['PHAR_PATH ' ] = $ this ->variables ['RUN_DIR ' ] . '/ ' . uniqid ( 'wp-cli-build- ' , true ) . '.phar ' ;
1023
1030
1031
+ $ is_bundle = false ;
1032
+
1024
1033
// Test running against a package installed as a WP-CLI dependency
1025
1034
// WP-CLI bundle installed as a project dependency
1026
1035
$ make_phar_path = self ::get_vendor_dir () . '/wp-cli/wp-cli-bundle/utils/make-phar.php ' ;
1027
1036
if ( ! file_exists ( $ make_phar_path ) ) {
1028
1037
// Running against WP-CLI bundle proper
1038
+ $ is_bundle = true ;
1039
+
1029
1040
$ make_phar_path = self ::get_vendor_dir () . '/../utils/make-phar.php ' ;
1030
1041
}
1031
1042
1043
+ // Temporarily modify the Composer autoloader used within the Phar
1044
+ // so that it doesn't clash if autoloading is already happening outside of it,
1045
+ // for example when generating code coverage.
1046
+ // This modifies composer.json.
1047
+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1048
+ $ this ->composer_command ( 'config autoloader-suffix "WpCliTestsPhar" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1049
+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1050
+ }
1051
+
1032
1052
$ this ->proc (
1033
1053
Utils \esc_cmd (
1034
1054
'php -dphar.readonly=0 %1$s %2$s --version=%3$s && chmod +x %2$s ' ,
@@ -1037,6 +1057,12 @@ public function build_phar( $version = 'same' ): void {
1037
1057
$ version
1038
1058
)
1039
1059
)->run_check ();
1060
+
1061
+ // Revert the suffix change again
1062
+ if ( $ is_bundle && self ::running_with_code_coverage () ) {
1063
+ $ this ->composer_command ( 'config autoloader-suffix "WpCliBundle" --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1064
+ $ this ->composer_command ( 'dump-autoload --working-dir= ' . dirname ( self ::get_vendor_dir () ) );
1065
+ }
1040
1066
}
1041
1067
1042
1068
/**
0 commit comments