Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2176805
Update install-wp-tests script
swissspidy Oct 6, 2025
f847213
remove svn usage
swissspidy Oct 6, 2025
a4e6950
more mariadb checks
swissspidy Oct 6, 2025
8746658
Add update check
swissspidy Oct 6, 2025
4f19c42
Add missing vars
swissspidy Oct 7, 2025
a05593f
Fix conditional check
swissspidy Oct 7, 2025
49792b7
Follow redirects
swissspidy Oct 7, 2025
2b5b25a
Update templates/install-wp-tests.sh
swissspidy Oct 7, 2025
7859375
Use built files
swissspidy Oct 7, 2025
634d3f6
use correct trunk branch
swissspidy Oct 7, 2025
428275b
Add some coloring and logging
swissspidy Oct 8, 2025
cd80f8b
No tput
swissspidy Oct 8, 2025
a961eca
Add back stmt
swissspidy Oct 8, 2025
dca43cc
Adjust test
swissspidy Oct 8, 2025
6f17adf
Update test
swissspidy Oct 8, 2025
0884b0a
Merge branch 'main' into try/322-refresh
swissspidy Jan 20, 2026
ff6dc07
Update templates/install-wp-tests.sh
swissspidy Jan 20, 2026
001c47c
Initial plan
Copilot Jan 20, 2026
8f7c499
Add validation for ref variable to prevent undefined usage
Copilot Jan 20, 2026
2495daf
Merge pull request #361 from wp-cli/copilot/sub-pr-356
swissspidy Jan 20, 2026
090cec3
Update templates/install-wp-tests.sh
swissspidy Jan 20, 2026
bf30dcc
Initial plan
Copilot Jan 20, 2026
4365778
Initial plan
Copilot Jan 20, 2026
7e63614
Replace 'which' with 'command -v' for better portability
Copilot Jan 20, 2026
51032da
Fix network failure handling in check_for_updates function
Copilot Jan 20, 2026
88e06b0
Merge pull request #362 from wp-cli/copilot/sub-pr-356
swissspidy Jan 20, 2026
c8af7e1
Merge branch 'try/322-refresh' into copilot/sub-pr-356-again
swissspidy Jan 20, 2026
9508d7f
Merge pull request #363 from wp-cli/copilot/sub-pr-356-again
swissspidy Jan 20, 2026
bbaf408
Initial plan
Copilot Jan 20, 2026
40c1d42
Merge pull request #364 from wp-cli/copilot/sub-pr-356
swissspidy Jan 20, 2026
aef6017
Initial plan
Copilot Jan 25, 2026
6049697
Add version padding for GitHub tag compatibility
Copilot Jan 25, 2026
b8622df
Merge pull request #366 from wp-cli/copilot/sub-pr-356
swissspidy Jan 25, 2026
903ad68
Merge branch 'main' into try/322-refresh
swissspidy Jan 25, 2026
b847772
Update templates/install-wp-tests.sh
swissspidy Jan 25, 2026
78c922f
Update templates/install-wp-tests.sh
swissspidy Jan 25, 2026
41b8fe0
Update templates/install-wp-tests.sh
swissspidy Jan 25, 2026
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
8 changes: 4 additions & 4 deletions features/install-wp-tests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Scaffold install-wp-tests.sh tests
When I try `/usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh`
Then STDOUT should contain:
"""
usage:
Usage:
"""
And the return code should be 1

Expand Down Expand Up @@ -119,7 +119,7 @@ Feature: Scaffold install-wp-tests.sh tests
"""
And STDOUT should contain:
"""
Recreated the database (wp_cli_test_scaffold)
Database (wp_cli_test_scaffold) recreated.
"""

When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest < negative-response`
Expand Down Expand Up @@ -250,7 +250,7 @@ Feature: Scaffold install-wp-tests.sh tests
"""
And STDOUT should contain:
"""
Recreated the database (wp_cli_test_scaffold)
Database (wp_cli_test_scaffold) recreated.
"""

When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest < negative-response`
Expand Down Expand Up @@ -369,7 +369,7 @@ Feature: Scaffold install-wp-tests.sh tests
"""
And STDOUT should contain:
"""
Recreated the database (wp_cli_test_scaffold)
Database (wp_cli_test_scaffold) recreated.
"""

When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest < negative-response`
Expand Down
194 changes: 157 additions & 37 deletions templates/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#!/usr/bin/env bash

# See https://raw.githubusercontent.com/wp-cli/scaffold-command/master/templates/install-wp-tests.sh

# Set up colors for output
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
CYAN="\033[0;36m"
RESET="\033[0m"

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
echo -e "${YELLOW}Usage:${RESET} $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
fi

