Skip to content

Commit 5603469

Browse files
Copilotswissspidy
andauthored
Fix review comments: slug validation, version URL validation, HTTP status checks, and feature tests
- Move empty-slug check to top of __invoke() before any filesystem ops - Add HEAD request validation for specific version URLs before downloading - Check HTTP response status code after download and clean up on failure - Update feature file: use 'Given an empty directory' instead of WP install, add scenarios for --version, --path, --force, and error paths - Add wp-cli's phpstan scan-files.php to resolve Requests_Response type Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 298a82d commit 5603469

4 files changed

Lines changed: 197 additions & 14 deletions

File tree

‎features/extension-download.feature‎

Lines changed: 138 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Feature: Download WordPress.org extensions without loading WordPress
22

33
Scenario: Downloading a plugin package works before WordPress is loaded
4-
Given a WP install
4+
Given an empty directory
55

6-
When I run `wp plugin download debug-bar --skip-wordpress`
6+
When I run `wp plugin download debug-bar`
77
Then STDOUT should contain:
88
"""
99
Downloading debug-bar
@@ -16,10 +16,77 @@ Feature: Download WordPress.org extensions without loading WordPress
1616
And the {DOWNLOADED_PLUGIN} file should exist
1717
And STDERR should be empty
1818

19+
Scenario: Downloading a plugin package to a custom path
20+
Given an empty directory
21+
22+
When I run `wp plugin download debug-bar --path=/tmp/wp-cli-download-test-plugin`
23+
Then STDOUT should contain:
24+
"""
25+
Success: Downloaded plugin package to
26+
"""
27+
And the /tmp/wp-cli-download-test-plugin/debug-bar file should exist
28+
And STDERR should be empty
29+
30+
Scenario: Downloading a specific version of a plugin
31+
Given an empty directory
32+
33+
When I run `wp plugin download debug-bar --version=1.4`
34+
Then STDOUT should contain:
35+
"""
36+
Downloading debug-bar (1.4)
37+
"""
38+
And STDOUT should contain:
39+
"""
40+
Success: Downloaded plugin package to
41+
"""
42+
And STDERR should be empty
43+
44+
Scenario: Downloading a non-existent version of a plugin fails with clear error
45+
Given an empty directory
46+
47+
When I try `wp plugin download debug-bar --version=9.9.9`
48+
Then STDERR should contain:
49+
"""
50+
Error: Can't find the requested plugin's version 9.9.9
51+
"""
52+
And the return code should be 1
53+
54+
Scenario: Downloading a plugin with --force overwrites existing file
55+
Given an empty directory
56+
57+
When I run `wp plugin download debug-bar`
58+
And I run `wp plugin download debug-bar --force`
59+
Then STDOUT should contain:
60+
"""
61+
Success: Downloaded plugin package to
62+
"""
63+
And STDERR should be empty
64+
65+
Scenario: Downloading a plugin without --force fails if destination exists
66+
Given an empty directory
67+
68+
When I run `wp plugin download debug-bar`
69+
And I try `wp plugin download debug-bar`
70+
Then STDERR should contain:
71+
"""
72+
Error: Destination file already exists:
73+
"""
74+
And the return code should be 1
75+
76+
Scenario: Downloading an unknown plugin fails with a clear error
77+
Given an empty directory
78+
79+
When I try `wp plugin download this-plugin-does-not-exist-xyz-abc-123`
80+
Then STDERR should contain:
81+
"""
82+
Error: The 'this-plugin-does-not-exist-xyz-abc-123' plugin could not be found.
83+
"""
84+
And the return code should be 1
85+
1986
Scenario: Downloading a theme package works before WordPress is loaded
20-
Given a WP install
87+
Given an empty directory
2188

22-
When I run `wp theme download twentytwelve --skip-wordpress`
89+
When I run `wp theme download twentytwelve`
2390
Then STDOUT should contain:
2491
"""
2592
Downloading twentytwelve
@@ -31,3 +98,70 @@ Feature: Download WordPress.org extensions without loading WordPress
3198
And save STDOUT 'Success: Downloaded theme package to (.+)' as {DOWNLOADED_THEME}
3299
And the {DOWNLOADED_THEME} file should exist
33100
And STDERR should be empty
101+
102+
Scenario: Downloading a theme package to a custom path
103+
Given an empty directory
104+
105+
When I run `wp theme download twentytwelve --path=/tmp/wp-cli-download-test-theme`
106+
Then STDOUT should contain:
107+
"""
108+
Success: Downloaded theme package to
109+
"""
110+
And the /tmp/wp-cli-download-test-theme/twentytwelve file should exist
111+
And STDERR should be empty
112+
113+
Scenario: Downloading a specific version of a theme
114+
Given an empty directory
115+
116+
When I run `wp theme download twentytwelve --version=1.3`
117+
Then STDOUT should contain:
118+
"""
119+
Downloading twentytwelve (1.3)
120+
"""
121+
And STDOUT should contain:
122+
"""
123+
Success: Downloaded theme package to
124+
"""
125+
And STDERR should be empty
126+
127+
Scenario: Downloading a non-existent version of a theme fails with clear error
128+
Given an empty directory
129+
130+
When I try `wp theme download twentytwelve --version=9.9.9`
131+
Then STDERR should contain:
132+
"""
133+
Error: Can't find the requested theme's version 9.9.9
134+
"""
135+
And the return code should be 1
136+
137+
Scenario: Downloading a theme with --force overwrites existing file
138+
Given an empty directory
139+
140+
When I run `wp theme download twentytwelve`
141+
And I run `wp theme download twentytwelve --force`
142+
Then STDOUT should contain:
143+
"""
144+
Success: Downloaded theme package to
145+
"""
146+
And STDERR should be empty
147+
148+
Scenario: Downloading a theme without --force fails if destination exists
149+
Given an empty directory
150+
151+
When I run `wp theme download twentytwelve`
152+
And I try `wp theme download twentytwelve`
153+
Then STDERR should contain:
154+
"""
155+
Error: Destination file already exists:
156+
"""
157+
And the return code should be 1
158+
159+
Scenario: Downloading an unknown theme fails with a clear error
160+
Given an empty directory
161+
162+
When I try `wp theme download this-theme-does-not-exist-xyz-abc-123`
163+
Then STDERR should contain:
164+
"""
165+
Error: The 'this-theme-does-not-exist-xyz-abc-123' theme could not be found.
166+
"""
167+
And the return code should be 1

