|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +DB_HOST="${DB_HOST:-127.0.0.1}" |
| 5 | +DB_NAME="${DB_NAME:-wordpress_test}" |
| 6 | +DB_USER="${DB_USER:-wordpress}" |
| 7 | +DB_PASSWORD="${DB_PASSWORD:-wordpress}" |
| 8 | +WP_CORE_DIR="${WP_CORE_DIR:-/opt/wordpress}" |
| 9 | +WP_DEVELOP_DIR="${WP_DEVELOP_DIR:-/opt/wordpress-develop}" |
| 10 | +WP_TESTS_DIR="${WP_TESTS_DIR:-/opt/wordpress-develop/tests/phpunit}" |
| 11 | +MYSQL_SOCKET="${MYSQL_SOCKET:-/run/mysqld/mysqld.sock}" |
| 12 | +MYSQL_DATA_DIR="${MYSQL_DATA_DIR:-/tmp/mysql-data}" |
| 13 | +CONFIG_SAMPLE="${WP_DEVELOP_DIR}/wp-tests-config-sample.php" |
| 14 | +CONFIG_FILE="${WP_DEVELOP_DIR}/wp-tests-config.php" |
| 15 | + |
| 16 | +MYSQL_RUNTIME_USER="${MYSQL_RUNTIME_USER:-$(id -un 2>/dev/null || echo root)}" |
| 17 | +MYSQL_RUN_DIR="$(dirname "${MYSQL_SOCKET}")" |
| 18 | + |
| 19 | +mkdir -p "${MYSQL_RUN_DIR}" "${MYSQL_DATA_DIR}" |
| 20 | + |
| 21 | +if [ "$(id -u)" -eq 0 ]; then |
| 22 | + chown -R "${MYSQL_RUNTIME_USER}" "${MYSQL_RUN_DIR}" "${MYSQL_DATA_DIR}" |
| 23 | +fi |
| 24 | + |
| 25 | +if [ ! -d "${MYSQL_DATA_DIR}/mysql" ]; then |
| 26 | + mariadb-install-db \ |
| 27 | + --user="${MYSQL_RUNTIME_USER}" \ |
| 28 | + --datadir="${MYSQL_DATA_DIR}" \ |
| 29 | + --skip-test-db \ |
| 30 | + --auth-root-authentication-method=normal >/dev/null |
| 31 | +fi |
| 32 | + |
| 33 | +mariadbd \ |
| 34 | + --user="${MYSQL_RUNTIME_USER}" \ |
| 35 | + --datadir="${MYSQL_DATA_DIR}" \ |
| 36 | + --socket="${MYSQL_SOCKET}" \ |
| 37 | + --bind-address=127.0.0.1 \ |
| 38 | + --skip-networking=0 & |
| 39 | +MYSQLD_PID=$! |
| 40 | + |
| 41 | +cleanup() { |
| 42 | + if kill -0 "${MYSQLD_PID}" >/dev/null 2>&1; then |
| 43 | + mysqladmin --protocol=socket --socket="${MYSQL_SOCKET}" -uroot shutdown >/dev/null 2>&1 || true |
| 44 | + wait "${MYSQLD_PID}" >/dev/null 2>&1 || true |
| 45 | + fi |
| 46 | +} |
| 47 | +trap cleanup EXIT |
| 48 | + |
| 49 | +MYSQL_READY=0 |
| 50 | +for _ in $(seq 1 30); do |
| 51 | + if mysqladmin --protocol=socket --socket="${MYSQL_SOCKET}" -uroot ping >/dev/null 2>&1; then |
| 52 | + MYSQL_READY=1 |
| 53 | + break |
| 54 | + fi |
| 55 | + sleep 1 |
| 56 | +done |
| 57 | + |
| 58 | +if [ "${MYSQL_READY}" -ne 1 ]; then |
| 59 | + echo "Timed out waiting for MariaDB to accept connections" >&2 |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +mysql --protocol=socket --socket="${MYSQL_SOCKET}" -uroot <<SQL |
| 64 | +CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
| 65 | +CREATE USER IF NOT EXISTS '${DB_USER}'@'%' IDENTIFIED BY '${DB_PASSWORD}'; |
| 66 | +GRANT ALL PRIVILEGES ON \`${DB_NAME}\`.* TO '${DB_USER}'@'%'; |
| 67 | +FLUSH PRIVILEGES; |
| 68 | +SQL |
| 69 | + |
| 70 | +if [ ! -f "${CONFIG_FILE}" ]; then |
| 71 | + echo "wp-tests-config.php missing; recreating from sample" >&2 |
| 72 | + if [ -f "${CONFIG_SAMPLE}" ]; then |
| 73 | + cp "${CONFIG_SAMPLE}" "${CONFIG_FILE}" |
| 74 | + else |
| 75 | + echo "Sample config not found at ${CONFIG_SAMPLE}" >&2 |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | +fi |
| 79 | + |
| 80 | +sed -i "s/youremptytestdbnamehere/${DB_NAME}/" "${CONFIG_FILE}" |
| 81 | +sed -i "s/yourusernamehere/${DB_USER}/" "${CONFIG_FILE}" |
| 82 | +sed -i "s/yourpasswordhere/${DB_PASSWORD}/" "${CONFIG_FILE}" |
| 83 | +sed -i "s|localhost|${DB_HOST}|1" "${CONFIG_FILE}" |
| 84 | +sed -i "s|dirname( __FILE__ ) . '/../../'|'${WP_CORE_DIR}/'|" "${CONFIG_FILE}" |
| 85 | + |
| 86 | +export WP_TESTS_DIR DB_HOST DB_NAME DB_USER DB_PASSWORD |
| 87 | + |
| 88 | +# ==== Here and below is a universal "command dispatcher" ==== |
| 89 | + |
| 90 | +WORKDIR="/srv/web" |
| 91 | +cd "$WORKDIR" |
| 92 | + |
| 93 | +log_section() { |
| 94 | + echo |
| 95 | + echo "========================================" |
| 96 | + echo ">>> $1" |
| 97 | + echo "========================================" |
| 98 | +} |
| 99 | + |
| 100 | +run_composer_install() { |
| 101 | + log_section "Composer install" |
| 102 | + if [ -f composer.json ]; then |
| 103 | + # Skip if vendor/ already exists. |
| 104 | + if [ -d vendor ]; then |
| 105 | + echo "vendor/ already exists, skipping composer install" |
| 106 | + else |
| 107 | + composer install --no-interaction --prefer-dist |
| 108 | + fi |
| 109 | + else |
| 110 | + echo "composer.json not found in ${WORKDIR}, skipping composer install" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +run_phpunit() { |
| 115 | + log_section "PHP Unit tests (phpunit.xml)" |
| 116 | + vendor/bin/phpunit -c phpunit.xml "$@" |
| 117 | +} |
| 118 | + |
| 119 | +run_wpunit() { |
| 120 | + log_section "WordPress Unit tests (php-wp-unit.xml)" |
| 121 | + vendor/bin/phpunit -c php-wp-unit.xml "$@" |
| 122 | +} |
| 123 | + |
| 124 | +# Collect exit codes of both suites |
| 125 | +run_all_tests() { |
| 126 | + local phpunit_exit=0 |
| 127 | + local wpunit_exit=0 |
| 128 | + |
| 129 | + run_composer_install |
| 130 | + |
| 131 | + run_phpunit "$@" || phpunit_exit=$? |
| 132 | + run_wpunit "$@" || wpunit_exit=$? |
| 133 | + |
| 134 | + if [ "$phpunit_exit" -ne 0 ] || [ "$wpunit_exit" -ne 0 ]; then |
| 135 | + echo |
| 136 | + echo "One or more test suites failed:" |
| 137 | + echo " PHP Unit exit code: $phpunit_exit" |
| 138 | + echo " WP Unit exit code: $wpunit_exit" |
| 139 | + # If needed to distinguish, we could return, for example, the first non-zero |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | +} |
| 143 | + |
| 144 | +CMD="${1:-test:all}" |
| 145 | + |
| 146 | +case "$CMD" in |
| 147 | + test:all) |
| 148 | + shift |
| 149 | + run_all_tests "$@" |
| 150 | + ;; |
| 151 | + |
| 152 | + test:phpunit) |
| 153 | + shift |
| 154 | + run_composer_install |
| 155 | + run_phpunit "$@" |
| 156 | + ;; |
| 157 | + |
| 158 | + test:wpunit) |
| 159 | + shift |
| 160 | + run_composer_install |
| 161 | + run_wpunit "$@" |
| 162 | + ;; |
| 163 | + |
| 164 | + composer-install) |
| 165 | + shift |
| 166 | + run_composer_install |
| 167 | + ;; |
| 168 | + |
| 169 | + *) |
| 170 | + # Fallback to default behavior: run the given command. |
| 171 | + # docker run image vendor/bin/phpunit -c phpunit.xml |
| 172 | + exec "$@" |
| 173 | + ;; |
| 174 | +esac |
0 commit comments