Skip to content

Commit 37faeda

Browse files
committed
Make the test runners quieter for non-interactive consumers
Adds two opt-in environment variables to the runner scripts, both unset by default so existing output is unchanged: * NO_COLOR (https://no-color.org/) stops the runners from forcing ANSI color codes on. parallel-lint and PHPUnit forced them unconditionally, which meant escape sequences in every captured log. * WP_CLI_TEST_QUIET switches the reporters to their most compact form: PHP_CodeSniffer to one line per violation with no progress ticker, PHPStan to one line per error with no progress bar and no result table, and Behat to omitting step definition snippets. Also documents narrowing a Behat run to a single scenario, --stop-on-failure and composer behat-rerun. Refs wp-cli/wp-cli#6161 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VcGmbu6CYzGGhP3jXuWDkJ
1 parent ff95ded commit 37faeda

7 files changed

Lines changed: 128 additions & 6 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,39 @@ composer behat -- features/cli-info.feature
104104

105105
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
106106

107+
The same mechanism works for narrowing a run down further, or for bailing out early:
108+
```bash
109+
# A single scenario, identified by the line it starts on.
110+
composer behat -- features/cli-info.feature:12
111+
112+
# Every scenario carrying a given tag.
113+
composer behat -- --tags=@require-wp-5.0
114+
115+
# Stop at the first failing scenario instead of running the whole suite.
116+
composer behat -- --stop-on-failure
117+
118+
# Re-run only the scenarios that failed the last time.
119+
composer behat-rerun
120+
```
121+
122+
### Controlling the amount of output
123+
124+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
125+
126+
- `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) stops the runners from forcing ANSI color codes on, and leaves the decision to each tool's own terminal detection. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
127+
- `WP_CLI_TEST_QUIET` switches the reporters to their most compact form: PHP_CodeSniffer reports one `file:line:col` line per violation with no progress ticker, PHPStan reports one `file:line:message` line per error with no progress bar and no result table, and Behat stops printing step definition snippets for undefined steps.
128+
129+
```bash
130+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
131+
```
132+
133+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
134+
135+
```bash
136+
export NO_COLOR=1
137+
export WP_CLI_TEST_QUIET=1
138+
```
139+
107140
### Controlling the test environment
108141

109142
#### WordPress Version

‎README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,39 @@ composer behat -- features/cli-info.feature
115115

116116
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
117117

118+
The same mechanism works for narrowing a run down further, or for bailing out early:
119+
```bash
120+
# A single scenario, identified by the line it starts on.
121+
composer behat -- features/cli-info.feature:12
122+
123+
# Every scenario carrying a given tag.
124+
composer behat -- --tags=@require-wp-5.0
125+
126+
# Stop at the first failing scenario instead of running the whole suite.
127+
composer behat -- --stop-on-failure
128+
129+
# Re-run only the scenarios that failed the last time.
130+
composer behat-rerun
131+
```
132+
133+
### Controlling the amount of output
134+
135+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
136+
137+
- `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) stops the runners from forcing ANSI color codes on, and leaves the decision to each tool's own terminal detection. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
138+
- `WP_CLI_TEST_QUIET` switches the reporters to their most compact form: PHP_CodeSniffer reports one `file:line:col` line per violation with no progress ticker, PHPStan reports one `file:line:message` line per error with no progress bar and no result table, and Behat stops printing step definition snippets for undefined steps.
139+
140+
```bash
141+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
142+
```
143+
144+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
145+
146+
```bash
147+
export NO_COLOR=1
148+
export WP_CLI_TEST_QUIET=1
149+
```
150+
118151
### Controlling the test environment
119152

120153
#### WordPress Version

‎bin/run-behat-tests‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ if [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]] && vendor/bin/behat --help 2>/dev/n
141141
BEHAT_EXTRA_ARGS+=('--xdebug')
142142
fi
143143

144+
# Honor the NO_COLOR convention (https://no-color.org/).
145+
if [ -n "${NO_COLOR}" ]; then
146+
BEHAT_EXTRA_ARGS+=('--no-colors')
147+
fi
148+
149+
# Drop the step definition snippets that Behat prints for undefined steps. They
150+
# are long, and they are only actionable when you are writing new step
151+
# definitions in this package. Useful for CI logs and for AI coding agents,
152+
# which pay for every token of the report they read back.
153+
if [ -n "${WP_CLI_TEST_QUIET}" ]; then
154+
BEHAT_EXTRA_ARGS+=('--no-snippets')
155+
fi
156+
144157
# Run the functional tests.
145158
FORMAT_ARGS=(--format progress)
146159
for arg in "$@"; do

