diff --git a/.readme-partials/USING.md b/.readme-partials/USING.md index 38d523381..252ba6b28 100644 --- a/.readme-partials/USING.md +++ b/.readme-partials/USING.md @@ -276,7 +276,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat | `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. | | `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. | -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. +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. 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. @@ -426,6 +426,24 @@ chmod +x ~/wp-cli/wp WP_CLI_BIN_DIR=~/wp-cli composer behat ``` +#### Xdebug Step Debugging + +Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3: + +```bash +WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12 +``` + +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. + +The launched processes get the following environment variables: + +* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage. +* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already. +* `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. + +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. + ### Setting up the tests in Travis CI Basic rules for setting up the test framework with Travis CI: diff --git a/README.md b/README.md index c75adaa57..bcf13dc90 100644 --- a/README.md +++ b/README.md @@ -287,7 +287,7 @@ WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat | `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. | | `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. | -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. +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. 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. @@ -437,6 +437,24 @@ chmod +x ~/wp-cli/wp WP_CLI_BIN_DIR=~/wp-cli composer behat ``` +#### Xdebug Step Debugging + +Set `WP_CLI_TEST_XDEBUG` to step debug the WP-CLI and WordPress code that the scenarios run, with Xdebug 3: + +```bash +WP_CLI_TEST_XDEBUG=true composer behat -- features/cli-info.feature:12 +``` + +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. + +The launched processes get the following environment variables: + +* `XDEBUG_MODE`: `debug`, added to the modes set through `XDEBUG_MODE` or `xdebug.mode`, and to `coverage` when collecting code coverage. +* `XDEBUG_SESSION=1`, unless `XDEBUG_SESSION` is set already. +* `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. + +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. + ### Setting up the tests in Travis CI Basic rules for setting up the test framework with Travis CI: diff --git a/bin/run-behat-tests b/bin/run-behat-tests index ab068ce41..d011a770e 100755 --- a/bin/run-behat-tests +++ b/bin/run-behat-tests @@ -60,6 +60,8 @@ if [ -n "${DOCKER}" ]; then without_docker "Docker is not available." elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then without_docker "Code coverage cannot be collected in Docker containers." + elif [[ "${WP_CLI_TEST_XDEBUG}" == "true" || "${WP_CLI_TEST_XDEBUG}" == "1" ]]; then + without_docker "Step debugging is not available in Docker containers." fi fi diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 4b84f4841..02da1cf19 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -308,6 +308,15 @@ private static function running_with_code_coverage() { return \in_array( $with_code_coverage, [ 'true', '1' ], true ); } + /** + * Whether the launched WP-CLI processes should connect to the Xdebug step debugger. + */ + private static function running_with_xdebug(): bool { + $with_xdebug = (string) getenv( 'WP_CLI_TEST_XDEBUG' ); + + return \in_array( $with_xdebug, [ 'true', '1' ], true ); + } + public static function merge_coverage_reports(): void { if ( ! self::running_with_code_coverage() ) { return; @@ -547,7 +556,7 @@ private static function get_process_env_variables(): array { } $env = array_filter( - array_merge( $env, self::get_php_tuning_env_variables() ), + array_merge( $env, self::get_php_tuning_env_variables(), self::get_xdebug_env_variables() ), static function ( $value ) { return null !== $value; } @@ -602,6 +611,46 @@ static function ( $value ) { return $env; } + /** + * Get the environment variables that make the launched PHP processes start a step debugging session. + * + * The debug mode is added to the modes already requested through XDEBUG_MODE or php.ini, + * and to the coverage mode when code coverage is collected. XDEBUG_SESSION is only set when not + * set already. Unless XDEBUG_CONFIG sets a log level, it is lowered so that Xdebug does not + * print a warning on STDERR, which scenarios check, when no debugging client is listening. + * + * @return array + */ + private static function get_xdebug_env_variables(): array { + if ( ! self::running_with_xdebug() ) { + return []; + } + + $mode = getenv( 'XDEBUG_MODE' ); + if ( false === $mode ) { + $mode = (string) ini_get( 'xdebug.mode' ); + } + + $modes = array_diff( array_map( 'trim', explode( ',', $mode ) ), [ '', 'off' ] ); + $modes[] = 'debug'; + if ( self::running_with_code_coverage() ) { + $modes[] = 'coverage'; + } + + $env = [ 'XDEBUG_MODE' => implode( ',', array_unique( $modes ) ) ]; + + if ( false === getenv( 'XDEBUG_SESSION' ) ) { + $env['XDEBUG_SESSION'] = '1'; + } + + $config = (string) getenv( 'XDEBUG_CONFIG' ); + if ( false === strpos( $config, 'log_level=' ) ) { + $env['XDEBUG_CONFIG'] = ltrim( "{$config} log_level=0" ); + } + + return $env; + } + /** * Get the environment variables that make the PHP processes launched by the tests cheaper. * diff --git a/tests/tests/TestXdebugEnv.php b/tests/tests/TestXdebugEnv.php new file mode 100644 index 000000000..d94ef5ff3 --- /dev/null +++ b/tests/tests/TestXdebugEnv.php @@ -0,0 +1,114 @@ + + */ + private $original_env = []; + + protected function set_up(): void { + parent::set_up(); + + foreach ( self::VARIABLES as $name ) { + $this->original_env[ $name ] = getenv( $name ); + putenv( $name ); + } + } + + protected function tear_down(): void { + foreach ( $this->original_env as $name => $value ) { + putenv( false === $value ? $name : "{$name}={$value}" ); + } + + parent::tear_down(); + } + + /** + * @return array + */ + private function get_xdebug_env_variables(): array { + $get_variables = Closure::bind( + static function (): array { + return FeatureContext::get_xdebug_env_variables(); + }, + null, + FeatureContext::class + ); + + return $get_variables(); + } + + public function test_nothing_is_set_without_the_variable(): void { + putenv( 'WP_CLI_TEST_XDEBUG=false' ); + + $this->assertSame( [], $this->get_xdebug_env_variables() ); + } + + public function test_defaults(): void { + putenv( 'WP_CLI_TEST_XDEBUG=1' ); + putenv( 'XDEBUG_MODE=' ); + + $this->assertSame( + [ + 'XDEBUG_MODE' => 'debug', + 'XDEBUG_SESSION' => '1', + 'XDEBUG_CONFIG' => 'log_level=0', + ], + $this->get_xdebug_env_variables() + ); + } + + public function test_debug_mode_is_added_to_the_ini_setting(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + + $modes = explode( ',', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + + $this->assertContains( 'debug', $modes ); + $this->assertNotContains( 'off', $modes ); + } + + public function test_debug_mode_is_added_to_the_requested_modes(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_MODE=develop, off,trace' ); + + $this->assertSame( 'develop,trace,debug', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + } + + public function test_coverage_mode_is_kept_when_collecting_code_coverage(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'WP_CLI_TEST_COVERAGE=true' ); + putenv( 'XDEBUG_MODE=debug' ); + + $this->assertSame( 'debug,coverage', $this->get_xdebug_env_variables()['XDEBUG_MODE'] ); + } + + public function test_session_is_not_overridden(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_SESSION=PHPSTORM' ); + + $this->assertArrayNotHasKey( 'XDEBUG_SESSION', $this->get_xdebug_env_variables() ); + } + + public function test_log_level_is_added_to_the_config(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_CONFIG=client_host=192.168.1.2' ); + + $this->assertSame( 'client_host=192.168.1.2 log_level=0', $this->get_xdebug_env_variables()['XDEBUG_CONFIG'] ); + } + + public function test_config_with_a_log_level_is_not_overridden(): void { + putenv( 'WP_CLI_TEST_XDEBUG=true' ); + putenv( 'XDEBUG_CONFIG=log_level=7' ); + + $this->assertArrayNotHasKey( 'XDEBUG_CONFIG', $this->get_xdebug_env_variables() ); + } +}