Skip to content

Commit b325338

Browse files
Copilotswissspidy
andcommitted
Require --context=admin for admin URL profiling, add error and test
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 51c34f6 commit b325338

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

features/profile-stage.feature

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Feature: Profile the template render stage
176176
Scenario: Admin URL runs as a backend request and skips frontend stages
177177
Given a WP install
178178

179-
When I run `wp profile stage --url=example.com/wp-admin/ --fields=stage`
179+
When I run `wp profile stage --url=example.com/wp-admin/ --context=admin --fields=stage`
180180
Then STDOUT should be a table containing rows:
181181
| stage |
182182
| bootstrap |
@@ -189,3 +189,14 @@ Feature: Profile the template render stage
189189
template
190190
"""
191191
And STDERR should be empty
192+
193+
@require-wp-4.0
194+
Scenario: Admin URL without --context=admin emits an error
195+
Given a WP install
196+
197+
When I try `wp profile stage --url=example.com/wp-admin/ --fields=stage`
198+
Then STDERR should contain:
199+
"""
200+
Profiling an admin URL requires --context=admin.
201+
"""
202+
And the return code should be 1

src/Profiler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function run() {
8282
$url = WP_CLI::get_runner()->config['url'];
8383
$this->is_admin_request = ! empty( $url ) && (bool) preg_match( '#/wp-admin(/|$)#i', $url );
8484

85+
if ( $this->is_admin_request && 'admin' !== WP_CLI::get_runner()->config['context'] ) {
86+
WP_CLI::error( 'Profiling an admin URL requires --context=admin.' );
87+
}
88+
8589
WP_CLI::add_wp_hook(
8690
'muplugins_loaded',
8791
function () {

0 commit comments

Comments
 (0)