diff --git a/features/testing.feature b/features/testing.feature index 4bee962c4..47b0377dd 100644 --- a/features/testing.feature +++ b/features/testing.feature @@ -62,3 +62,21 @@ Feature: Test that WP-CLI loads. """ sqlite """ + + Scenario: WP installation with specific version + Given a WP 6.4.2 installation + + When I run `wp core version` + Then STDOUT should be: + """ + 6.4.2 + """ + + Scenario: WP installation in subdirectory with specific version + Given a WP 6.3.1 installation in 'wordpress' + + When I run `wp core version --path=wordpress` + Then STDOUT should be: + """ + 6.3.1 + """ diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 4ec3c15a3..7cff16319 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -621,10 +621,12 @@ private static function configure_sqlite( $dir ): void { /** * We cache the results of `wp core download` to improve test performance. * Ideally, we'd cache at the HTTP layer for more reliable tests. + * + * @param string $version */ - private static function cache_wp_files(): void { - $wp_version = getenv( 'WP_VERSION' ); - $wp_version_suffix = ( false !== $wp_version ) ? "-$wp_version" : ''; + private static function cache_wp_files( $version = '' ): void { + $wp_version = $version ?: getenv( 'WP_VERSION' ); + $wp_version_suffix = $wp_version ? "-$wp_version" : ''; self::$cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-download-cache' . $wp_version_suffix; self::$sqlite_cache_dir = sys_get_temp_dir() . '/wp-cli-test-sqlite-integration-cache'; @@ -1288,10 +1290,15 @@ public function add_line_to_wp_config( &$wp_config_code, $line ): void { /** * @param string $subdir + * @param string $version */ - public function download_wp( $subdir = '' ): void { - if ( ! self::$cache_dir ) { - self::cache_wp_files(); + public function download_wp( $subdir = '', $version = '' ): void { + $wp_version = $version ?: getenv( 'WP_VERSION' ); + $wp_version_suffix = $wp_version ? "-$wp_version" : ''; + $expected_cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-download-cache' . $wp_version_suffix; + + if ( ! self::$cache_dir || self::$cache_dir !== $expected_cache_dir ) { + self::cache_wp_files( $version ); $result = Process::create( Utils\esc_cmd( 'wp core version --debug --path=%s', self::$cache_dir ), null, self::get_process_env_variables() )->run_check(); echo "[Debug messages]\n"; @@ -1365,10 +1372,11 @@ public function create_config( $subdir = '', $extra_php = false ): void { /** * @param string $subdir + * @param string $version */ - public function install_wp( $subdir = '' ): void { - $wp_version = getenv( 'WP_VERSION' ); - $wp_version_suffix = ( false !== $wp_version ) ? "-$wp_version" : ''; + public function install_wp( $subdir = '', $version = '' ): void { + $wp_version = $version ?: getenv( 'WP_VERSION' ); + $wp_version_suffix = $wp_version ? "-$wp_version" : ''; self::$install_cache_dir = sys_get_temp_dir() . '/wp-cli-test-core-install-cache' . $wp_version_suffix; if ( ! file_exists( self::$install_cache_dir ) ) { mkdir( self::$install_cache_dir ); @@ -1383,7 +1391,7 @@ public function install_wp( $subdir = '' ): void { $this->create_db(); } $this->create_run_dir(); - $this->download_wp( $subdir ); + $this->download_wp( $subdir, $version ); $this->create_config( $subdir, $config_extra_php ); $install_args = [ diff --git a/src/Context/GivenStepDefinitions.php b/src/Context/GivenStepDefinitions.php index 248766a0e..6321bf235 100644 --- a/src/Context/GivenStepDefinitions.php +++ b/src/Context/GivenStepDefinitions.php @@ -391,14 +391,20 @@ public function given_a_database(): void { * Scenario: My other scenario * Given a WP install * ... + * + * Scenario: My version-specific scenario + * Given a WP 6.4.2 installation + * ... * ``` * * @access public * - * @Given a WP install(ation) + * @Given /^a WP( [^\s]+)? install(?:ation)?$/ + * + * @param string $version Optional version number (may include leading space) */ - public function given_a_wp_installation(): void { - $this->install_wp(); + public function given_a_wp_installation( $version = '' ): void { + $this->install_wp( '', trim( $version ) ); } /** @@ -412,16 +418,21 @@ public function given_a_wp_installation(): void { * Scenario: My other scenario * Given a WP install in 'bar' * ... + * + * Scenario: My version-specific scenario + * Given a WP 6.4.2 installation in 'subdir' + * ... * ``` * * @access public * - * @Given a WP install(ation) in :subdir + * @Given /^a WP( [^\s]+)? install(?:ation)? in ['"]?([^'"]+)['"]?$/ * + * @param string $version Optional version number (may include leading space) * @param string $subdir */ - public function given_a_wp_installation_in_a_specific_folder( $subdir ): void { - $this->install_wp( $subdir ); + public function given_a_wp_installation_in_a_specific_folder( $version = '', $subdir = '' ): void { + $this->install_wp( $subdir, trim( $version ) ); } /**