Skip to content

Commit

Permalink
Merge pull request #15 from gitlost/version
Browse files Browse the repository at this point in the history
Use {WP_VERSION-latest} vars to avoid needing to update tests on WP releases.
  • Loading branch information
danielbachhuber authored May 30, 2017
2 parents d1e0beb + d64e586 commit 2822bb1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/
*.zip
*.tar.gz
composer.lock
.*.swp
35 changes: 34 additions & 1 deletion features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ public function getHookDefinitionResources() {
}

public function replace_variables( $str ) {
return preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
$ret = preg_replace_callback( '/\{([A-Z_]+)\}/', array( $this, '_replace_var' ), $str );
if ( false !== strpos( $str, '{WP_VERSION-' ) ) {
$ret = $this->_replace_wp_versions( $ret );
}
return $ret;
}

private function _replace_var( $matches ) {
Expand All @@ -209,6 +213,35 @@ private function _replace_var( $matches ) {
return $cmd;
}

// Substitute "{WP_VERSION-version-latest}" variables.
private function _replace_wp_versions( $str ) {
static $wp_versions = null;
if ( null === $wp_versions ) {
$wp_versions = array();

$response = Requests::get( 'https://api.wordpress.org/core/version-check/1.7/', null, array( 'timeout' => 30 ) );
if ( 200 === $response->status_code && ( $body = json_decode( $response->body ) ) && is_object( $body ) && isset( $body->offers ) && is_array( $body->offers ) ) {
// Latest version alias.
$wp_versions["{WP_VERSION-latest}"] = count( $body->offers ) ? $body->offers[0]->version : '';
foreach ( $body->offers as $offer ) {
$sub_ver = preg_replace( '/(^[0-9]+\.[0-9]+)\.[0-9]+$/', '$1', $offer->version );
$sub_ver_key = "{WP_VERSION-{$sub_ver}-latest}";

$main_ver = preg_replace( '/(^[0-9]+)\.[0-9]+$/', '$1', $sub_ver );
$main_ver_key = "{WP_VERSION-{$main_ver}-latest}";

if ( ! isset( $wp_versions[ $main_ver_key ] ) ) {
$wp_versions[ $main_ver_key ] = $offer->version;
}
if ( ! isset( $wp_versions[ $sub_ver_key ] ) ) {
$wp_versions[ $sub_ver_key ] = $offer->version;
}
}
}
}
return strtr( $str, $wp_versions );
}

public function create_run_dir() {
if ( !isset( $this->variables['RUN_DIR'] ) ) {
$this->variables['RUN_DIR'] = sys_get_temp_dir() . '/' . uniqid( "wp-cli-test-run-", TRUE );
Expand Down
18 changes: 9 additions & 9 deletions features/core-check-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Feature: Check for more recent versions

When I run `wp core check-update`
Then STDOUT should be a table containing rows:
| version | update_type | package_url |
| 4.7.5 | major | https://downloads.wordpress.org/release/wordpress-4.7.5.zip |
| 4.4.10 | minor | https://downloads.wordpress.org/release/wordpress-4.4.10-partial-0.zip |
| version | update_type | package_url |
| {WP_VERSION-latest} | major | https://downloads.wordpress.org/release/wordpress-{WP_VERSION-latest}.zip |
| {WP_VERSION-4.4-latest} | minor | https://downloads.wordpress.org/release/wordpress-{WP_VERSION-4.4-latest}-partial-0.zip |

When I run `wp core check-update --format=count`
Then STDOUT should be:
Expand All @@ -20,8 +20,8 @@ Feature: Check for more recent versions

When I run `wp core check-update --major`
Then STDOUT should be a table containing rows:
| version | update_type | package_url |
| 4.7.5 | major | https://downloads.wordpress.org/release/wordpress-4.7.5.zip |
| version | update_type | package_url |
| {WP_VERSION-latest} | major | https://downloads.wordpress.org/release/wordpress-{WP_VERSION-latest}.zip |

When I run `wp core check-update --major --format=count`
Then STDOUT should be:
Expand All @@ -31,8 +31,8 @@ Feature: Check for more recent versions

When I run `wp core check-update --minor`
Then STDOUT should be a table containing rows:
| version | update_type | package_url |
| 4.4.10 | minor | https://downloads.wordpress.org/release/wordpress-4.4.10-partial-0.zip |
| version | update_type | package_url |
| {WP_VERSION-4.4-latest} | minor | https://downloads.wordpress.org/release/wordpress-{WP_VERSION-4.4-latest}-partial-0.zip |

When I run `wp core check-update --minor --format=count`
Then STDOUT should be:
Expand All @@ -51,5 +51,5 @@ Feature: Check for more recent versions

When I run `wp core check-update --minor`
Then STDOUT should be a table containing rows:
| version | update_type | package_url |
| 4.0.18 | minor | https://downloads.wordpress.org/release/wordpress-4.0.18-partial-0.zip |
| version | update_type | package_url |
| {WP_VERSION-4.0-latest} | minor | https://downloads.wordpress.org/release/wordpress-{WP_VERSION-4.0-latest}-partial-0.zip |
5 changes: 3 additions & 2 deletions features/core-download.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Feature: Download WordPress
And save STDOUT 'Downloading WordPress ([\d\.]+)' as {VERSION}
Then the wp-settings.php file should exist
And the {SUITE_CACHE_DIR}/core/wordpress-{VERSION}-en_US.tar.gz file should exist
And the {SUITE_CACHE_DIR}/core/wordpress-{WP_VERSION-latest}-en_US.tar.gz file should exist

When I run `mkdir inner`
And I run `cd inner && wp core download`
Expand Down Expand Up @@ -117,7 +118,7 @@ Feature: Download WordPress
Success: WordPress downloaded.
"""

# we shouldn't cache nightly builds
# we shouldn't cache nightly builds
When I run `wp core download --version=nightly --force`
Then the wp-settings.php file should exist
And STDOUT should not contain:
Expand Down Expand Up @@ -185,7 +186,7 @@ Feature: Download WordPress
And an empty cache

When I try `wp core download --version=nightly --locale=de_DE`
Then the return code should be 1
Then the return code should be 1
And STDERR should contain:
"""
Error: Nightly builds are only available for the en_US locale.
Expand Down
14 changes: 7 additions & 7 deletions features/core-update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Feature: Update WordPress core
When I run `wp core update --minor`
Then STDOUT should contain:
"""
Updating to version 3.7.21
Updating to version {WP_VERSION-3.7-latest}
"""

When I run `wp core update --minor`
Expand All @@ -53,7 +53,7 @@ Feature: Update WordPress core
When I run `wp core version`
Then STDOUT should be:
"""
3.7.21
{WP_VERSION-3.7-latest}
"""

Scenario: Update to the latest minor release (PHP 7.1 compatible with WP >= 3.9)
Expand All @@ -65,7 +65,7 @@ Feature: Update WordPress core
When I run `wp core update --minor`
Then STDOUT should contain:
"""
Updating to version 3.9.19
Updating to version {WP_VERSION-3.9-latest}
"""

When I run `wp core update --minor`
Expand All @@ -77,7 +77,7 @@ Feature: Update WordPress core
When I run `wp core version`
Then STDOUT should be:
"""
3.9.19
{WP_VERSION-3.9-latest}
"""

Scenario: Core update from cache
Expand Down Expand Up @@ -105,7 +105,7 @@ Feature: Update WordPress core
And STDOUT should not contain:
"""
Downloading
"""
"""

Scenario: Don't run update when up-to-date
Given a WP install
Expand Down Expand Up @@ -231,8 +231,8 @@ Feature: Update WordPress core
When I run `wp core update --minor`
Then STDOUT should contain:
"""
Updating to version 4.0.18 (en_US)...
Descargando paquete de instalación desde https://downloads.wordpress.org/release/wordpress-4.0.18-partial-0.zip
Updating to version {WP_VERSION-4.0-latest} (en_US)...
Descargando paquete de instalación desde https://downloads.wordpress.org/release/wordpress-{WP_VERSION-4.0-latest}-partial-0.zip
"""
And STDOUT should contain:
"""
Expand Down
3 changes: 2 additions & 1 deletion features/steps/then.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ function ( $world, $stream ) {
}
);

$steps->Then( '/^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|!=|<>) ([+\w\.-]+)$/',
$steps->Then( '/^(STDOUT|STDERR) should be a version string (<|<=|>|>=|==|=|!=|<>) ([+\w.{}-]+)$/',
function ( $world, $stream, $operator, $goal_ver ) {
$goal_ver = $world->replace_variables( $goal_ver );
$stream = strtolower( $stream );
if ( false === version_compare( trim( $world->result->$stream, "\n" ), $goal_ver, $operator ) ) {
throw new Exception( $world->result );
Expand Down

0 comments on commit 2822bb1

Please sign in to comment.