Skip to content

Commit 0cc3fd1

Browse files
swissspidyclaude
andauthored
Make the test runners cheaper to run repeatedly and quieter to read back (#356)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent f1b77ec commit 0cc3fd1

11 files changed

Lines changed: 444 additions & 16 deletions

‎.gherkin-lintrc‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"file-name": [
3+
"on",
4+
{
5+
"style": "kebab-case"
6+
}
7+
],
8+
"indentation": [
9+
"on",
10+
{
11+
"Feature": 0,
12+
"Background": 2,
13+
"Scenario": 2,
14+
"Examples": 4,
15+
"Step": 4,
16+
"given": 4,
17+
"example": 6,
18+
"and": 4
19+
}
20+
],
21+
"no-dupe-feature-names": "on",
22+
"no-dupe-scenario-names": "off",
23+
"no-empty-file": "on",
24+
"no-files-without-scenarios": "on",
25+
"no-multiple-empty-lines": "off",
26+
"no-partially-commented-tag-lines": "on",
27+
"no-trailing-spaces": "off",
28+
"no-unnamed-features": "on",
29+
"no-unnamed-scenarios": "on",
30+
"no-scenario-outlines-without-examples": "on",
31+
"use-and": "on"
32+
}

‎.readme-partials/USING.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ To make use of the WP-CLI testing framework, you need to complete the following
1111
"behat": "run-behat-tests",
1212
"behat-rerun": "rerun-behat-tests",
1313
"lint": "run-linter-tests",
14+
"lint-gherkin": "run-gherkin-lint-tests",
1415
"phpcs": "run-phpcs-tests",
1516
"phpcbf": "run-phpcbf-cleanup",
1617
"phpstan": "run-phpstan-tests",
1718
"phpunit": "run-php-unit-tests",
1819
"prepare-tests": "install-package-tests",
1920
"test": [
2021
"@lint",
22+
"@lint-gherkin",
2123
"@phpcs",
2224
"@phpstan",
2325
"@phpunit",
@@ -96,6 +98,7 @@ You can use the following commands to control the tests:
9698
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
9799
* `composer test` - Run all test suites.
98100
* `composer lint` - Run only the linting test suite.
101+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
99102
* `composer phpcs` - Run only the code sniffer test suite.
100103
* `composer phpcbf` - Run only the code sniffer cleanup.
101104
* `composer phpstan` - Run only the static analysis.
@@ -226,6 +229,53 @@ composer behat -- features/cli-info.feature
226229
227230
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
228231
232+
The same mechanism works for narrowing a run down further, or for bailing out early:
233+
```bash
234+
# A single scenario, identified by the line it starts on.
235+
composer behat -- features/cli-info.feature:12
236+
237+
# Every scenario carrying a given tag.
238+
composer behat -- --tags=@require-wp-5.0
239+
240+
# Stop at the first failing scenario instead of running the whole suite.
241+
composer behat -- --stop-on-failure
242+
243+
# Re-run only the scenarios that failed the last time.
244+
composer behat-rerun
245+
```
246+
247+
### Linting the feature files
248+
249+
`composer lint-gherkin` checks `features/` with
250+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
251+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
252+
different rules can override it by committing its own `.gherkin-lintrc`.
253+
254+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
255+
later. Where `npx` is not available the check reports that it is skipping, rather
256+
than failing a suite that is otherwise entirely PHP. Its version is pinned in
257+
this package's `package.json`, which exists only to hold that pin.
258+
259+
### Controlling the amount of output
260+
261+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
262+
263+
* `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) turns off the ANSI color codes in the output of every runner. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
264+
* `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. This covers the analysis of the PHP files themselves; the checks over the PHP blocks embedded in feature files keep their own reports, which are rewritten to point back at the feature file a block came from. Behat's own output is already minimal, so it is unaffected.
265+
266+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
267+
268+
```bash
269+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
270+
```
271+
272+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
273+
274+
```bash
275+
export NO_COLOR=1
276+
export WP_CLI_TEST_QUIET=1
277+
```
278+
229279
### Controlling the test environment
230280

231281
#### WordPress Version
@@ -241,6 +291,13 @@ Here's how to run your tests against the latest trunk version of WordPress:
241291
WP_VERSION=trunk composer behat
242292
```
243293
294+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
295+
WordPress versions data, which is fetched once and cached in the system temp
296+
directory for a day. Repeated runs do not repeat the request, and a run without
297+
connectivity falls back to the last known copy.
298+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
299+
`0` fetches it every time.
300+
244301
#### WordPress Archive
245302
246303
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary

‎README.md‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ To make use of the WP-CLI testing framework, you need to complete the following
2222
"behat": "run-behat-tests",
2323
"behat-rerun": "rerun-behat-tests",
2424
"lint": "run-linter-tests",
25+
"lint-gherkin": "run-gherkin-lint-tests",
2526
"phpcs": "run-phpcs-tests",
2627
"phpcbf": "run-phpcbf-cleanup",
2728
"phpstan": "run-phpstan-tests",
2829
"phpunit": "run-php-unit-tests",
2930
"prepare-tests": "install-package-tests",
3031
"test": [
3132
"@lint",
33+
"@lint-gherkin",
3234
"@phpcs",
3335
"@phpstan",
3436
"@phpunit",
@@ -107,6 +109,7 @@ You can use the following commands to control the tests:
107109
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
108110
* `composer test` - Run all test suites.
109111
* `composer lint` - Run only the linting test suite.
112+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
110113
* `composer phpcs` - Run only the code sniffer test suite.
111114
* `composer phpcbf` - Run only the code sniffer cleanup.
112115
* `composer phpstan` - Run only the static analysis.
@@ -237,6 +240,53 @@ composer behat -- features/cli-info.feature
237240
238241
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
239242
243+
The same mechanism works for narrowing a run down further, or for bailing out early:
244+
```bash
245+
# A single scenario, identified by the line it starts on.
246+
composer behat -- features/cli-info.feature:12
247+
248+
# Every scenario carrying a given tag.
249+
composer behat -- --tags=@require-wp-5.0
250+
251+
# Stop at the first failing scenario instead of running the whole suite.
252+
composer behat -- --stop-on-failure
253+
254+
# Re-run only the scenarios that failed the last time.
255+
composer behat-rerun
256+
```
257+
258+
### Linting the feature files
259+
260+
`composer lint-gherkin` checks `features/` with
261+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
262+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
263+
different rules can override it by committing its own `.gherkin-lintrc`.
264+
265+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
266+
later. Where `npx` is not available the check reports that it is skipping, rather
267+
than failing a suite that is otherwise entirely PHP. Its version is pinned in
268+
this package's `package.json`, which exists only to hold that pin.
269+
270+
### Controlling the amount of output
271+
272+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
273+
274+
* `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) turns off the ANSI color codes in the output of every runner. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
275+
* `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. This covers the analysis of the PHP files themselves; the checks over the PHP blocks embedded in feature files keep their own reports, which are rewritten to point back at the feature file a block came from. Behat's own output is already minimal, so it is unaffected.
276+
277+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
278+
279+
```bash
280+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
281+
```
282+
283+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
284+
285+
```bash
286+
export NO_COLOR=1
287+
export WP_CLI_TEST_QUIET=1
288+
```
289+
240290
### Controlling the test environment
241291

242292
#### WordPress Version
@@ -252,6 +302,13 @@ Here's how to run your tests against the latest trunk version of WordPress:
252302
WP_VERSION=trunk composer behat
253303
```
254304
305+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
306+
WordPress versions data, which is fetched once and cached in the system temp
307+
directory for a day. Repeated runs do not repeat the request, and a run without
308+
connectivity falls back to the last known copy.
309+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
310+
`0` fetches it every time.
311+
255312
#### WordPress Archive
256313
257314
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary

‎bin/run-behat-tests‎

Lines changed: 131 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,137 @@ if [ -n "${WP_CLI_TEST_CORE_ZIP-}" ] && [ -z "${WP_VERSION-}" ]; then
9797
export WP_VERSION=trunk
9898
fi
9999

100+
# Everything WP_VERSION resolution needs is in one file: the wp-versions artifact
101+
# maps every WordPress release to its status, with the current one marked
102+
# "latest". Cache it, so that re-running a single scenario while iterating does
103+
# not refetch it every time, and so that a run without connectivity can fall back
104+
# to the last known answer instead of ending up with no version at all.
105+
#
106+
# Set WP_CLI_TEST_WP_VERSION_CACHE_TTL to 0 to always refetch.
107+
WP_VERSIONS_URL="https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json"
108+
WP_VERSIONS_CACHE_FILE="${TMPDIR:-/tmp}/wp-cli-test-wp-version-cache/wp-versions.json"
109+
WP_VERSIONS_CACHE_TTL="${WP_CLI_TEST_WP_VERSION_CACHE_TTL:-86400}"
110+
111+
# The versions data is a non-empty object mapping every release to its status.
112+
# An error page, or a copy that was truncated on its way to disk, is not one.
113+
is_valid_versions_json() {
114+
jq -e 'type == "object" and length > 0' > /dev/null 2>&1
115+
}
116+
117+
# Print the cached versions file if it is younger than the given number of
118+
# seconds. A negative TTL accepts it at any age.
119+
read_versions_cache() {
120+
local ttl="$1"
121+
local age
122+
123+
[ -s "${WP_VERSIONS_CACHE_FILE}" ] || return 1
124+
125+
if [ "${ttl}" -ge 0 ]; then
126+
# PHP rather than `find -newermt`, which is not portable across
127+
# GNU and BSD userlands. The Behat runner needs PHP anyway.
128+
age=$(php -r 'echo time() - filemtime( $argv[1] );' "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null)
129+
case ${age} in
130+
''|*[!0-9]*) return 1;;
131+
esac
132+
[ "${age}" -lt "${ttl}" ] || return 1
133+
fi
134+
135+
# Held to the same standard as a fetched copy, so that an unusable cache
136+
# counts as a miss and the network gets a chance to replace it, instead of
137+
# being served unchecked for the rest of its lifetime.
138+
is_valid_versions_json < "${WP_VERSIONS_CACHE_FILE}" || return 1
139+
140+
cat "${WP_VERSIONS_CACHE_FILE}"
141+
}
142+
143+
# Store the versions data for the next run. The write goes through a temporary
144+
# file in the same directory, so that a concurrent runner reading the cache sees
145+
# either the previous copy or the new one, never a partial write. Caching is
146+
# best effort throughout: a temp directory that cannot be written to is not a
147+
# reason to fail the run.
148+
write_versions_cache() {
149+
local dir
150+
local tmp
151+
152+
dir=$( dirname "${WP_VERSIONS_CACHE_FILE}" )
153+
mkdir -p "${dir}" 2>/dev/null || return 0
154+
155+
tmp=$( mktemp "${dir}/wp-versions.XXXXXX" 2>/dev/null ) || return 0
156+
157+
if printf '%s' "$1" > "${tmp}" 2>/dev/null; then
158+
# mktemp creates the file private to its owner; the cache directory is
159+
# shared, and the data in it is public.
160+
chmod 644 "${tmp}" 2>/dev/null
161+
mv -f "${tmp}" "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null || rm -f "${tmp}"
162+
else
163+
rm -f "${tmp}"
164+
fi
165+
166+
return 0
167+
}
168+
169+
# Print the WordPress versions data, from the cache where possible. Warnings go
170+
# to STDERR so that they cannot end up inside the returned JSON.
171+
get_wp_versions() {
172+
local json
173+
local ttl="${WP_VERSIONS_CACHE_TTL}"
174+
175+
# A typo must not turn into a cache that never expires: the age comparison
176+
# errors out on a non-numeric TTL, which would then accept the cached copy
177+
# at any age.
178+
if ! is_numeric "${ttl}"; then
179+
echo "Warning: WP_CLI_TEST_WP_VERSION_CACHE_TTL is not a number of seconds, falling back to 86400." >&2
180+
ttl=86400
181+
fi
182+
183+
json=$( read_versions_cache "${ttl}" )
184+
if [ -n "${json}" ]; then
185+
printf '%s' "${json}"
186+
return 0
187+
fi
188+
189+
# Bounded, so that an unreachable or unresponsive host falls back to the
190+
# cached copy instead of holding up the run indefinitely.
191+
json=$( curl -s --connect-timeout 10 --max-time 30 "${WP_VERSIONS_URL}" )
192+
193+
# Only cache a well-formed response; an error page is not one.
194+
if printf '%s' "${json}" | is_valid_versions_json; then
195+
write_versions_cache "${json}"
196+
printf '%s' "${json}"
197+
return 0
198+
fi
199+
200+
# Prefer a stale answer over no answer.
201+
json=$( read_versions_cache -1 )
202+
if [ -n "${json}" ]; then
203+
echo "Warning: Could not fetch the WordPress versions data, falling back to the cached copy." >&2
204+
printf '%s' "${json}"
205+
return 0
206+
fi
207+
208+
return 1
209+
}
210+
100211
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
101212
if [ "${WP_VERSION-latest}" = "latest" ]; then
102-
export WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current")
103-
fi
213+
WP_VERSION=$( get_wp_versions | jq -r 'to_entries | map( select( .value == "latest" ) ) | last | .key // empty' )
104214

105-
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release, not X.Y.0).
106-
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
107-
if [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
215+
if [ -z "${WP_VERSION}" ]; then
216+
echo "Warning: Could not determine the latest WordPress version. Version-specific tags will not be filtered." >&2
217+
fi
218+
219+
export WP_VERSION
220+
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release,
221+
# not X.Y.0). This asks for that specific release, so it must not fall through to
222+
# the patch resolution below.
223+
elif [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
108224
export WP_VERSION="${BASH_REMATCH[1]}"
225+
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
109226
elif [[ "${WP_VERSION}" =~ ^[0-9]+\.[0-9]+$ ]]; then
110-
WP_VERSIONS_JSON=$(curl -s https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json)
111-
if [ -n "${WP_VERSIONS_JSON}" ]; then
112-
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
113-
if [ -n "${RESOLVED_VERSION}" ]; then
114-
export WP_VERSION="${RESOLVED_VERSION}"
115-
fi
227+
RESOLVED_VERSION=$( get_wp_versions | jq -r --arg prefix "${WP_VERSION}." 'keys | map( select( startswith( $prefix ) ) ) | sort_by( split(".") | map( tonumber ) ) | last // empty' )
228+
229+
if [ -n "${RESOLVED_VERSION}" ]; then
230+
export WP_VERSION="${RESOLVED_VERSION}"
116231
fi
117232
fi
118233

@@ -141,6 +256,11 @@ if [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]] && vendor/bin/behat --help 2>/dev/n
141256
BEHAT_EXTRA_ARGS+=('--xdebug')
142257
fi
143258

259+
# Honor the NO_COLOR convention (https://no-color.org/).
260+
if [ -n "${NO_COLOR}" ]; then
261+
BEHAT_EXTRA_ARGS+=('--no-colors')
262+
fi
263+
144264
# Run the functional tests.
145265
FORMAT_ARGS=(--format progress)
146266
for arg in "$@"; do

0 commit comments

Comments
 (0)