‎phpstan.neon.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- vendor/wp-cli/wp-cli/utils/phpstan/scan-files.php
1011
treatPhpDocTypesAsCertain: false
1112
ignoreErrors:
1213
- identifier: missingType.iterableValue

‎src/Plugin_Download_Command.php‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class Plugin_Download_Command {
4141
* @param array{path?: string, version?: string, force?: bool, insecure?: bool} $assoc_args Associative arguments.
4242
*/
4343
public function __invoke( $args, $assoc_args ) {
44-
$slug = (string) $args[0];
44+
$slug = (string) $args[0];
45+
if ( '' === $slug ) {
46+
WP_CLI::error( 'Please provide a plugin slug.' );
47+
}
48+
4549
$insecure = Utils\get_flag_value( $assoc_args, 'insecure', false );
4650
$force = Utils\get_flag_value( $assoc_args, 'force', false );
4751
$requested = Utils\get_flag_value( $assoc_args, 'version', null );
4852
$download_dir = Utils\get_flag_value( $assoc_args, 'path', getcwd() );
49-
if ( '' === $slug ) {
50-
WP_CLI::error( 'Please provide a plugin slug.' );
51-
}
5253

5354
if ( ! is_dir( $download_dir ) ) {
5455
if ( ! @mkdir( $download_dir, 0755, true ) ) {
@@ -81,6 +82,22 @@ public function __invoke( $args, $assoc_args ) {
8182
} else {
8283
$download_url = str_replace( $current_zip, $slug . '.' . $requested . '.zip', $download_url );
8384
$version = $requested;
85+
86+
try {
87+
$head_response = Utils\http_request( 'HEAD', $download_url, null, [], [ 'insecure' => (bool) $insecure ] );
88+
} catch ( Exception $exception ) {
89+
WP_CLI::error( $exception->getMessage() );
90+
}
91+
92+
if ( 200 !== (int) $head_response->status_code ) {
93+
WP_CLI::error(
94+
sprintf(
95+
"Can't find the requested plugin's version %s in the WordPress.org plugin repository (HTTP code %d).",
96+
$requested,
97+
$head_response->status_code
98+
)
99+
);
100+
}
84101
}
85102
}
86103

@@ -98,7 +115,7 @@ public function __invoke( $args, $assoc_args ) {
98115
WP_CLI::log( "Downloading {$slug} ({$version})..." );
99116

100117
try {
101-
Utils\http_request(
118+
$response = Utils\http_request(
102119
'GET',
103120
$download_url,
104121
null,
@@ -112,6 +129,13 @@ public function __invoke( $args, $assoc_args ) {
112129
WP_CLI::error( $exception->getMessage() );
113130
}
114131

132+
if ( 200 !== (int) $response->status_code ) {
133+
if ( file_exists( $download_file ) ) {
134+
unlink( $download_file );
135+
}
136+
WP_CLI::error( sprintf( 'Failed to download plugin package (HTTP code %d).', $response->status_code ) );
137+
}
138+
115139
WP_CLI::success( "Downloaded plugin package to {$download_file}" );
116140
}
117141
}

‎src/Theme_Download_Command.php‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class Theme_Download_Command {
4141
* @param array{path?: string, version?: string, force?: bool, insecure?: bool} $assoc_args Associative arguments.
4242
*/
4343
public function __invoke( $args, $assoc_args ) {
44-
$slug = (string) $args[0];
44+
$slug = (string) $args[0];
45+
if ( '' === $slug ) {
46+
WP_CLI::error( 'Please provide a theme slug.' );
47+
}
48+
4549
$insecure = Utils\get_flag_value( $assoc_args, 'insecure', false );
4650
$force = Utils\get_flag_value( $assoc_args, 'force', false );
4751
$requested = Utils\get_flag_value( $assoc_args, 'version', null );
4852
$download_dir = Utils\get_flag_value( $assoc_args, 'path', getcwd() );
49-
if ( '' === $slug ) {
50-
WP_CLI::error( 'Please provide a theme slug.' );
51-
}
5253

5354
if ( ! is_dir( $download_dir ) ) {
5455
if ( ! @mkdir( $download_dir, 0755, true ) ) {
@@ -81,6 +82,22 @@ public function __invoke( $args, $assoc_args ) {
8182
} else {
8283
$download_url = str_replace( $current_zip, $slug . '.' . $requested . '.zip', $download_url );
8384
$version = $requested;
85+
86+
try {
87+
$head_response = Utils\http_request( 'HEAD', $download_url, null, [], [ 'insecure' => (bool) $insecure ] );
88+
} catch ( Exception $exception ) {
89+
WP_CLI::error( $exception->getMessage() );
90+
}
91+
92+
if ( 200 !== (int) $head_response->status_code ) {
93+
WP_CLI::error(
94+
sprintf(
95+
"Can't find the requested theme's version %s in the WordPress.org theme repository (HTTP code %d).",
96+
$requested,
97+
$head_response->status_code
98+
)
99+
);
100+
}
84101
}
85102
}
86103

@@ -98,7 +115,7 @@ public function __invoke( $args, $assoc_args ) {
98115
WP_CLI::log( "Downloading {$slug} ({$version})..." );
99116

100117
try {
101-
Utils\http_request(
118+
$response = Utils\http_request(
102119
'GET',
103120
$download_url,
104121
null,
@@ -112,6 +129,13 @@ public function __invoke( $args, $assoc_args ) {
112129
WP_CLI::error( $exception->getMessage() );
113130
}
114131

132+
if ( 200 !== (int) $response->status_code ) {
133+
if ( file_exists( $download_file ) ) {
134+
unlink( $download_file );
135+
}
136+
WP_CLI::error( sprintf( 'Failed to download theme package (HTTP code %d).', $response->status_code ) );
137+
}
138+
115139
WP_CLI::success( "Downloaded theme package to {$download_file}" );
116140
}
117141
}

0 commit comments

Comments
 (0)