-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathruncommand.feature
More file actions
408 lines (358 loc) · 10.9 KB
/
runcommand.feature
File metadata and controls
408 lines (358 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
Feature: Run a WP-CLI command
Background:
Given an empty directory
And a command.php file:
"""
<?php
/**
* Run a WP-CLI command with WP_CLI::runcommand();
*
* ## OPTIONS
*
* <command>
* : Command to run, quoted.
*
* [--launch]
* : Launch a new process for the command.
*
* [--exit_error]
* : Exit on error.
*
* [--return[=<return>]]
* : Capture and return output.
*
* [--parse=<format>]
* : Parse returned output as a particular format.
*/
WP_CLI::add_command( 'run', function( $args, $assoc_args ){
$ret = WP_CLI::runcommand( $args[0], $assoc_args );
$ret = is_object( $ret ) ? (array) $ret : $ret;
WP_CLI::log( 'returned: ' . var_export( $ret, true ) );
});
"""
And a wp-cli.yml file:
"""
user: admin
require:
- command.php
"""
And a config.yml file:
"""
user get:
0: admin
field: user_email
"""
Scenario Outline: Run a WP-CLI command and render output
Given a WP installation
When I run `wp <flag> run 'option get home'`
Then STDOUT should be:
"""
https://example.com
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> run 'eval "echo wp_get_current_user()->user_login . PHP_EOL;"'`
Then STDOUT should be:
"""
admin
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
When I run `WP_CLI_CONFIG_PATH=config.yml wp <flag> run 'user get'`
Then STDOUT should be:
"""
admin@example.com
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Run a WP-CLI command and capture output
Given a WP installation
When I run `wp run <flag> --return 'option get home'`
Then STDOUT should be:
"""
returned: 'https://example.com'
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> --return run 'eval "echo wp_get_current_user()->user_login . PHP_EOL;"'`
Then STDOUT should be:
"""
returned: 'admin'
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> --return=stderr run 'eval "echo wp_get_current_user()->user_login . PHP_EOL;"'`
Then STDOUT should be:
"""
returned: ''
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> --return=return_code run 'eval "echo wp_get_current_user()->user_login . PHP_EOL;"'`
Then STDOUT should be:
"""
returned: 0
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> --return=all run 'eval "echo wp_get_current_user()->user_login . PHP_EOL;"'`
Then STDOUT should be:
"""
returned: array (
'stdout' => 'admin',
'stderr' => '',
'return_code' => 0,
)
"""
And STDERR should be empty
And the return code should be 0
When I run `WP_CLI_CONFIG_PATH=config.yml wp --return <flag> run 'user get'`
Then STDOUT should be:
"""
returned: 'admin@example.com'
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Use 'parse=json' to parse JSON output
Given a WP installation
When I run `wp run --return --parse=json <flag> 'user get admin --fields=user_login,user_email --format=json'`
Then STDOUT should be:
"""
returned: array (
'user_login' => 'admin',
'user_email' => 'admin@example.com',
)
"""
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Exit on error by default
Given a WP installation
When I try `wp run <flag> 'eval "WP_CLI::error( var_export( get_current_user_id(), true ) );"'`
Then STDOUT should be empty
And STDERR should be:
"""
Error: 1
"""
And the return code should be 1
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Override erroring on exit
Given a WP installation
When I try `wp run <flag> --no-exit_error --return=all 'eval "WP_CLI::error( var_export( get_current_user_id(), true ) );"'`
Then STDOUT should be:
"""
returned: array (
'stdout' => '',
'stderr' => 'Error: 1',
'return_code' => 1,
)
"""
And STDERR should be empty
And the return code should be 0
When I run `wp <flag> --no-exit_error run 'option pluck foo$bar barfoo'`
Then STDOUT should be:
"""
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Output using echo and log, success, warning and error
Given a WP installation
# Note WP_CLI::error() terminates eval processing so needs to be last.
When I run `wp run <flag> --no-exit_error --return=all 'eval "WP_CLI::log( '\'log\'' ); echo '\'echo\''; WP_CLI::success( '\'success\'' ); WP_CLI::error( '\'error\'' );"'`
Then STDOUT should be:
"""
returned: array (
'stdout' => 'log
echoSuccess: success',
'stderr' => 'Error: error',
'return_code' => 1,
)
"""
And STDERR should be empty
And the return code should be 0
When I run `wp run <flag> --no-exit_error --return=all 'eval "echo '\'echo\''; WP_CLI::log( '\'log\'' ); WP_CLI::warning( '\'warning\''); WP_CLI::success( '\'success\'' );"'`
Then STDOUT should be:
"""
returned: array (
'stdout' => 'echolog
Success: success',
'stderr' => 'Warning: warning',
'return_code' => 0,
)
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
@less-than-php-8
Scenario Outline: Installed packages work as expected
Given a WP installation
# Allow for composer/ca-bundle using `openssl_x509_parse()` which throws PHP warnings on old versions of PHP.
When I try `wp package install wp-cli/scaffold-package-command`
And I run `wp <flag> run 'help scaffold package'`
Then STDOUT should contain:
"""
wp scaffold package <name>
"""
And STDERR should be empty
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Persists global parameters when supplied interactively
Given a WP installation in 'foo'
When I run `wp <flag> --path=foo run 'config set test 42 --type=constant'`
Then STDOUT should be:
"""
Success: Added the constant 'test' to the 'wp-config.php' file with the value '42'.
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
Scenario: Persists alias when launching a new process via runcommand
Given a WP installation in 'foo'
And a wp-cli.yml file:
"""
@foo:
path: foo
user: admin
require:
- command.php
"""
When I run `wp @foo --launch --return run 'option get home'`
Then STDOUT should be:
"""
returned: 'https://example.com'
"""
And STDERR should be empty
And the return code should be 0
Scenario Outline: Apply backwards compat conversions
Given a WP installation
When I run `wp <flag> run 'term url category 1'`
Then STDOUT should be:
"""
https://example.com/?cat=1
returned: NULL
"""
And STDERR should be empty
And the return code should be 0
Examples:
| flag |
| --no-launch |
| --launch |
Scenario Outline: Check that proc_open() and proc_close() aren't disabled for launch
Given a WP installation
When I try `{INVOKE_WP_CLI_WITH_PHP_ARGS--ddisable_functions=<func>} --launch run 'option get home'`
Then STDERR should contain:
"""
Error: Cannot do 'launch option': The PHP functions `proc_open()` and/or `proc_close()` are disabled
"""
And the return code should be 1
Examples:
| func |
| proc_open |
| proc_close |
Scenario: Check that command_args provided to runcommand are used in command
Given a WP installation
And a custom-cmd.php file:
"""
<?php
class Custom_Command extends WP_CLI_Command {
/**
* Custom command to test passing command_args via runcommand options
*
* @when after_wp_load
*/
public function echo_test( $args ) {
$cli_opts = array( 'command_args' => array( '--exec="echo \'test\' . PHP_EOL;"' ) );
WP_CLI::runcommand( 'option get home', $cli_opts);
}
public function bad_path( $args ) {
$cli_opts = array( 'command_args' => array('--path=/bad/path' ) );
WP_CLI::runcommand( 'option get home', $cli_opts);
}
}
WP_CLI::add_command( 'custom-command', 'Custom_Command' );
"""
When I run `wp --require=custom-cmd.php custom-command echo_test`
Then STDOUT should be:
"""
test
https://example.com
"""
When I try `wp --require=custom-cmd.php custom-command bad_path`
Then STDERR should contain:
"""
The used path is: /bad/path/
"""
Scenario: Check that required files are used from command arguments and ENV VAR
Given a WP installation
And a custom-cmd.php file:
"""
<?php
class Custom_Command extends WP_CLI_Command {
/**
* Custom command to test passing command_args via runcommand options
*
* @when after_wp_load
*/
public function echo_test( $args ) {
echo "test" . PHP_EOL;
}
}
WP_CLI::add_command( 'custom-command', 'Custom_Command' );
"""
And a env.php file:
"""
<?php
echo 'ENVIRONMENT REQUIRE' . PHP_EOL;
"""
And a env-2.php file:
"""
<?php
echo 'ENVIRONMENT REQUIRE 2' . PHP_EOL;
"""
When I run `WP_CLI_REQUIRE=env.php wp eval 'return null;' --skip-wordpress`
Then STDOUT should be:
"""
ENVIRONMENT REQUIRE
"""
When I run `WP_CLI_REQUIRE=env.php wp --require=custom-cmd.php custom-command echo_test`
Then STDOUT should be:
"""
ENVIRONMENT REQUIRE
test
"""
When I run `WP_CLI_REQUIRE='env.php,env-2.php' wp --require=custom-cmd.php custom-command echo_test`
Then STDOUT should be:
"""
ENVIRONMENT REQUIRE
ENVIRONMENT REQUIRE 2
test
"""