From ae5ba6d593acc7d90353a27b3f5eefbcb337cb45 Mon Sep 17 00:00:00 2001 From: Isla Waters Date: Tue, 18 Mar 2025 08:12:00 -0400 Subject: [PATCH 1/2] Use WP_CLI_REQUIRE to require codecov file for testing Instead of trying to place --require in the cmd string at the right spot, which is more error prone. --- src/Context/FeatureContext.php | 54 +++++++---------------------- src/Context/WhenStepDefinitions.php | 2 -- 2 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 2e3fe67b1..261f8d251 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -297,6 +297,19 @@ private static function get_process_env_variables() { 'TEST_RUN_DIR' => self::$behat_run_dir, ]; + $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); + if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { + $coverage_require_file = self::$behat_run_dir . '/vendor/wp-cli/wp-cli/tests/utils/generate-coverage.php'; + if ( ! file_exists( $coverage_require_file ) ) { + // This file is not vendored inside the wp-cli-tests project + $coverage_require_file = self::$behat_run_dir . '/utils/generate-coverage.php'; + } + + $current = getenv( 'WP_CLI_REQUIRE' ); + $updated = $current ? "{$current},{$coverage_require_file}" : $coverage_require_file; + $env['WP_CLI_REQUIRE'] = $updated; + } + $config_path = getenv( 'WP_CLI_CONFIG_PATH' ); if ( false !== $config_path ) { $env['WP_CLI_CONFIG_PATH'] = $config_path; @@ -377,7 +390,6 @@ private static function get_behat_internal_variables() { 'FRAMEWORK_ROOT' => realpath( $framework_root ), 'SRC_DIR' => realpath( dirname( dirname( __DIR__ ) ) ), 'PROJECT_DIR' => realpath( dirname( dirname( dirname( dirname( dirname( __DIR__ ) ) ) ) ) ), - 'TEST_RUN_DIR' => self::$behat_run_dir, ]; return $variables; @@ -690,46 +702,6 @@ public function __construct() { $this->set_cache_dir(); } - /** - * Enhances a `wp ` string with an additional `--require` for code coverage collection. - * - * Only applies if `WP_CLI_TEST_COVERAGE` is set. - * - * @param string $cmd Command string. - * @return string Possibly enhanced command string. - */ - public function get_command_with_coverage( $cmd ) { - $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); - - if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { - - $modify_command = function ( $part ) { - if ( preg_match( '/(^wp )|( wp )|(\/wp )/', $part ) ) { - $part = preg_replace( '/(^wp )|( wp )|(\/wp )/', '$1$2$3', $part ); - - $require_path = '{TEST_RUN_DIR}/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php'; - if ( ! file_exists( $this->variables['TEST_RUN_DIR'] . '/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php' ) ) { - // This file is not vendored inside the wp-cli-tests project - $require_path = '{TEST_RUN_DIR}/utils/generate-coverage.php'; - } - $part .= " --require={$require_path}"; - - } - return $part; - }; - - if ( strpos( $cmd, '|' ) !== false ) { - $parts = explode( '|', $cmd ); - $parts = array_map( $modify_command, $parts ); - $cmd = implode( '|', $parts ); - } else { - $cmd = $modify_command( $cmd ); - } - } - - return $cmd; - } - /** * Replace standard {VARIABLE_NAME} variables and the special {INVOKE_WP_CLI_WITH_PHP_ARGS-args} and {WP_VERSION-version-latest} variables. * Note that standard variable names can only contain uppercase letters, digits and underscores and cannot begin with a digit. diff --git a/src/Context/WhenStepDefinitions.php b/src/Context/WhenStepDefinitions.php index 3fac6b75e..a727e8c7f 100644 --- a/src/Context/WhenStepDefinitions.php +++ b/src/Context/WhenStepDefinitions.php @@ -35,7 +35,6 @@ public function when_i_launch_in_the_background( $cmd ) { * @When /^I (run|try) `([^`]+)`$/ */ public function when_i_run( $mode, $cmd ) { - $cmd = $this->get_command_with_coverage( $cmd ); $cmd = $this->replace_variables( $cmd ); $this->result = $this->wpcli_tests_invoke_proc( $this->proc( $cmd ), $mode ); list( $this->result->stdout, $this->email_sends ) = $this->wpcli_tests_capture_email_sends( $this->result->stdout ); @@ -45,7 +44,6 @@ public function when_i_run( $mode, $cmd ) { * @When /^I (run|try) `([^`]+)` from '([^\s]+)'$/ */ public function when_i_run_from_a_subfolder( $mode, $cmd, $subdir ) { - $cmd = $this->get_command_with_coverage( $cmd ); $cmd = $this->replace_variables( $cmd ); $this->result = $this->wpcli_tests_invoke_proc( $this->proc( $cmd, array(), $subdir ), $mode ); list( $this->result->stdout, $this->email_sends ) = $this->wpcli_tests_capture_email_sends( $this->result->stdout ); From 7a340b8e72cf36e918472ca4eca0689c0a3854cb Mon Sep 17 00:00:00 2001 From: Isla Waters Date: Tue, 18 Mar 2025 08:19:08 -0400 Subject: [PATCH 2/2] Fix typo in path --- src/Context/FeatureContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 261f8d251..28a0613a0 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -299,7 +299,7 @@ private static function get_process_env_variables() { $with_code_coverage = (string) getenv( 'WP_CLI_TEST_COVERAGE' ); if ( \in_array( $with_code_coverage, [ 'true', '1' ], true ) ) { - $coverage_require_file = self::$behat_run_dir . '/vendor/wp-cli/wp-cli/tests/utils/generate-coverage.php'; + $coverage_require_file = self::$behat_run_dir . '/vendor/wp-cli/wp-cli-tests/utils/generate-coverage.php'; if ( ! file_exists( $coverage_require_file ) ) { // This file is not vendored inside the wp-cli-tests project $coverage_require_file = self::$behat_run_dir . '/utils/generate-coverage.php';