Skip to content

Commit 3e85a9b

Browse files
authored
Workflow Updates
1 parent 9350785 commit 3e85a9b

File tree

2 files changed

+138
-90
lines changed

2 files changed

+138
-90
lines changed

.github/workflows/wordpress-tests.yml

Lines changed: 64 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -78,84 +78,68 @@ jobs:
7878
- name: Install SVN and jq
7979
run: |
8080
sudo apt-get update
81-
sudo apt-get install -y subversion jq
81+
sudo apt-get install -y subversion jq bc
8282
83-
- name: Install Composer dependencies
84-
uses: ramsey/composer-install@v3
85-
with:
86-
composer-options: "--prefer-dist --no-progress"
87-
continue-on-error: true
88-
89-
- name: Verify Composer installation
83+
# Robust PHPUnit installation
84+
- name: Verify and Install PHPUnit
9085
run: |
91-
# Clean start - remove composer.lock to avoid cached dependency issues
92-
rm -f composer.lock
93-
94-
# Add bc command needed for version comparison
95-
sudo apt-get install -y bc
86+
echo "Installing PHPUnit for PHP ${{ matrix.php }}"
9687
97-
# Define PHP version variable for use in conditions
88+
# Define PHP version for easier conditions
9889
PHP_VERSION="${{ matrix.php }}"
9990
100-
# Determine what PHPUnit version to use
101-
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
102-
echo "::notice::Using PHP $PHP_VERSION, configuring for PHPUnit 9.x"
103-
# Use jq to update phpunit requirement in composer.json
104-
cp composer.json composer.json.bak
105-
jq '.["require-dev"]["phpunit/phpunit"] = "^9.0"' composer.json.bak > composer.json
106-
107-
# Also update yoast/phpunit-polyfills to latest
108-
jq '.["require-dev"]["yoast/phpunit-polyfills"] = "^2.0"' composer.json > composer.json.tmp
109-
mv composer.json.tmp composer.json
110-
else
111-
echo "::notice::Using PHP $PHP_VERSION, keeping PHPUnit 7.5.x"
112-
# No change needed for PHP 7.x
113-
fi
91+
# Remove any existing lock file to ensure clean install
92+
rm -f composer.lock
11493
115-
# Write a custom composer.json for PHP 8.2 to handle specific requirements
116-
if [[ "$PHP_VERSION" == "8.2" ]]; then
117-
echo "::notice::Using PHP 8.2, creating specialized composer configuration"
118-
119-
# Start with a copy of the existing composer.json
120-
cp composer.json composer.json.original
121-
122-
# Update dependencies for PHP 8.2 compatibility
123-
jq '.["require-dev"]["phpunit/phpunit"] = "^9.5"' composer.json.original > composer.json.tmp
124-
jq '.["require-dev"]["yoast/phpunit-polyfills"] = "^2.0"' composer.json.tmp > composer.json.tmp2
94+
# For PHP 8.x, use PHPUnit 9.5
95+
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
96+
echo "::notice::Using PHPUnit 9.5 for PHP $PHP_VERSION"
12597
126-
# Set platform config
127-
jq '.config.platform.php = "8.1.99"' composer.json.tmp2 > composer.json.tmp3
98+
# Create a compatible composer.json for PHP 8.x
99+
cp composer.json composer.json.backup
100+
jq '.["require-dev"]["phpunit/phpunit"] = "^9.5"' composer.json.backup > composer.json.tmp1
101+
jq '.["require-dev"]["yoast/phpunit-polyfills"] = "^2.0"' composer.json.tmp1 > composer.json
128102
129-
# Add PHP 8 specific test script
130-
jq '.scripts["test:php8"] = "php run-phpunit.php"' composer.json.tmp3 > composer.json
103+
# Add PHP 8 specific test script if needed
104+
if [[ "$PHP_VERSION" == "8.2" ]]; then
105+
jq '.config.platform.php = "8.1.99"' composer.json > composer.json.tmp2
106+
jq '.scripts["test:php8"] = "php run-phpunit.php"' composer.json.tmp2 > composer.json
107+
fi
131108
132-
# Clean up tmp files
133-
rm -f composer.json.tmp*
134-
rm -f composer.json.original
135-
fi
136-
137-
# Install dependencies based on PHP version
138-
echo "::notice::Installing Composer dependencies for PHP $PHP_VERSION"
139-
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
140-
composer update --no-progress --ignore-platform-reqs
109+
# Install using the modified composer.json
110+
composer update --no-progress --ignore-platform-reqs || {
111+
echo "::warning::Composer update failed, trying direct require"
112+
rm -rf vendor
113+
composer require --dev phpunit/phpunit:^9.5 yoast/phpunit-polyfills:^2.0 --no-progress --ignore-platform-reqs || {
114+
echo "::error::PHPUnit installation failed"
115+
exit 1
116+
}
117+
}
141118
else
142-
composer update --no-progress
119+
# For PHP 7.x, use PHPUnit 7.5
120+
echo "::notice::Using PHPUnit 7.5 for PHP $PHP_VERSION"
121+
composer update --no-progress || {
122+
echo "::warning::Composer update failed, trying direct require"
123+
rm -rf vendor
124+
composer require --dev phpunit/phpunit:^7.5 yoast/phpunit-polyfills:^1.0 --no-progress || {
125+
echo "::error::PHPUnit installation failed"
126+
exit 1
127+
}
128+
}
143129
fi
144130
145-
# Check if installation was successful
146-
if [ ! -d "vendor" ] || [ ! -f "vendor/bin/phpunit" ]; then
147-
echo "::error::Failed to install PHPUnit. Trying a direct approach."
148-
rm -rf vendor
149-
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
150-
composer require --dev yoast/phpunit-polyfills:^2.0 phpunit/phpunit:^9.5 --ignore-platform-reqs --no-progress
151-
else
152-
composer require --dev yoast/phpunit-polyfills:^1.0 phpunit/phpunit:^7.5 --no-progress
153-
fi
131+
# Clean up temporary files
132+
rm -f composer.json.tmp* composer.json.backup
133+
134+
# Verify PHPUnit installation
135+
if [ ! -f "vendor/bin/phpunit" ]; then
136+
echo "::error::PHPUnit installation unsuccessful."
137+
exit 1
154138
fi
155139
156-
# Verify PHPUnit version
140+
# Show installed PHPUnit version
157141
echo "Installed PHPUnit version:"
158-
vendor/bin/phpunit --version || echo "PHPUnit seems to be missing or broken"
142+
vendor/bin/phpunit --version || echo "PHPUnit seems to be installed but not working properly"
159143
160144
- name: Check Test Environment
161145
id: check-test-env
@@ -170,26 +154,33 @@ jobs:
170154
echo "test_env_ready=true" >> $GITHUB_OUTPUT
171155
fi
172156
157+
# Enhanced WordPress Test Environment Setup
173158
- name: Setup WordPress Test Environment
174159
if: steps.check-test-env.outputs.test_env_ready == 'true'
175160
run: |
176-
if mysql -u root -h 127.0.0.1 -e "USE wordpress_test;" 2>/dev/null; then
177-
echo "Database 'wordpress_test' already exists. Dropping it first."
178-
mysql -u root -h 127.0.0.1 -e "DROP DATABASE wordpress_test;"
179-
fi
180-
181-
# Create the database explicitly to avoid potential issues
182-
mysql -u root -h 127.0.0.1 -e "CREATE DATABASE IF NOT EXISTS wordpress_test;"
161+
# Ensure database exists and is clean
162+
echo "Setting up test database..."
163+
mysql -u root -h 127.0.0.1 -e "DROP DATABASE IF EXISTS wordpress_test;"
164+
mysql -u root -h 127.0.0.1 -e "CREATE DATABASE wordpress_test;"
183165
184166
# Make the install script executable
185167
chmod +x bin/install-wp-tests.sh
186168
187-
# Install WordPress test environment
169+
# Run the script with detailed logging
170+
echo "Installing WordPress test environment for version ${{ matrix.wordpress }}..."
188171
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 ${{ matrix.wordpress }} || {
189-
echo "::warning::Failed to setup WordPress test environment. Showing detailed error information:"
190-
cat /tmp/wordpress-tests-lib-install-*.log 2>/dev/null || echo "No install logs found."
172+
echo "::error::Failed to setup WordPress test environment. Showing detailed error information:"
173+
find /tmp -name "wordpress-tests-lib-install-*.log" -exec cat {} \; 2>/dev/null || echo "No install logs found."
191174
exit 1
192175
}
176+
177+
# Verify the test environment was created successfully
178+
if [ ! -d "/tmp/wordpress-tests-lib" ] || [ ! -f "/tmp/wordpress-tests-lib/includes/functions.php" ]; then
179+
echo "::error::WordPress test environment setup failed. Missing required files."
180+
exit 1
181+
fi
182+
183+
echo "WordPress test environment successfully set up."
193184
194185
- name: Run tests
195186
if: steps.check-test-env.outputs.test_env_ready == 'true'