‎bin/run-linter-tests‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
#!/bin/sh
22

3-
vendor/bin/parallel-lint -j 10 --colors --exclude vendor . "$@"
3+
# Honor the NO_COLOR convention (https://no-color.org/) by leaving the color
4+
# decision to the tool's own TTY detection instead of forcing it.
5+
COLOR_ARGS="--colors"
6+
if [ -n "${NO_COLOR}" ]; then
7+
COLOR_ARGS=""
8+
fi
9+
10+
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
11+
vendor/bin/parallel-lint -j 10 $COLOR_ARGS --exclude vendor . "$@"

‎bin/run-php-unit-tests‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ then
1010
EXTRA_ARGS="--display-warnings --fail-on-warning --display-notices --fail-on-notice --display-deprecations --fail-on-deprecation"
1111
fi
1212

13+
# Honor the NO_COLOR convention (https://no-color.org/). This has to be an
14+
# explicit "never" rather than an omitted flag, because a project's
15+
# phpunit.xml may well turn colors on by itself.
16+
COLOR_ARGS="--color=always"
17+
if [ -n "${NO_COLOR}" ]; then
18+
COLOR_ARGS="--color=never"
19+
fi
20+
1321
if [ -f "./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php" ]; then
14-
vendor/bin/phpunit --color=always "$@" $EXTRA_ARGS --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php
22+
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
23+
vendor/bin/phpunit $COLOR_ARGS "$@" $EXTRA_ARGS --bootstrap ./vendor/wp-cli/wp-cli-tests/tests/bootstrap.php
1524
else
16-
vendor/bin/phpunit --color=always "$@" $EXTRA_ARGS
25+
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
26+
vendor/bin/phpunit $COLOR_ARGS "$@" $EXTRA_ARGS
1727
fi
1828
fi

‎bin/run-phpcs-tests‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@
33
# Run the code style check only if a configuration file exists.
44
if [ -f ".phpcs.xml" ] || [ -f "phpcs.xml" ] || [ -f ".phpcs.xml.dist" ] || [ -f "phpcs.xml.dist" ]
55
then
6-
vendor/bin/phpcs "$@"
6+
EXTRA_ARGS=""
7+
8+
# Compact, one-line-per-violation output without the progress ticker.
9+
# Useful for CI logs and for AI coding agents, which pay for every token of
10+
# the report they read back.
11+
if [ -n "${WP_CLI_TEST_QUIET}" ]; then
12+
EXTRA_ARGS="-q --report=emacs"
13+
fi
14+
15+
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
16+
vendor/bin/phpcs $EXTRA_ARGS "$@"
717
fi

‎bin/run-phpstan-tests‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
#!/bin/sh
22

3-
# Run the code style check only if a configuration file exists.
3+
# Run the static analysis only if a configuration file exists.
44
if [ -f "phpstan.dist.neon" ] || [ -f "phpstan.neon.dist" ] || [ -f "phpstan.neon" ]
55
then
6-
vendor/bin/phpstan --memory-limit=2048M analyse "$@"
6+
EXTRA_ARGS=""
7+
8+
# Honor the NO_COLOR convention (https://no-color.org/).
9+
if [ -n "${NO_COLOR}" ]; then
10+
EXTRA_ARGS="--no-ansi"
11+
fi
12+
13+
# Replace the redrawing progress bar and the box-drawing result table with
14+
# plain `file:line:message` lines. Useful for CI logs and for AI coding
15+
# agents, which pay for every token of the report they read back.
16+
if [ -n "${WP_CLI_TEST_QUIET}" ]; then
17+
EXTRA_ARGS="${EXTRA_ARGS} --no-progress --error-format=raw"
18+
fi
19+
20+
# shellcheck disable=SC2086 # Intentional word splitting of the optional arguments.
21+
vendor/bin/phpstan --memory-limit=2048M analyse $EXTRA_ARGS "$@"
722
fi

0 commit comments

Comments
 (0)