Skip to content

Commit f38e2e6

Browse files
committed
Cache the WP_VERSION lookups performed by the Behat runner
Every `composer behat` invocation resolved WP_VERSION over the network: one request to api.wordpress.org for `latest`, and a second one to the wp-versions artifact when the version has no patch number. That cost applies equally to a full suite run and to re-running one scenario for the fifth time while iterating on a fix. The answers now go into a cache in the system temp directory with a configurable lifetime, defaulting to a day. Two side effects worth noting: * A run without connectivity falls back to the last known answer rather than continuing with an empty WP_VERSION, which silently disabled the filtering of version-specific tags. * When there is nothing to fall back to, that case is now reported instead of being silent. 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 37faeda commit f38e2e6

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ Here's how to run your tests against the latest trunk version of WordPress:
152152
WP_VERSION=trunk composer behat
153153
```
154154
155+
Resolving `latest`, or a `X.Y` version without a patch number, needs a network
156+
request. The answer is cached in the system temp directory for a day, so that
157+
repeated runs do not repeat the lookup, and so that a run without connectivity
158+
falls back to the last known answer. `WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the
159+
lifetime of that cache in seconds; `0` looks the version up every time.
160+
155161
#### WordPress Archive
156162
157163
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ Here's how to run your tests against the latest trunk version of WordPress:
163163
WP_VERSION=trunk composer behat
164164
```
165165
166+
Resolving `latest`, or a `X.Y` version without a patch number, needs a network
167+
request. The answer is cached in the system temp directory for a day, so that
168+
repeated runs do not repeat the lookup, and so that a run without connectivity
169+
falls back to the last known answer. `WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the
170+
lifetime of that cache in seconds; `0` looks the version up every time.
171+
166172
#### WordPress Archive
167173
168174
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary

‎bin/run-behat-tests‎

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,84 @@ 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.
104+
#
105+
# 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"
114+
local age
115+
116+
[ -s "${cache_file}" ] || return 1
117+
118+
if [ "${ttl}" -ge 0 ]; then
119+
# PHP rather than `find -newermt`, which is not portable across
120+
# 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+
case ${age} in
123+
''|*[!0-9]*) return 1;;
124+
esac
125+
[ "${age}" -lt "${ttl}" ] || return 1
126+
fi
127+
128+
cat "${cache_file}"
129+
}
130+
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+
100136
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
101137
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")
138+
LATEST_WP_VERSION=$(read_version_cache latest "${WP_VERSION_CACHE_TTL}")
139+
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")
142+
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)
148+
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
155+
fi
156+
157+
export WP_VERSION="${LATEST_WP_VERSION}"
103158
fi
104159

105160
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release, not X.Y.0).
106161
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
107162
if [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
108163
export WP_VERSION="${BASH_REMATCH[1]}"
109164
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)
165+
WP_VERSIONS_JSON=$(read_version_cache wp-versions.json "${WP_VERSION_CACHE_TTL}")
166+
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)
169+
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
176+
fi
177+
111178
if [ -n "${WP_VERSIONS_JSON}" ]; then
112179
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
113180
if [ -n "${RESOLVED_VERSION}" ]; then

0 commit comments

Comments
 (0)