Skip to content

Commit 6057bfa

Browse files
Copilotswissspidy
andauthored
Add WP_CLI_TEST_XDEBUG environment variable for step debugging in Behat tests (#289)
Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 9c36668 commit 6057bfa

5 files changed

Lines changed: 204 additions & 3 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
310310
| `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. |
311311
| `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. |
312312

313-
Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
313+
Docker is not used on Windows, when collecting code coverage, when step debugging, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
314314

315315
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
316316

@@ -460,6 +460,24 @@ chmod +x ~/wp-cli/wp
460460
WP_CLI_BIN_DIR=~/wp-cli composer behat
461461
```
462462

463+
#### Xdebug Step Debugging
464+
465+
Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3:
466+
467+
```bash
468+
WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12
469+
```
470+
471+
Every WP-CLI process launched by the tests then starts a debugging session, which your IDE needs to listen for. The test runner itself is not debugged; set `XDEBUG_MODE` and `XDEBUG_SESSION` for that.
472+
473+
The launched processes get the following environment variables:
474+
475+
* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage.
476+
* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already.
477+
* `XDEBUG_CONFIG`: `log_level=0`, added to the settings in `XDEBUG_CONFIG` unless they set a log level, so that Xdebug does not print warnings into the output the scenarios check when no IDE is listening.
478+
479+
As every process connects to the debugger, run a single scenario or feature file rather than the whole suite, and without parallel workers. Docker is not used when step debugging.
480+
463481
### Setting up the tests in Travis CI
464482

465483
Basic rules for setting up the test framework with Travis CI:

‎README.md‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
321321
| `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. |
322322
| `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. |
323323

324-
Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
324+
Docker is not used on Windows, when collecting code coverage, when step debugging, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
325325

326326
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
327327

@@ -471,6 +471,24 @@ chmod +x ~/wp-cli/wp
471471
WP_CLI_BIN_DIR=~/wp-cli composer behat
472472
```
473473

474+
#### Xdebug Step Debugging
475+
476+
Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3:
477+
478+
```bash
479+
WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12
480+
```
481+
482+
Every WP-CLI process launched by the tests then starts a debugging session, which your IDE needs to listen for. The test runner itself is not debugged; set `XDEBUG_MODE` and `XDEBUG_SESSION` for that.
483+
484+
The launched processes get the following environment variables:
485+
486+
* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage.
487+
* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already.
488+
* `XDEBUG_CONFIG`: `log_level=0`, added to the settings in `XDEBUG_CONFIG` unless they set a log level, so that Xdebug does not print warnings into the output the scenarios check when no IDE is listening.
489+
490+
As every process connects to the debugger, run a single scenario or feature file rather than the whole suite, and without parallel workers. Docker is not used when step debugging.
491+
474492
### Setting up the tests in Travis CI
475493

476494
Basic rules for setting up the test framework with Travis CI:

‎bin/run-behat-tests‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ if [ -n "${DOCKER}" ]; then
6060
without_docker "Docker is not available."
6161
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
6262
without_docker "Code coverage cannot be collected in Docker containers."
63+
elif [[ "${WP_CLI_TEST_XDEBUG}" == "true" || "${WP_CLI_TEST_XDEBUG}" == "1" ]]; then
64+
without_docker "Step debugging is not available in Docker containers."
6365
fi
6466
fi
6567

‎src/Context/FeatureContext.php‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@ private static function running_with_code_coverage() {
305305
return \in_array( $with_code_coverage, [ 'true', '1' ], true );
306306
}
307307

308+
/**
309+
* Whether the launched WP-CLI processes should connect to the Xdebug step debugger.
310+
*/
311+
private static function running_with_xdebug(): bool {
312+
$with_xdebug = (string) getenv( 'WP_CLI_TEST_XDEBUG' );
313+
314+
return \in_array( $with_xdebug, [ 'true', '1' ], true );
315+
}
316+
308317
public static function merge_coverage_reports(): void {
309318
if ( ! self::running_with_code_coverage() ) {
310319
return;
@@ -544,7 +553,7 @@ private static function get_process_env_variables(): array {
544553
}
545554

546555
$env = array_filter(
547-
array_merge( $env, self::get_php_tuning_env_variables() ),
556+
array_merge( $env, self::get_php_tuning_env_variables(), self::get_xdebug_env_variables() ),
548557
static function ( $value ) {
549558
return null !== $value;
550559
}
@@ -599,6 +608,46 @@ static function ( $value ) {
599608
return $env;
600609
}
601610

611+
/**
612+
* Get the environment variables that make the launched PHP processes start a step debugging session.
613+
*
614+
* The debug mode is added to the modes already requested through XDEBUG_MODE or php.ini,
615+
* and to the coverage mode when code coverage is collected. XDEBUG_SESSION is only set when not
616+
* set already. Unless XDEBUG_CONFIG sets a log level, it is lowered so that Xdebug does not
617+
* print a warning on STDERR, which scenarios check, when no debugging client is listening.
618+
*
619+
* @return array<string, string>
620+
*/
621+
private static function get_xdebug_env_variables(): array {
622+
if ( ! self::running_with_xdebug() ) {
623+
return [];
624+
}
625+
626+
$mode = getenv( 'XDEBUG_MODE' );
627+
if ( false === $mode ) {
628+
$mode = (string) ini_get( 'xdebug.mode' );
629+
}
630+
631+
$modes = array_diff( array_map( 'trim', explode( ',', $mode ) ), [ '', 'off' ] );
632+
$modes[] = 'debug';
633+
if ( self::running_with_code_coverage() ) {
634+
$modes[] = 'coverage';
635+
}
636+
637+
$env = [ 'XDEBUG_MODE' => implode( ',', array_unique( $modes ) ) ];
638+
639+
if ( false === getenv( 'XDEBUG_SESSION' ) ) {
640+
$env['XDEBUG_SESSION'] = '1';
641+
}
642+
643+
$config = (string) getenv( 'XDEBUG_CONFIG' );
644+
if ( false === strpos( $config, 'log_level=' ) ) {
645+
$env['XDEBUG_CONFIG'] = ltrim( "{$config} log_level=0" );
646+
}
647+
648+
return $env;
649+
}
650+
602651
/**
603652
* Get the environment variables that make the PHP processes launched by the tests cheaper.
604653
*

‎tests/tests/TestXdebugEnv.php‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace WP_CLI\Tests\Tests;
4+
5+
use Closure;
6+
use WP_CLI\Tests\Context\FeatureContext;
7+
use WP_CLI\Tests\TestCase;
8+
9+
class TestXdebugEnv extends TestCase {
10+
11+
const VARIABLES = [ 'WP_CLI_TEST_XDEBUG', 'WP_CLI_TEST_COVERAGE', 'XDEBUG_MODE', 'XDEBUG_SESSION', 'XDEBUG_CONFIG' ];
12+
13+
/**
14+
* @var array<string, string|false>
15+
*/
16+
private $original_env = [];
17+
18+
protected function set_up(): void {
19+
parent::set_up();
20+
21+
foreach ( self::VARIABLES as $name ) {
22+
$this->original_env[ $name ] = getenv( $name );
23+
putenv( $name );
24+
}
25+
}
26+
27+
protected function tear_down(): void {
28+
foreach ( $this->original_env as $name => $value ) {
29+
putenv( false === $value ? $name : "{$name}={$value}" );
30+
}
31+
32+
parent::tear_down();
33+
}
34+
35+
/**
36+
* @return array<string, string>
37+
*/
38+
private function get_xdebug_env_variables(): array {
39+
$get_variables = Closure::bind(
40+
static function (): array {
41+
return FeatureContext::get_xdebug_env_variables();
42+
},
43+
null,
44+
FeatureContext::class
45+
);
46+
47+
return $get_variables();
48+
}
49+
50+
public function test_nothing_is_set_without_the_variable(): void {
51+
putenv( 'WP_CLI_TEST_XDEBUG=false' );
52+
53+
$this->assertSame( [], $this->get_xdebug_env_variables() );
54+
}
55+
56+
public function test_defaults(): void {
57+
putenv( 'WP_CLI_TEST_XDEBUG=1' );
58+
putenv( 'XDEBUG_MODE=' );
59+
60+
$this->assertSame(
61+
[
62+
'XDEBUG_MODE' => 'debug',
63+
'XDEBUG_SESSION' => '1',
64+
'XDEBUG_CONFIG' => 'log_level=0',
65+
],
66+
$this->get_xdebug_env_variables()
67+
);
68+
}
69+
70+
public function test_debug_mode_is_added_to_the_ini_setting(): void {
71+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
72+
73+
$modes = explode( ',', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
74+
75+
$this->assertContains( 'debug', $modes );
76+
$this->assertNotContains( 'off', $modes );
77+
}
78+
79+
public function test_debug_mode_is_added_to_the_requested_modes(): void {
80+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
81+
putenv( 'XDEBUG_MODE=develop, off,trace' );
82+
83+
$this->assertSame( 'develop,trace,debug', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
84+
}
85+
86+
public function test_coverage_mode_is_kept_when_collecting_code_coverage(): void {
87+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
88+
putenv( 'WP_CLI_TEST_COVERAGE=true' );
89+
putenv( 'XDEBUG_MODE=debug' );
90+
91+
$this->assertSame( 'debug,coverage', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
92+
}
93+
94+
public function test_session_is_not_overridden(): void {
95+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
96+
putenv( 'XDEBUG_SESSION=PHPSTORM' );
97+
98+
$this->assertArrayNotHasKey( 'XDEBUG_SESSION', $this->get_xdebug_env_variables() );
99+
}
100+
101+
public function test_log_level_is_added_to_the_config(): void {
102+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
103+
putenv( 'XDEBUG_CONFIG=client_host=192.168.1.2' );
104+
105+
$this->assertSame( 'client_host=192.168.1.2 log_level=0', $this->get_xdebug_env_variables()['XDEBUG_CONFIG'] );
106+
}
107+
108+
public function test_config_with_a_log_level_is_not_overridden(): void {
109+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
110+
putenv( 'XDEBUG_CONFIG=log_level=7' );
111+
112+
$this->assertArrayNotHasKey( 'XDEBUG_CONFIG', $this->get_xdebug_env_variables() );
113+
}
114+
}

0 commit comments

Comments
 (0)