Skip to content

Commit 06c88e4

Browse files
Copilotschlesseraclaudeswissspidy
authored
Use Utils\make_temp_file() / Utils\make_temp_dir() for package ZIP install temp paths (#250)
Co-authored-by: schlessera <83631+schlessera@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 55bdd2b commit 06c88e4

2 files changed

Lines changed: 104 additions & 6 deletions

File tree

‎features/package-install.feature‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,98 @@ Feature: Install WP-CLI packages
10171017
wp-cli/google-sitemap-generator-cli
10181018
"""
10191019
1020+
Scenario: Install a package from a remote zip URL
1021+
Given an empty directory
1022+
And a remote-zip-test-command/composer.json file:
1023+
"""
1024+
{
1025+
"name": "wp-cli-test/remote-zip-test-command",
1026+
"description": "Dummy package served over HTTP as a ZIP archive for acceptance tests.",
1027+
"type": "wp-cli-package"
1028+
}
1029+
"""
1030+
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
1032+
1033+
When I run `wp package install http://localhost:8080/remote-zip-test-command.zip`
1034+
Then STDOUT should contain:
1035+
"""
1036+
Installing package wp-cli-test/remote-zip-test-command
1037+
"""
1038+
And STDOUT should contain:
1039+
"""
1040+
Success: Package installed.
1041+
"""
1042+
1043+
When I run `wp package is-installed wp-cli-test/remote-zip-test-command`
1044+
Then the return code should be 0
1045+
And STDERR should be empty
1046+
And STDOUT should be empty
1047+
1048+
Scenario: Install a package from a remote zip URL with files at archive root
1049+
Given an empty directory
1050+
And a composer-flat.json file:
1051+
"""
1052+
{
1053+
"name": "wp-cli-test/remote-zip-flat-command",
1054+
"description": "Dummy flat-layout ZIP package for acceptance tests.",
1055+
"type": "wp-cli-package"
1056+
}
1057+
"""
1058+
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
1060+
1061+
When I run `wp package install http://localhost:8080/remote-zip-flat-command.zip`
1062+
Then STDOUT should contain:
1063+
"""
1064+
Installing package wp-cli-test/remote-zip-flat-command
1065+
"""
1066+
And STDOUT should contain:
1067+
"""
1068+
Success: Package installed.
1069+
"""
1070+
1071+
When I run `wp package is-installed wp-cli-test/remote-zip-flat-command`
1072+
Then the return code should be 0
1073+
And STDERR should be empty
1074+
And STDOUT should be empty
1075+
1076+
Scenario: Install from a remote zip URL with invalid zip contents
1077+
Given an empty directory
1078+
And an invalid.zip file:
1079+
"""
1080+
this is not a zip archive
1081+
"""
1082+
And a PHP built-in web server
1083+
1084+
When I try `wp package install http://localhost:8080/invalid.zip`
1085+
Then the return code should be 1
1086+
And STDERR should contain:
1087+
"""
1088+
ZipArchive failed to unzip
1089+
"""
1090+
1091+
Scenario: Install from a remote zip URL that returns 404
1092+
Given an empty directory
1093+
And a PHP built-in web server
1094+
1095+
When I try `wp package install http://localhost:8080/missing.zip`
1096+
Then the return code should be 1
1097+
And STDERR should contain:
1098+
"""
1099+
Couldn't download package from 'http://localhost:8080/missing.zip' (HTTP code 404).
1100+
"""
1101+
1102+
Scenario: Install from a remote zip URL that cannot be reached
1103+
Given an empty directory
1104+
1105+
When I try `wp package install http://localhost:1/unreachable.zip`
1106+
Then the return code should be 1
1107+
And STDERR should contain:
1108+
"""
1109+
Error: Failed to get url 'http://localhost:1/unreachable.zip'
1110+
"""
1111+
10201112
@github-api
10211113
Scenario: Install a package from Git using a shortened mixed-case package identifier but lowercase composer.json name
10221114
Given an empty directory

‎src/Package_Command.php‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,22 +276,28 @@ public function install( $args, $assoc_args ) {
276276
// Download the remote ZIP file to a temp directory
277277
$temp = false;
278278
if ( false !== strpos( $package_name, '://' ) ) {
279-
$temp = Utils\get_temp_dir() . uniqid( 'wp-cli-package_', true /*more_entropy*/ ) . '.zip';
279+
$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 ) );
291297
}
292298
$package_name = $temp;
293299
}
294-
$dir_package = Utils\get_temp_dir() . uniqid( 'wp-cli-package_', true /*more_entropy*/ );
300+
$dir_package = Utils\make_temp_dir( 'wp-cli-package_' );
295301
try {
296302
// Extract the package to get the package name
297303
Extractor::extract( $package_name, $dir_package );

0 commit comments

Comments
 (0)