bin/install-wp-tests.sh

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ if [ $# -lt 3 ]; then
1414
exit 1
1515
fi
1616

17+
# Check for missing dependencies before proceeding
18+
if ! command -v mysql >/dev/null 2>&1; then
19+
echo "Error: MySQL client is not installed."
20+
exit 1
21+
fi
22+
1723
DB_NAME=$1
1824
DB_USER=$2
1925
DB_PASS=$3
@@ -35,10 +41,15 @@ echo " WP_TESTS_DIR: $WP_TESTS_DIR"
3541
echo " WP_CORE_DIR: $WP_CORE_DIR"
3642

3743
download() {
38-
if [ `which curl` ]; then
39-
curl -s "$1" > "$2";
40-
elif [ `which wget` ]; then
41-
wget -nv -O "$2" "$1"
44+
if command -v curl >/dev/null 2>&1; then
45+
echo "Downloading $1 using curl..."
46+
curl -s "$1" -o "$2" || { echo "Error: Failed to download $1"; exit 1; }
47+
elif command -v wget >/dev/null 2>&1; then
48+
echo "Downloading $1 using wget..."
49+
wget -nv -O "$2" "$1" || { echo "Error: Failed to download $1"; exit 1; }
50+
else
51+
echo "Error: Neither curl nor wget is available for downloads."
52+
exit 1
4253
fi
4354
}
4455

@@ -63,17 +74,18 @@ elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
6374
WP_TESTS_TAG="trunk"
6475
echo "Using WordPress trunk/nightly"
6576
else
66-
# http serves a single offer, whereas https serves multiple. we only want one
67-
echo "Determining latest WordPress version..."
68-
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
69-
grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
70-
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
71-
if [[ -z "$LATEST_VERSION" ]]; then
72-
echo "Error: Latest WordPress version could not be found"
73-
exit 1
77+
# Fetch the latest WordPress version with fallback
78+
if [[ $WP_VERSION == 'latest' ]]; then
79+
echo "Determining the latest WordPress version..."
80+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
81+
LATEST_VERSION=$(grep -oP '"version":"\K[^"]+' /tmp/wp-latest.json | head -1)
82+
if [[ -z "$LATEST_VERSION" ]]; then
83+
echo "Error: Could not determine the latest WordPress version."
84+
exit 1
85+
fi
86+
WP_TESTS_TAG="tags/$LATEST_VERSION"
87+
echo "Using latest WordPress version: $LATEST_VERSION"
7488
fi
75-
WP_TESTS_TAG="tags/$LATEST_VERSION"
76-
echo "Latest WordPress version: $LATEST_VERSION"
7789
fi
7890
set -ex
7991

@@ -167,6 +179,31 @@ install_test_suite() {
167179
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
168180
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
169181
fi
182+
183+
# Verify test environment was created successfully
184+
if [ ! -d "$WP_TESTS_DIR/includes" ] || [ ! -f "$WP_TESTS_DIR/wp-tests-config.php" ]; then
185+
echo "Error: WordPress test environment setup failed. Missing required files."
186+
exit 1
187+
fi
188+
}
189+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
190+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
191+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
192+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
193+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
194+
fi
195+
196+
# Verify test environment was created successfully
197+
if [ ! -d "$WP_TESTS_DIR/includes" ] || [ ! -f "$WP_TESTS_DIR/wp-tests-config.php" ]; then
198+
echo "Error: WordPress test environment setup failed. Missing required files."
199+
exit 1
200+
fi
201+
}
202+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
203+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
204+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
205+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
206+
fi
170207

171208
}
172209

@@ -205,10 +242,30 @@ install_db() {
205242

206243
# create database
207244
echo "Creating database $DB_NAME..."
208-
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
245+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA || {
246+
echo "Error: Failed to create database $DB_NAME. Check your credentials and permissions."
247+
exit 1
248+
}
209249

210-
# Verify database was created successfully
211-
if [ $? -ne 0 ]; then
250+
echo "Database $DB_NAME created successfully."
251+
}
252+
253+
echo "Starting WordPress test environment setup..."
254+
255+
install_wp
256+
install_test_suite
257+
install_db
258+
259+
echo "WordPress test environment setup complete."
260+
echo "Log file available at: $LOG_FILE"
261+
262+
# Final verification of test environment
263+
if [ ! -d "$WP_TESTS_DIR/includes" ] || [ ! -f "$WP_TESTS_DIR/wp-tests-config.php" ]; then
264+
echo "Error: WordPress test environment setup failed. Missing required files."
265+
exit 1
266+
fi
267+
268+
echo "Success: WordPress test environment is ready."
212269
echo "Error: Could not create database $DB_NAME."
213270
echo "Please check your database credentials and permissions."
214271
exit 1

0 commit comments

Comments
 (0)