Skip to content

Commit 60588ac

Browse files
swissspidyclaude
andcommitted
Fix remote ZIP tests and clean up temp file on download failure
The `a PHP built-in web server` step relies on a router script from wp-cli/server-command that does not exist in an empty run directory, so every request returned an error page with a 200 status and the download "succeeded" with non-ZIP content. Start a plain PHP built-in server in the background instead, which serves the ZIP fixtures directly and returns a real 404 for missing files. `Utils\make_temp_file()` creates the file immediately, so a connection or SSL failure in `Utils\http_request()` used to exit via `WP_CLI::error()` and leave an empty `wp-cli-package_*.zip` in the temp directory. Disable `halt_on_error` and remove the temp file before reporting the error. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019g8sd6U4w59qiQW7LTt1jG
1 parent 23066fb commit 60588ac

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

‎features/package-install.feature‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ Feature: Install WP-CLI packages
10281028
}
10291029
"""
10301030
And I run `php -r '$zip = new ZipArchive(); $zip->open( "remote-zip-test-command.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFromString( "remote-zip-test-command/composer.json", file_get_contents( "remote-zip-test-command/composer.json" ) ); $zip->close();'`
1031-
And a PHP built-in web server
1031+
And I launch in the background `php -S localhost:8080 -t .`
10321032
10331033
When I run `wp package install http://localhost:8080/remote-zip-test-command.zip`
10341034
Then STDOUT should contain:
@@ -1056,7 +1056,7 @@ Feature: Install WP-CLI packages
10561056
}
10571057
"""
10581058
And I run `php -r '$zip = new ZipArchive(); $zip->open( "remote-zip-flat-command.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFromString( "composer.json", file_get_contents( "composer-flat.json" ) ); $zip->close();'`
1059-
And a PHP built-in web server
1059+
And I launch in the background `php -S localhost:8080 -t .`
10601060
10611061
When I run `wp package install http://localhost:8080/remote-zip-flat-command.zip`
10621062
Then STDOUT should contain:
@@ -1079,7 +1079,7 @@ Feature: Install WP-CLI packages
10791079
"""
10801080
this is not a zip archive
10811081
"""
1082-
And a PHP built-in web server
1082+
And I launch in the background `php -S localhost:8080 -t .`
10831083
10841084
When I try `wp package install http://localhost:8080/invalid.zip`
10851085
Then the return code should be 1
@@ -1090,7 +1090,7 @@ Feature: Install WP-CLI packages
10901090
10911091
Scenario: Install from a remote zip URL that returns 404
10921092
Given an empty directory
1093-
And a PHP built-in web server
1093+
And I launch in the background `php -S localhost:8080 -t .`
10941094
10951095
When I try `wp package install http://localhost:8080/missing.zip`
10961096
Then the return code should be 1

‎src/Package_Command.php‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,19 @@ public function install( $args, $assoc_args ) {
278278
if ( false !== strpos( $package_name, '://' ) ) {
279279
$temp = Utils\make_temp_file( 'wp-cli-package_', '.zip' );
280280
$options = [
281-
'timeout' => 600,
282-
'filename' => $temp,
283-
'insecure' => $insecure,
281+
'timeout' => 600,
282+
'filename' => $temp,
283+
'insecure' => $insecure,
284+
'halt_on_error' => false, // Handle failures here so the temp file is cleaned up.
284285
];
285286
$gitlab_token = getenv( 'GITLAB_TOKEN' ); // Use GITLAB_TOKEN if available to avoid authorization failures or rate-limiting.
286287
$headers = $gitlab_token && 'gitlab.com' === strtolower( (string) Utils\parse_url( $package_name, PHP_URL_HOST ) ) ? [ 'PRIVATE-TOKEN' => $gitlab_token ] : [];
287-
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
288+
try {
289+
$response = Utils\http_request( 'GET', $package_name, null, $headers, $options );
290+
} catch ( Exception $e ) {
291+
@unlink( $temp ); // @codingStandardsIgnoreLine
292+
WP_CLI::error( $e->getMessage() );
293+
}
288294
if ( 20 !== (int) substr( (string) $response->status_code, 0, 2 ) ) {
289295
@unlink( $temp ); // @codingStandardsIgnoreLine
290296
WP_CLI::error( sprintf( "Couldn't download package from '%s' (HTTP code %d).", $package_name, $response->status_code ) );

0 commit comments

Comments
 (0)