Skip to content

Commit 7efac11

Browse files
swissspidyclaude
andcommitted
Keep coverage and user Xdebug settings when step debugging
Add the debug mode to the modes set through XDEBUG_MODE or php.ini instead of replacing them, and keep the coverage mode when code coverage is collected. Append the lowered log level to a user's XDEBUG_CONFIG rather than leaving it out, as the warnings Xdebug prints when no IDE is listening make scenarios fail, and drop the IDE key, which only matters for DBGp proxies and overrode the one set in php.ini. Don't use Docker when step debugging, as the containers would neither receive the variable nor reach the IDE. Move the documentation into the README partial and add unit tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 6837402 commit 7efac11

4 files changed

Lines changed: 144 additions & 11 deletions

File tree

‎.readme-partials/USING.md‎

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

279-
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.
279+
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.
280280

281281
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.
282282

@@ -426,6 +426,24 @@ chmod +x ~/wp-cli/wp
426426
WP_CLI_BIN_DIR=~/wp-cli composer behat
427427
```
428428

429+
#### Xdebug Step Debugging
430+
431+
Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3:
432+
433+
```bash
434+
WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12
435+
```
436+
437+
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.
438+
439+
The launched processes get the following environment variables:
440+
441+
* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage.
442+
* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already.
443+
* `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.
444+
445+
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.
446+
429447
### Setting up the tests in Travis CI
430448

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

‎README.md‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
287287
| `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. |
288288
| `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. |
289289

290-
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.
290+
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.
291291

292292
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.
293293

@@ -439,20 +439,21 @@ WP_CLI_BIN_DIR=~/wp-cli composer behat
439439

440440
#### Xdebug Step Debugging
441441

442-
You can enable Xdebug step debugging for Behat tests by setting the `WP_CLI_TEST_XDEBUG` environment variable.
443-
444-
This is useful when you want to debug the actual WP-CLI + WordPress code that is being run during the tests.
442+
Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3:
445443

446444
```bash
447-
WP_CLI_TEST_XDEBUG=true composer behat
445+
WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12
448446
```
449447

450-
This will set the following Xdebug environment variables for all WP-CLI processes spawned by the tests:
451-
- `XDEBUG_MODE=debug`
452-
- `XDEBUG_SESSION=1`
453-
- `XDEBUG_CONFIG="idekey=WP_CLI_TEST_XDEBUG log_level=0"`
448+
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.
449+
450+
The launched processes get the following environment variables:
451+
452+
* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage.
453+
* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already.
454+
* `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.
454455

455-
Note: You need to have Xdebug installed and your IDE configured to listen for debug connections.
456+
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.
456457

457458
### Setting up the tests in Travis CI
458459

‎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

‎tests/tests/TestXdebugEnv.php‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace WP_CLI\Tests\Tests;
4+
5+
use ReflectionMethod;
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+
$method = new ReflectionMethod( FeatureContext::class, 'get_xdebug_env_variables' );
40+
if ( PHP_VERSION_ID < 80100 ) {
41+
$method->setAccessible( true );
42+
}
43+
44+
/** @var array<string, string> */
45+
return $method->invoke( null );
46+
}
47+
48+
public function test_nothing_is_set_without_the_variable(): void {
49+
putenv( 'WP_CLI_TEST_XDEBUG=false' );
50+
51+
$this->assertSame( [], $this->get_xdebug_env_variables() );
52+
}
53+
54+
public function test_defaults(): void {
55+
putenv( 'WP_CLI_TEST_XDEBUG=1' );
56+
putenv( 'XDEBUG_MODE=' );
57+
58+
$this->assertSame(
59+
[
60+
'XDEBUG_MODE' => 'debug',
61+
'XDEBUG_SESSION' => '1',
62+
'XDEBUG_CONFIG' => 'log_level=0',
63+
],
64+
$this->get_xdebug_env_variables()
65+
);
66+
}
67+
68+
public function test_debug_mode_is_added_to_the_ini_setting(): void {
69+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
70+
71+
$modes = explode( ',', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
72+
73+
$this->assertContains( 'debug', $modes );
74+
$this->assertNotContains( 'off', $modes );
75+
}
76+
77+
public function test_debug_mode_is_added_to_the_requested_modes(): void {
78+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
79+
putenv( 'XDEBUG_MODE=develop, off,trace' );
80+
81+
$this->assertSame( 'develop,trace,debug', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
82+
}
83+
84+
public function test_coverage_mode_is_kept_when_collecting_code_coverage(): void {
85+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
86+
putenv( 'WP_CLI_TEST_COVERAGE=true' );
87+
putenv( 'XDEBUG_MODE=debug' );
88+
89+
$this->assertSame( 'debug,coverage', $this->get_xdebug_env_variables()['XDEBUG_MODE'] );
90+
}
91+
92+
public function test_session_is_not_overridden(): void {
93+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
94+
putenv( 'XDEBUG_SESSION=PHPSTORM' );
95+
96+
$this->assertArrayNotHasKey( 'XDEBUG_SESSION', $this->get_xdebug_env_variables() );
97+
}
98+
99+
public function test_log_level_is_added_to_the_config(): void {
100+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
101+
putenv( 'XDEBUG_CONFIG=client_host=192.168.1.2' );
102+
103+
$this->assertSame( 'client_host=192.168.1.2 log_level=0', $this->get_xdebug_env_variables()['XDEBUG_CONFIG'] );
104+
}
105+
106+
public function test_config_with_a_log_level_is_not_overridden(): void {
107+
putenv( 'WP_CLI_TEST_XDEBUG=true' );
108+
putenv( 'XDEBUG_CONFIG=log_level=7' );
109+
110+
$this->assertArrayNotHasKey( 'XDEBUG_CONFIG', $this->get_xdebug_env_variables() );
111+
}
112+
}

0 commit comments

Comments
 (0)