-
Notifications
You must be signed in to change notification settings - Fork 26
Add script to resolve semver for WP_VERSION env
#207
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
c57ce93
7ed37f3
d0aecb9
fd79fd6
213e968
5296351
801b601
7d8b671
52c024b
43f7a29
b1f3131
5911717
4dc3431
df2d133
aee3d2f
2faa3d1
9fd76dd
ed1adba
de04095
739021f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ composer.lock | |
| phpunit.xml | ||
| phpcs.xml | ||
| .phpcs.xml | ||
| .phpunit.result.cache | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|
|
||
| use WP_CLI\Utils; | ||
| use WP_CLI\Tests\TestCase; | ||
|
|
||
| class WPVersionResolverTest 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.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', | ||
| ); | ||
| } | ||
|
|
||
| private function data_wp_version_resolver() { | ||
| return array( | ||
| array( '5.0', '5.0' ), // Does not match any version. So return as it is. | ||
| array( '5', '6.5.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( __DIR__ ) . '/utils/wp-version-resolver.php' ); | ||
|
|
||
| $this->assertEquals( $expected, $output ); | ||
|
|
||
| // Reset the environment variable. | ||
| putenv( 'WP_VERSION' ); | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| 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' ); | ||||||||||||||||||||
|
||||||||||||||||||||
| $fp = fopen( $wp_versions_file_path, 'w' ); | |
| $fp = fopen( $wp_versions_file_path, 'w' ); | |
| if ( false === $fp ) { | |
| echo "Error: Unable to open file for writing: $wp_versions_file_path" . PHP_EOL; | |
| exit( 1 ); | |
| } |
Copilot
AI
Dec 9, 2025
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.
The return value of curl_init() is not checked. If curl fails to initialize, $ch will be false, causing subsequent curl_setopt() calls to fail. Consider checking if $ch is valid before using it.
| if ( $ch === false ) { | |
| if ( $fp ) { | |
| fclose( $fp ); | |
| } | |
| echo "Error: Failed to initialize cURL to fetch WP versions.\n"; | |
| exit( 1 ); | |
| } |
Copilot
AI
Dec 9, 2025
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.
The return value of json_decode() is not checked. If the JSON file is invalid or corrupted, this will result in null, causing errors in subsequent operations. Consider checking if decoding was successful and handle the error case appropriately.
| if ( !is_array( $wp_versions_json ) ) { | |
| echo "Error: Failed to decode WP versions JSON file.\n"; | |
| echo $wp_version_env; | |
| exit( 1 ); | |
| } |
Copilot
AI
Dec 9, 2025
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.
The script doesn't handle the trunk value for WP_VERSION, which was previously supported according to the README documentation. The script should either handle trunk as a special case (similar to latest) or the documentation should be updated to reflect this breaking change.
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:
The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0. Ref: https://getcomposer.org/doc/articles/versions.md#tilde-version-range-
Copilot
AI
Dec 9, 2025
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.
[nitpick] Missing space before the ternary operator. The expression should be $wp_version ? $wp_version : $wp_version_env with proper spacing for readability.
| echo $wp_version ? : $wp_version_env; | |
| echo $wp_version ? $wp_version : $wp_version_env; |
Copilot
AI
Dec 9, 2025
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.
[nitpick] The end() function modifies the internal pointer of the array. If $wp_satisfied_versions is empty, end() returns false, which is then correctly handled by the ternary operator. However, consider using a more explicit check or alternative approach like array_pop(array_slice($wp_satisfied_versions, -1)) or checking if the array is empty before calling end().
| $wp_version = end( $wp_satisfied_versions ); | |
| echo $wp_version ? : $wp_version_env; | |
| $wp_version = !empty($wp_satisfied_versions) ? $wp_satisfied_versions[count($wp_satisfied_versions) - 1] : false; | |
| echo $wp_version ? $wp_version : $wp_version_env; |
Uh oh!
There was an error while loading. Please reload this page.