Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions bin/run-behat-tests
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ if [ "${WP_VERSION-latest}" = "latest" ]; then
export WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current")
fi

# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release, not X.Y.0).
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
if [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
export WP_VERSION="${BASH_REMATCH[1]}"
elif [[ "${WP_VERSION}" =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_VERSIONS_JSON=$(curl -s https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json)
if [ -n "${WP_VERSIONS_JSON}" ]; then
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For a slight performance improvement and to avoid creating an extra process for echo, you can use a here-string (<<<) to pass the JSON content to jq. This is generally a more efficient way to feed a string to a command's standard input in Bash.

Suggested change
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
RESOLVED_VERSION=$(jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty' <<< "${WP_VERSIONS_JSON}")

if [ -n "${RESOLVED_VERSION}" ]; then
export WP_VERSION="${RESOLVED_VERSION}"
fi
fi
fi

# To retrieve the WP-CLI tests package root folder, we start with this scripts
# location.
SOURCE="${BASH_SOURCE[0]}"
Expand Down
Loading