-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add script to resolve semver for WP_VERSION
env
#207
Open
thelovekesh
wants to merge
16
commits into
main
Choose a base branch
from
feature/wp-version-resolve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c57ce93
Add composer/semver package
thelovekesh 7ed37f3
Remove latest WP version resolving from run-behat-tests bin file
thelovekesh d0aecb9
Add util file to be ignored from WPCS
thelovekesh fd79fd6
Add PHP script to resolve minor and patch WP version
thelovekesh 213e968
Add step to update WP_VERSION
thelovekesh 5296351
Add .phpunit.result.cache
thelovekesh 801b601
Fix incorrect return condition
thelovekesh 7d8b671
Add test cases for WP version resolver
thelovekesh 52c024b
Fix PHPCS errors
thelovekesh 43f7a29
Merge branch 'main' into feature/wp-version-resolve
swissspidy b1f3131
Update assertion method to check for same values
thelovekesh 5911717
Fix minor version constraint
thelovekesh 4dc3431
Update test file name
thelovekesh df2d133
Update test cases for minor version assertions
thelovekesh aee3d2f
Fix data provider visibility
thelovekesh 2faa3d1
Make the data providers static
thelovekesh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
use WP_CLI\Utils; | ||
use WP_CLI\Tests\TestCase; | ||
|
||
class TestWPVersionResolverTest extends TestCase { | ||
|
||
private $temp_file; | ||
|
||
protected function set_up() { | ||
parent::set_up(); | ||
|
||
$this->temp_file = Utils\get_temp_dir() . 'wp-cli-tests-wp-versions.json'; | ||
|
||
$fp = fopen( $this->temp_file, 'w' ); | ||
fwrite( $fp, json_encode( $this->wp_versions_data() ) ); | ||
fclose( $fp ); | ||
} | ||
|
||
protected function tear_down() { | ||
if ( $this->temp_file && file_exists( $this->temp_file ) ) { | ||
unlink( $this->temp_file ); | ||
} | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
private function wp_versions_data() { | ||
return array( | ||
'5.4' => 'insecure', | ||
'5.9' => 'insecure', | ||
'5.9.1' => 'insecure', | ||
'5.9.2' => 'insecure', | ||
'6.0' => 'insecure', | ||
'6.0.1' => 'insecure', | ||
'6.0.2' => 'insecure', | ||
'6.1' => 'insecure', | ||
'6.1.1' => 'insecure', | ||
'6.1.2' => 'insecure', | ||
'6.2' => 'insecure', | ||
'6.2.1' => 'insecure', | ||
'6.2.2' => 'insecure', | ||
'6.5' => 'insecure', | ||
'6.5.2' => 'latest', | ||
); | ||
} | ||
|
||
public function data_wp_version_resolver() { | ||
return array( | ||
array( '5.0', '5.0' ), // Does not match any version. So return as it is. | ||
array( '5', '5.9.2' ), // Return the latest major version. | ||
array( '5.9', '5.9.2' ), // Return the latest patch version. | ||
array( '5.9.1', '5.9.1' ), // Return the exact version. | ||
array( '6', '6.5.2' ), // Return the latest minor version. | ||
array( '6.0', '6.0.2' ), // Return the latest patch version. | ||
array( '6.0.0', '6.0' ), // Return the requested version. | ||
array( '', '6.5.2' ), // Return the latest version. | ||
array( 'latest', '6.5.2' ), // Return the latest version. | ||
array( 'some-mismatched-version', 'some-mismatched-version' ), // Does not match any version. So return as it is. | ||
array( '6.5-alpha', '6.5.2' ), // Return the latest version. | ||
array( '6.5-beta', '6.5.2' ), // Return the latest version. | ||
array( '6.5-rc', '6.5.2' ), // Return the latest version. | ||
array( '6.5-nightly', '6.5-nightly' ), // Does not match any version. So return as it is. | ||
array( '6.5.0.0', '6.5' ), // Return the latest version. | ||
array( '6.5.2.0', '6.5.2' ), // Return the latest version. | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider data_wp_version_resolver | ||
*/ | ||
public function test_wp_version_resolver( $env, $expected ) { | ||
if ( $env ) { | ||
putenv( "WP_VERSION=$env" ); | ||
} | ||
|
||
$output = exec( 'php ' . dirname( dirname( __DIR__ ) ) . '/utils/wp-version-resolver.php' ); | ||
|
||
// Reset the environment variable. | ||
putenv( 'WP_VERSION' ); | ||
|
||
$this->assertSame( $expected, $output ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* Resolve the WP version to use for the tests. | ||
*/ | ||
|
||
use Composer\Semver\Semver; | ||
|
||
const WP_VERSIONS_JSON_FILE = '/wp-cli-tests-wp-versions.json'; | ||
const WP_VERSIONS_JSON_URL = 'https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json'; | ||
|
||
$wp_version_env = getenv( 'WP_VERSION' ); | ||
$wp_versions_file_path = sys_get_temp_dir() . WP_VERSIONS_JSON_FILE; | ||
|
||
if ( ! file_exists( $wp_versions_file_path ) ) { | ||
$ch = curl_init( WP_VERSIONS_JSON_URL ); | ||
$fp = fopen( $wp_versions_file_path, 'w' ); | ||
|
||
curl_setopt( $ch, CURLOPT_FILE, $fp ); | ||
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); | ||
|
||
curl_exec( $ch ); | ||
curl_close( $ch ); | ||
fclose( $fp ); | ||
} | ||
|
||
$wp_versions_json = json_decode( file_get_contents( $wp_versions_file_path ), true ); | ||
|
||
if ( empty( $wp_version_env ) || 'latest' === $wp_version_env ) { | ||
$wp_version = array_search( 'latest', $wp_versions_json, true ); | ||
|
||
if ( empty( $wp_version ) ) { | ||
$wp_version = $wp_version_env; | ||
} | ||
|
||
echo $wp_version; | ||
exit( 0 ); | ||
} | ||
|
||
$wp_version = ''; | ||
$constraint = ''; | ||
$wp_versions = array_keys( $wp_versions_json ); | ||
$version_count = count( explode( '.', $wp_version_env ) ); | ||
|
||
if ( 1 === $version_count ) { | ||
$constraint = "^$wp_version_env"; // Get the latest minor version. | ||
} elseif ( 2 === $version_count ) { | ||
$constraint = "~$wp_version_env.0"; // Get the latest patch version. | ||
} elseif ( 3 === $version_count ) { | ||
$constraint = "=$wp_version_env"; // Get the exact version. | ||
} else { | ||
$constraint = $wp_version_env; | ||
} | ||
|
||
if ( ! class_exists( 'Composer\Semver\Semver' ) ) { | ||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
} | ||
|
||
try { | ||
$wp_satisfied_versions = Semver::satisfiedBy( $wp_versions, $constraint ); | ||
} catch ( Exception $e ) { | ||
echo $wp_version_env; | ||
exit( 0 ); | ||
} | ||
|
||
$wp_version = end( $wp_satisfied_versions ); | ||
echo $wp_version ? : $wp_version_env; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
.0
as patch version due to semver package API behavior: