Skip to content

Commit e844d35

Browse files
committed
Address review feedback on the Behat runner
Two points from review: * The wp-versions artifact already marks the current release with a "latest" status, so the separate request to api.wordpress.org was redundant. Both the "latest" and the X.Y resolution now come out of that one file, which means one cached artifact and at most one network request per run instead of two. * Behat's step definition snippets are not only printed when writing new step definitions; they are also how a typo in an existing step surfaces. That makes them a diagnostic rather than noise, and they only appear when something is already wrong, so suppressing them under WP_CLI_TEST_QUIET saved nothing in the passing case and cost information in the failing one. Dropped, which leaves WP_CLI_TEST_QUIET with no effect on Behat. Also adds lint-gherkin to the setup instructions, which listed the scripts a consuming package should wire up but not the new one. 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 203f0ca commit e844d35

3 files changed

Lines changed: 76 additions & 77 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ 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
"phpunit": "run-php-unit-tests",
1718
"prepare-tests": "install-package-tests",
1819
"test": [
1920
"@lint",
21+
"@lint-gherkin",
2022
"@phpcs",
2123
"@phpunit",
2224
"@behat"
@@ -137,7 +139,7 @@ this package's `package.json`, which exists only to hold that pin.
137139
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
138140
139141
- `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.
140-
- `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.
142+
- `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. Behat's own output is already minimal, so it is unaffected.
141143
142144
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
143145
@@ -167,11 +169,12 @@ Here's how to run your tests against the latest trunk version of WordPress:
167169
WP_VERSION=trunk composer behat
168170
```
169171
170-
Resolving `latest`, or a `X.Y` version without a patch number, needs a network
171-
request. The answer is cached in the system temp directory for a day, so that
172-
repeated runs do not repeat the lookup, and so that a run without connectivity
173-
falls back to the last known answer. `WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the
174-
lifetime of that cache in seconds; `0` looks the version up every time.
172+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
173+
WordPress versions data, which is fetched once and cached in the system temp
174+
directory for a day. Repeated runs do not repeat the request, and a run without
175+
connectivity falls back to the last known copy.
176+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
177+
`0` fetches it every time.
175178
176179
#### WordPress Archive
177180

‎README.md‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ 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
"phpunit": "run-php-unit-tests",
2829
"prepare-tests": "install-package-tests",
2930
"test": [
3031
"@lint",
32+
"@lint-gherkin",
3133
"@phpcs",
3234
"@phpunit",
3335
"@behat"
@@ -148,7 +150,7 @@ this package's `package.json`, which exists only to hold that pin.
148150
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
149151
150152
- `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.
151-
- `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.
153+
- `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. Behat's own output is already minimal, so it is unaffected.
152154
153155
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
154156
@@ -178,11 +180,12 @@ Here's how to run your tests against the latest trunk version of WordPress:
178180
WP_VERSION=trunk composer behat
179181
```
180182
181-
Resolving `latest`, or a `X.Y` version without a patch number, needs a network
182-
request. The answer is cached in the system temp directory for a day, so that
183-
repeated runs do not repeat the lookup, and so that a run without connectivity
184-
falls back to the last known answer. `WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the
185-
lifetime of that cache in seconds; `0` looks the version up every time.
183+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
184+
WordPress versions data, which is fetched once and cached in the system temp
185+
directory for a day. Repeated runs do not repeat the request, and a run without
186+
connectivity falls back to the last known copy.
187+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
188+
`0` fetches it every time.
186189
187190
#### WordPress Archive
188191

‎bin/run-behat-tests‎

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

100-
# Resolving WP_VERSION costs up to two network round trips on every invocation.
101-
# Cache the answers, so that re-running a single scenario while iterating does
102-
# not repeat them, and so that a run without connectivity can fall back to the
103-
# last known answer instead of ending up with no version at all.
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.
104105
#
105106
# Set WP_CLI_TEST_WP_VERSION_CACHE_TTL to 0 to always refetch.
106-
WP_VERSION_CACHE_DIR="${TMPDIR:-/tmp}/wp-cli-test-wp-version-cache"
107-
WP_VERSION_CACHE_TTL="${WP_CLI_TEST_WP_VERSION_CACHE_TTL:-86400}"
108-
109-
# Print a cache entry if it exists and is younger than the given number of
110-
# seconds. A negative TTL accepts an entry of any age.
111-
read_version_cache() {
112-
local cache_file="${WP_VERSION_CACHE_DIR}/$1"
113-
local ttl="$2"
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+
# Print the cached versions file if it is younger than the given number of
112+
# seconds. A negative TTL accepts it at any age.
113+
read_versions_cache() {
114+
local ttl="$1"
114115
local age
115116

116-
[ -s "${cache_file}" ] || return 1
117+
[ -s "${WP_VERSIONS_CACHE_FILE}" ] || return 1
117118

118119
if [ "${ttl}" -ge 0 ]; then
119120
# PHP rather than `find -newermt`, which is not portable across
120121
# GNU and BSD userlands. The Behat runner needs PHP anyway.
121-
age=$(php -r 'echo time() - filemtime( $argv[1] );' "${cache_file}" 2>/dev/null)
122+
age=$(php -r 'echo time() - filemtime( $argv[1] );' "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null)
122123
case ${age} in
123124
''|*[!0-9]*) return 1;;
124125
esac
125126
[ "${age}" -lt "${ttl}" ] || return 1
126127
fi
127128

128-
cat "${cache_file}"
129+
cat "${WP_VERSIONS_CACHE_FILE}"
129130
}
130131

131-
write_version_cache() {
132-
mkdir -p "${WP_VERSION_CACHE_DIR}" 2>/dev/null || return 0
133-
printf '%s' "$2" > "${WP_VERSION_CACHE_DIR}/$1" 2>/dev/null || true
134-
}
135-
136-
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
137-
if [ "${WP_VERSION-latest}" = "latest" ]; then
138-
LATEST_WP_VERSION=$(read_version_cache latest "${WP_VERSION_CACHE_TTL}")
132+
# Print the WordPress versions data, from the cache where possible. Warnings go
133+
# to STDERR so that they cannot end up inside the returned JSON.
134+
get_wp_versions() {
135+
local json
139136

140-
if [ -z "${LATEST_WP_VERSION}" ]; then
141-
LATEST_WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current // empty")
137+
json=$( read_versions_cache "${WP_VERSIONS_CACHE_TTL}" )
138+
if [ -n "${json}" ]; then
139+
printf '%s' "${json}"
140+
return 0
141+
fi
142142

143-
if [ -n "${LATEST_WP_VERSION}" ]; then
144-
write_version_cache latest "${LATEST_WP_VERSION}"
145-
else
146-
# Prefer a stale answer over no answer.
147-
LATEST_WP_VERSION=$(read_version_cache latest -1)
143+
json=$( curl -s "${WP_VERSIONS_URL}" )
148144

149-
if [ -n "${LATEST_WP_VERSION}" ]; then
150-
echo "Warning: Could not reach api.wordpress.org, falling back to the cached latest WordPress version ${LATEST_WP_VERSION}."
151-
else
152-
echo "Warning: Could not determine the latest WordPress version. Version-specific tags will not be filtered."
153-
fi
154-
fi
145+
# Only cache a well-formed response; an error page is not one.
146+
if echo "${json}" | jq -e 'type == "object" and length > 0' > /dev/null 2>&1; then
147+
mkdir -p "$( dirname "${WP_VERSIONS_CACHE_FILE}" )" 2>/dev/null \
148+
&& printf '%s' "${json}" > "${WP_VERSIONS_CACHE_FILE}" 2>/dev/null || true
149+
printf '%s' "${json}"
150+
return 0
155151
fi
156152

157-
export WP_VERSION="${LATEST_WP_VERSION}"
158-
fi
153+
# Prefer a stale answer over no answer.
154+
json=$( read_versions_cache -1 )
155+
if [ -n "${json}" ]; then
156+
echo "Warning: Could not fetch the WordPress versions data, falling back to the cached copy." >&2
157+
printf '%s' "${json}"
158+
return 0
159+
fi
159160

160-
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release, not X.Y.0).
161-
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
162-
if [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
163-
export WP_VERSION="${BASH_REMATCH[1]}"
164-
elif [[ "${WP_VERSION}" =~ ^[0-9]+\.[0-9]+$ ]]; then
165-
WP_VERSIONS_JSON=$(read_version_cache wp-versions.json "${WP_VERSION_CACHE_TTL}")
161+
return 1
162+
}
166163

167-
if [ -z "${WP_VERSIONS_JSON}" ]; then
168-
WP_VERSIONS_JSON=$(curl -s https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json)
164+
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
165+
if [ "${WP_VERSION-latest}" = "latest" ]; then
166+
WP_VERSION=$( get_wp_versions | jq -r 'to_entries | map( select( .value == "latest" ) ) | last | .key // empty' )
169167

170-
# Only cache a well-formed response; a proxy error page is not one.
171-
if echo "${WP_VERSIONS_JSON}" | jq -e 'type == "object"' > /dev/null 2>&1; then
172-
write_version_cache wp-versions.json "${WP_VERSIONS_JSON}"
173-
else
174-
WP_VERSIONS_JSON=$(read_version_cache wp-versions.json -1)
175-
fi
168+
if [ -z "${WP_VERSION}" ]; then
169+
echo "Warning: Could not determine the latest WordPress version. Version-specific tags will not be filtered."
176170
fi
177171

178-
if [ -n "${WP_VERSIONS_JSON}" ]; then
179-
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
180-
if [ -n "${RESOLVED_VERSION}" ]; then
181-
export WP_VERSION="${RESOLVED_VERSION}"
182-
fi
172+
export WP_VERSION
173+
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release,
174+
# not X.Y.0). This asks for that specific release, so it must not fall through to
175+
# the patch resolution below.
176+
elif [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
177+
export WP_VERSION="${BASH_REMATCH[1]}"
178+
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
179+
elif [[ "${WP_VERSION}" =~ ^[0-9]+\.[0-9]+$ ]]; then
180+
RESOLVED_VERSION=$( get_wp_versions | jq -r --arg prefix "${WP_VERSION}." 'keys | map( select( startswith( $prefix ) ) ) | sort_by( split(".") | map( tonumber ) ) | last // empty' )
181+
182+
if [ -n "${RESOLVED_VERSION}" ]; then
183+
export WP_VERSION="${RESOLVED_VERSION}"
183184
fi
184185
fi
185186

@@ -213,14 +214,6 @@ if [ -n "${NO_COLOR}" ]; then
213214
BEHAT_EXTRA_ARGS+=('--no-colors')
214215
fi
215216

216-
# Drop the step definition snippets that Behat prints for undefined steps. They
217-
# are long, and they are only actionable when you are writing new step
218-
# definitions in this package. Useful for CI logs and for AI coding agents,
219-
# which pay for every token of the report they read back.
220-
if [ -n "${WP_CLI_TEST_QUIET}" ]; then
221-
BEHAT_EXTRA_ARGS+=('--no-snippets')
222-
fi
223-
224217
# Run the functional tests.
225218
FORMAT_ARGS=(--format progress)
226219
for arg in "$@"; do

0 commit comments

Comments
 (0)