Expand All @@ -15,31 +24,67 @@ SKIP_DB_CREATE=${6-false}
TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_TESTS_FILE="$WP_TESTS_DIR"/includes/functions.php
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
WP_CORE_FILE="$WP_CORE_DIR"/wp-settings.php

download() {
if [ `which curl` ]; then
curl -s "$1" > "$2";
elif [ `which wget` ]; then
if command -v curl > /dev/null 2>&1; then
curl -L -s "$1" > "$2";
return $?
elif command -v wget > /dev/null 2>&1; then
wget -nv -O "$2" "$1"
return $?
else
echo "Error: Neither curl nor wget is installed."
echo -e "${RED}Error: Neither curl nor wget is installed.${RESET}"
exit 1
fi
}

# Check if svn is installed
check_svn_installed() {
if ! command -v svn > /dev/null; then
echo "Error: svn is not installed. Please install svn and try again."
exit 1
fi
check_for_updates() {
local remote_url="https://raw.githubusercontent.com/wp-cli/scaffold-command/main/templates/install-wp-tests.sh"
local tmp_script="$TMPDIR/install-wp-tests.sh.latest"

if ! download "$remote_url" "$tmp_script"; then
echo -e "${YELLOW}Warning: Failed to download the latest version of the script for update check.${RESET}"
return
fi

if [ ! -f "$tmp_script" ] || [ ! -s "$tmp_script" ]; then
echo -e "${YELLOW}Warning: Downloaded script is missing or empty, cannot check for updates.${RESET}"
rm -f "$tmp_script"
return
fi

local local_hash=""
local remote_hash=""

if command -v shasum > /dev/null; then
local_hash=$(shasum -a 256 "$0" | awk '{print $1}')
remote_hash=$(shasum -a 256 "$tmp_script" | awk '{print $1}')
elif command -v sha256sum > /dev/null; then
local_hash=$(sha256sum "$0" | awk '{print $1}')
remote_hash=$(sha256sum "$tmp_script" | awk '{print $1}')
else
echo -e "${YELLOW}Warning: Could not find shasum or sha256sum to check for script updates.${RESET}"
rm "$tmp_script"
return
fi

rm "$tmp_script"

if [ "$local_hash" != "$remote_hash" ]; then
echo -e "${YELLOW}Warning: A newer version of this script is available at $remote_url${RESET}"
fi
}
# Allow disabling the update check by setting WP_INSTALL_TESTS_SKIP_UPDATE_CHECK=true in the environment.
if [ "${WP_INSTALL_TESTS_SKIP_UPDATE_CHECK:-false}" != "true" ]; then
check_for_updates
fi

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
WP_BRANCH=${WP_VERSION%\-*}
WP_TESTS_TAG="branches/$WP_BRANCH"

elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
Expand All @@ -54,30 +99,35 @@ elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
LATEST_VERSION=$(grep -oE '"version":"[^"]*' /tmp/wp-latest.json | head -n 1 | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
echo -e "${RED}Error: Latest WordPress version could not be found.${RESET}"
exit 1
fi
# The version-check endpoint returns major.minor (e.g., 6.9), but GitHub tags include the patch version (e.g., 6.9.0)
if [[ $LATEST_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
LATEST_VERSION="${LATEST_VERSION}.0"
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {

if [ -d $WP_CORE_DIR ]; then
if [ -f $WP_CORE_FILE ]; then
echo -e "${CYAN}WordPress is already installed.${RESET}"
return;
fi

echo -e "${CYAN}Installing WordPress...${RESET}"

rm -rf $WP_CORE_DIR
mkdir -p $WP_CORE_DIR

if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
mkdir -p $TMPDIR/wordpress-trunk
rm -rf $TMPDIR/wordpress-trunk/*
check_svn_installed
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
download https://github.com/WordPress/wordpress/archive/refs/heads/master.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
else
if [ $WP_VERSION == 'latest' ]; then
local ARCHIVE_NAME='latest'
Expand All @@ -103,8 +153,7 @@ install_wp() {
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
echo -e "${GREEN}WordPress installed successfully.${RESET}"
}

install_test_suite() {
Expand All @@ -115,18 +164,68 @@ install_test_suite() {
local ioption='-i'
fi

# set up testing suite if it doesn't yet exist
if [ ! -d $WP_TESTS_DIR ]; then
# set up testing suite if it doesn't yet exist or only partially exists
if [ ! -f $WP_TESTS_FILE ]; then
echo -e "${CYAN}Installing test suite...${RESET}"
# set up testing suite
rm -rf $WP_TESTS_DIR
mkdir -p $WP_TESTS_DIR
rm -rf $WP_TESTS_DIR/{includes,data}
check_svn_installed
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data

if [[ $WP_TESTS_TAG == 'trunk' ]]; then
ref=trunk
archive_url="https://github.com/WordPress/wordpress-develop/archive/refs/heads/${ref}.tar.gz"
elif [[ $WP_TESTS_TAG == branches/* ]]; then
ref=${WP_TESTS_TAG#branches/}
archive_url="https://github.com/WordPress/wordpress-develop/archive/refs/heads/${ref}.tar.gz"
else
ref=${WP_TESTS_TAG#tags/}
archive_url="https://github.com/WordPress/wordpress-develop/archive/refs/tags/${ref}.tar.gz"
fi

if [ -z "$ref" ]; then
echo -e "${RED}Error:${RESET} Unable to determine git reference from WP_TESTS_TAG: $WP_TESTS_TAG"
exit 1
fi

download "${archive_url}" "$TMPDIR/wordpress-develop.tar.gz"

# Validate that the tarball was downloaded correctly before extracting
if [ ! -s "$TMPDIR/wordpress-develop.tar.gz" ]; then
echo -e "${RED}Error:${RESET} Downloaded test suite archive is missing or empty: $TMPDIR/wordpress-develop.tar.gz"
exit 1
fi

if ! tar -tzf "$TMPDIR/wordpress-develop.tar.gz" >/dev/null 2>&1; then
echo -e "${RED}Error:${RESET} Downloaded test suite archive is not a valid tar.gz file: $TMPDIR/wordpress-develop.tar.gz"
exit 1
fi

tar -zxmf "$TMPDIR/wordpress-develop.tar.gz" -C "$TMPDIR"
mv "$TMPDIR/wordpress-develop-${ref}/tests/phpunit/includes" "$WP_TESTS_DIR"/
mv "$TMPDIR/wordpress-develop-${ref}/tests/phpunit/data" "$WP_TESTS_DIR"/
rm -rf "$TMPDIR/wordpress-develop-${ref}"
rm "$TMPDIR/wordpress-develop.tar.gz"
echo -e "${GREEN}Test suite installed.${RESET}"
else
echo -e "${CYAN}Test suite is already installed.${RESET}"
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
if [ ! -f "$WP_TESTS_DIR"/wp-tests-config.php ]; then
echo -e "${CYAN}Configuring test suite...${RESET}"
if [[ $WP_TESTS_TAG == 'trunk' ]]; then
ref=trunk
elif [[ $WP_TESTS_TAG == branches/* ]]; then
ref=${WP_TESTS_TAG#branches/}
else
ref=${WP_TESTS_TAG#tags/}
fi

if [ -z "$ref" ]; then
echo -e "${RED}Error:${RESET} Unable to determine git reference from WP_TESTS_TAG: $WP_TESTS_TAG"
exit 1
fi

download https://raw.githubusercontent.com/WordPress/wordpress-develop/${ref}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
Expand All @@ -135,6 +234,9 @@ install_test_suite() {
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
echo -e "${GREEN}Test suite configured.${RESET}"
else
echo -e "${CYAN}Test suite is already configured.${RESET}"
fi

}
Expand All @@ -143,22 +245,32 @@ recreate_db() {
shopt -s nocasematch
if [[ $1 =~ ^(y|yes)$ ]]
then
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
echo -e "${CYAN}Recreating the database ($DB_NAME)...${RESET}"
if command -v mariadb-admin > /dev/null 2>&1; then
mariadb-admin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
else
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi
create_db
echo "Recreated the database ($DB_NAME)."
echo -e "${GREEN}Database ($DB_NAME) recreated.${RESET}"
else
echo "Leaving the existing database ($DB_NAME) in place."
echo -e "${YELLOW}Leaving the existing database ($DB_NAME) in place.${RESET}"
fi
shopt -u nocasematch
}

create_db() {
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
if command -v mariadb-admin > /dev/null 2>&1; then
mariadb-admin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
else
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
fi
}

install_db() {

if [ ${SKIP_DB_CREATE} = "true" ]; then
echo -e "${YELLOW}Skipping database creation.${RESET}"
return 0
fi

Expand All @@ -179,16 +291,24 @@ install_db() {
fi

# create database
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
if command -v mariadb > /dev/null 2>&1; then
local DB_CLIENT='mariadb'
else
local DB_CLIENT='mysql'
fi
if $DB_CLIENT --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep -q "^$DB_NAME$";
then
echo "Reinstalling will delete the existing test database ($DB_NAME)"
echo -e "${YELLOW}Reinstalling will delete the existing test database ($DB_NAME)${RESET}"
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
recreate_db $DELETE_EXISTING_DB
else
echo -e "${CYAN}Creating database ($DB_NAME)...${RESET}"
create_db
echo -e "${GREEN}Database ($DB_NAME) created.${RESET}"
fi
}

install_wp
install_test_suite
install_db
echo -e "${GREEN}Done.${RESET}"