-
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 all 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,98 @@ | ||||||||||
| <?php | ||||||||||
|
|
||||||||||
| use WP_CLI\Utils; | ||||||||||
| use WP_CLI\Tests\TestCase; | ||||||||||
| use PHPUnit\Framework\Attributes\DataProvider; | ||||||||||
|
|
||||||||||
| class TestWPVersionResolverTest extends TestCase { | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @var string | ||||||||||
| */ | ||||||||||
| 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(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return array<string, string> | ||||||||||
| */ | ||||||||||
| private function wp_versions_data(): array { | ||||||||||
| 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', | ||||||||||
| ); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * @return array<int, array{0: string, 1: string}> | ||||||||||
| */ | ||||||||||
| public static function data_wp_version_resolver(): array { | ||||||||||
| 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. | ||||||||||
|
Comment on lines
+74
to
+75
|
||||||||||
| array( '6.5.0.0', '6.5' ), // Return the latest version. | |
| array( '6.5.2.0', '6.5.2' ), // Return the latest version. | |
| array( '6.5.0.0', '6.5' ), // Does not match standard semver. Return as normalized version. | |
| array( '6.5.2.0', '6.5.2' ), // Does not match standard semver. Return as normalized version. |
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 environment variable is not properly unset. Using putenv('WP_VERSION') without a value may not work correctly on all systems. Consider using putenv('WP_VERSION=') with an empty string or checking if the variable needs to be restored to its previous value.
| putenv( 'WP_VERSION' ); | |
| putenv( 'WP_VERSION=' ); |
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 test doesn't verify the actual error handling behavior when the JSON file doesn't exist initially. While the setup creates the file, consider adding a test case that verifies the script correctly downloads the JSON file when it's missing.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||||||||||
| <?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 curl_exec() is not checked. If the curl request fails, the script will continue and potentially read an empty or invalid JSON file. Consider checking the return value and handling failure cases appropriately.
| curl_exec( $ch ); | |
| if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0. | |
| // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated | |
| curl_close( $ch ); | |
| } | |
| fclose( $fp ); | |
| $result = curl_exec( $ch ); | |
| if ( PHP_VERSION_ID < 80000 ) { // curl_close() has no effect as of PHP 8.0. | |
| // phpcs:ignore PHPCompatibility.FunctionUse.RemovedFunctions.curl_closeDeprecated,Generic.PHP.DeprecatedFunctions.Deprecated | |
| curl_close( $ch ); | |
| } | |
| fclose( $fp ); | |
| if ( $result === false ) { | |
| // Remove incomplete file if it exists. | |
| if ( file_exists( $wp_versions_file_path ) ) { | |
| unlink( $wp_versions_file_path ); | |
| } | |
| fwrite( STDERR, "Error: Failed to download WP versions JSON from " . WP_VERSIONS_JSON_URL . "\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; |
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 class name has a redundant "Test" suffix. The class is already named
TestWPVersionResolverTest, which is redundant. Consider renaming toTestWPVersionResolverorWPVersionResolverTest.