Skip to content

Commit 2c13df0

Browse files
schlesseraclaude
andcommitted
Add a step to run several setup commands in a single process
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9298472 commit 2c13df0

3 files changed

Lines changed: 132 additions & 0 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,33 @@ Feature: Test that WP-CLI Behat steps work as expected
468468
When I launch in the background `wp eval 'sleep(2); echo "done";'`
469469
# Background process should not block, continuing immediately
470470

471+
@require-wp
472+
Scenario: Run setup commands in a single process
473+
Given a WP installation
474+
And these setup commands:
475+
| command | save |
476+
| wp eval 'echo wp_insert_post( [ "post_title" => "First", "post_status" => "publish" ] );' | POST_ID |
477+
| wp eval 'echo get_the_title( {POST_ID} );' | TITLE |
478+
| wp eval 'echo getmypid();' | FIRST |
479+
| wp eval 'echo getmypid();' | SECOND |
480+
| wp config set WP_CLI_TEST_CONSTANT "a value" | |
481+
482+
When I run `wp eval 'echo get_post_field( "post_title", {POST_ID} ) . "|{TITLE}";'`
483+
Then STDOUT should be:
484+
"""
485+
First|First
486+
"""
487+
488+
When I run `wp eval 'echo "{FIRST}" === "{SECOND}" ? "same process" : "different processes";'`
489+
Then STDOUT should be:
490+
"""
491+
same process
492+
"""
493+
And the wp-config.php file should contain:
494+
"""
495+
'WP_CLI_TEST_CONSTANT', 'a value'
496+
"""
497+
471498
@require-wp
472499
Scenario: Test custom wp-content directory
473500
Given a WP installation

‎src/Context/FeatureContext.php‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,76 @@ public function proc( $command, $assoc_args = [], $path = '' ): Process {
16761676
return Process::create( $command, $cwd, $env );
16771677
}
16781678

1679+
/**
1680+
* Run several WP-CLI commands one after the other in a single `wp` process.
1681+
*
1682+
* Each command has to start with `wp `, and has to succeed. A command can use the
1683+
* output saved by an earlier command in the same batch as a {VARIABLE}.
1684+
*
1685+
* @param array<array{0: string, 1: string}> $commands Pairs of command and the name of the variable to save its STDOUT into, or ''.
1686+
*/
1687+
public function run_commands_in_single_process( array $commands ): void {
1688+
$batch_variables = array_filter( array_column( $commands, 1 ) );
1689+
1690+
$rows = [];
1691+
foreach ( $commands as list( $command, $save ) ) {
1692+
// Variables saved within the batch are only known once the process runs.
1693+
$command = (string) preg_replace_callback(
1694+
'/\{([A-Z_][A-Z_0-9]*)\}/',
1695+
function ( $matches ) use ( $batch_variables ) {
1696+
return in_array( $matches[1], $batch_variables, true ) ? $matches[0] : $this->replace_variables( $matches[0] );
1697+
},
1698+
$command
1699+
);
1700+
1701+
if ( 0 !== strpos( $command, 'wp ' ) ) {
1702+
throw new RuntimeException( "Only WP-CLI commands can run in a single process, got: {$command}" );
1703+
}
1704+
1705+
$rows[] = [ substr( $command, 3 ), $save ];
1706+
}
1707+
1708+
$marker = 'WP_CLI_TEST_SAVED_VARIABLES:';
1709+
$code = '<?php
1710+
$wp_cli_test_saved = [];
1711+
foreach ( ' . var_export( $rows, true ) . ' as $wp_cli_test_row ) {
1712+
$wp_cli_test_command = strtr( $wp_cli_test_row[0], $wp_cli_test_saved );
1713+
$wp_cli_test_result = WP_CLI::runcommand( $wp_cli_test_command, [ "launch" => false, "return" => "all", "exit_error" => false ] );
1714+
if ( 0 !== $wp_cli_test_result->return_code ) {
1715+
fwrite( STDERR, "\$ wp {$wp_cli_test_command}\n{$wp_cli_test_result->stdout}\n{$wp_cli_test_result->stderr}\nexit status: {$wp_cli_test_result->return_code}\n" );
1716+
exit( 1 );
1717+
}
1718+
if ( "" !== $wp_cli_test_row[1] ) {
1719+
$wp_cli_test_saved[ "{" . $wp_cli_test_row[1] . "}" ] = trim( $wp_cli_test_result->stdout, "\n" );
1720+
}
1721+
}
1722+
echo "\n' . $marker . '" . json_encode( $wp_cli_test_saved ) . "\n";
1723+
';
1724+
1725+
$file = $this->variables['RUN_DIR'] . '/' . uniqid( 'wp-cli-test-commands-', true ) . '.php';
1726+
file_put_contents( $file, $code );
1727+
1728+
try {
1729+
$result = $this->proc( Utils\esc_cmd( 'wp eval-file %s', $file ) )->run();
1730+
} finally {
1731+
unlink( $file );
1732+
}
1733+
1734+
if ( 0 !== $result->return_code ) {
1735+
throw new RuntimeException( "Running the commands in a single process failed:\n{$result->stderr}\n{$result->stdout}" );
1736+
}
1737+
1738+
$position = strrpos( $result->stdout, $marker );
1739+
$saved = false !== $position ? json_decode( trim( substr( $result->stdout, $position + strlen( $marker ) ) ), true ) : null;
1740+
if ( ! is_array( $saved ) ) {
1741+
throw new RuntimeException( "Could not read the saved variables from the output:\n{$result->stdout}" );
1742+
}
1743+
1744+
foreach ( $saved as $placeholder => $value ) {
1745+
$this->variables[ trim( $placeholder, '{}' ) ] = $value;
1746+
}
1747+
}
1748+
16791749
/**
16801750
* Start a background process. Will automatically be closed when the tests finish.
16811751
*

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,41 @@ public function given_these_installed_and_active_plugins( $stream ): void {
430430
$this->proc( "wp plugin install $plugins --activate" )->run_check();
431431
}
432432

433+
/**
434+
* Runs several setup commands in a single process.
435+
*
436+
* Avoids loading WordPress once per command when a scenario needs several fixtures. The `command`
437+
* column holds the WP-CLI commands, which all have to succeed. The optional `save` column names a
438+
* variable to save the command's STDOUT into, which later commands in the same table can use.
439+
*
440+
* ```
441+
* Scenario: My example scenario
442+
* Given a WP installation
443+
* And these setup commands:
444+
* | command | save |
445+
* | wp post create --post_title="First post" --porcelain | POST_ID |
446+
* | wp post meta add {POST_ID} color blue | |
447+
* | wp term create category Fruit --porcelain | TERM_ID |
448+
* ```
449+
*
450+
* @access public
451+
*
452+
* @Given these setup commands:
453+
*
454+
* @param TableNode $table
455+
*/
456+
public function given_these_setup_commands( TableNode $table ): void {
457+
$commands = [];
458+
foreach ( $table->getHash() as $row ) {
459+
if ( ! isset( $row['command'] ) ) {
460+
throw new RuntimeException( 'The setup commands table needs a "command" column.' );
461+
}
462+
$commands[] = [ $row['command'], trim( $row['save'] ?? '', '{} ' ) ];
463+
}
464+
465+
$this->run_commands_in_single_process( $commands );
466+
}
467+
433468
/**
434469
* Configure a custom `wp-content` directory.
435470
*

0 commit comments

Comments
 (0)