|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +if [ $# -lt 3 ]; then |
| 4 | + echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]" |
| 5 | + exit 1 |
| 6 | +fi |
| 7 | + |
| 8 | +DB_NAME=$1 |
| 9 | +DB_USER=$2 |
| 10 | +DB_PASS=$3 |
| 11 | +DB_HOST=${4-localhost} |
| 12 | +WP_VERSION=${5-latest} |
| 13 | + |
| 14 | +TMPDIR=${TMPDIR-/tmp} |
| 15 | +TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") |
| 16 | +WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} |
| 17 | +WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress/} |
| 18 | + |
| 19 | +RETRIES=5 |
| 20 | +SLEEP=10 |
| 21 | + |
| 22 | +download_and_extract_wordpress_develop() { |
| 23 | + local ref=$1 |
| 24 | + local archive_url=$2 |
| 25 | + local dest_dir=$3 |
| 26 | + |
| 27 | + rm -rf "$dest_dir" |
| 28 | + mkdir -p "$dest_dir" |
| 29 | + |
| 30 | + local archive_file |
| 31 | + archive_file=$(mktemp) |
| 32 | + if ! retry_run curl -sSL "$archive_url" -o "$archive_file"; then |
| 33 | + rm -f "$archive_file" |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + |
| 37 | + if ! tar -xzf "$archive_file" -C "$dest_dir" --strip-components=1; then |
| 38 | + rm -f "$archive_file" |
| 39 | + return 1 |
| 40 | + fi |
| 41 | + |
| 42 | + rm -f "$archive_file" |
| 43 | + return 0 |
| 44 | +} |
| 45 | + |
| 46 | +# retry_run: run a command and retry on failure |
| 47 | +retry_run() { |
| 48 | + local n=0 |
| 49 | + until "$@"; do |
| 50 | + n=$((n+1)) |
| 51 | + if [ $n -ge $RETRIES ]; then |
| 52 | + echo "Command failed after $RETRIES attempts: $@" >&2 |
| 53 | + return 1 |
| 54 | + fi |
| 55 | + echo "Command failed. Retrying in $SLEEP seconds... ($n/$RETRIES)" >&2 |
| 56 | + sleep $SLEEP |
| 57 | + done |
| 58 | + return 0 |
| 59 | +} |
| 60 | + |
| 61 | +if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then |
| 62 | + WP_TESTS_TAG="branches/$WP_VERSION" |
| 63 | +elif [[ $WP_VERSION == 'trunk' ]]; then |
| 64 | + WP_TESTS_TAG="trunk" |
| 65 | +else |
| 66 | + # fetch the latest version with retries |
| 67 | + TMP_VER_FILE=$(mktemp) |
| 68 | + if ! retry_run curl -sSL https://api.wordpress.org/core/version-check/1.1/ -o "$TMP_VER_FILE"; then |
| 69 | + echo "Latest WordPress version could not be fetched after $RETRIES attempts" >&2 |
| 70 | + rm -f "$TMP_VER_FILE" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + LATEST_VERSION=$(tail -1 "$TMP_VER_FILE") |
| 74 | + rm -f "$TMP_VER_FILE" |
| 75 | + if [[ -z "$LATEST_VERSION" ]]; then |
| 76 | + echo "Latest WordPress version could not be found" |
| 77 | + exit 1 |
| 78 | + fi |
| 79 | + WP_TESTS_TAG="tags/$LATEST_VERSION" |
| 80 | +fi |
| 81 | + |
| 82 | +set -ex |
| 83 | + |
| 84 | +install_wp_and_test_suite() { |
| 85 | + local wp_develop_dir="$TMPDIR/wordpress-develop" |
| 86 | + local wp_develop_url="" |
| 87 | + local wp_develop_ref="" |
| 88 | + |
| 89 | + if [[ $WP_TESTS_TAG == trunk ]]; then |
| 90 | + wp_develop_ref="trunk" |
| 91 | + wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}" |
| 92 | + elif [[ $WP_TESTS_TAG =~ ^branches/ ]]; then |
| 93 | + wp_develop_ref=${WP_TESTS_TAG#branches/} |
| 94 | + wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}" |
| 95 | + elif [[ $WP_TESTS_TAG =~ ^tags/ ]]; then |
| 96 | + wp_develop_ref=${WP_TESTS_TAG#tags/} |
| 97 | + wp_develop_url="https://codeload.github.com/WordPress/wordpress-develop/tar.gz/${wp_develop_ref}" |
| 98 | + fi |
| 99 | + |
| 100 | + # setup up WordPress |
| 101 | + if [ ! -d "$WP_CORE_DIR" ]; then |
| 102 | + mkdir -p "$WP_CORE_DIR" |
| 103 | + if [[ -n "$wp_develop_url" ]] && download_and_extract_wordpress_develop "$wp_develop_ref" "$wp_develop_url" "$wp_develop_dir"; then |
| 104 | + rm -rf "$WP_CORE_DIR" |
| 105 | + mkdir -p "$WP_CORE_DIR" |
| 106 | + cp -R "$wp_develop_dir/src/." "$WP_CORE_DIR" |
| 107 | + else |
| 108 | + # Fallback to SVN if GitHub is unavailable. |
| 109 | + retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/src/ "$WP_CORE_DIR" |
| 110 | + fi |
| 111 | + fi |
| 112 | + |
| 113 | + # set up testing suite if it doesn't yet exist |
| 114 | + if [ ! -d "$WP_TESTS_DIR" ]; then |
| 115 | + # set up testing suite |
| 116 | + mkdir -p "$WP_TESTS_DIR" |
| 117 | + if [[ -n "$wp_develop_url" ]] && [ -d "$wp_develop_dir/tests/phpunit" ]; then |
| 118 | + cp -R "$wp_develop_dir/tests/phpunit/includes" "$WP_TESTS_DIR/includes" |
| 119 | + cp -R "$wp_develop_dir/tests/phpunit/data" "$WP_TESTS_DIR/data" |
| 120 | + else |
| 121 | + # Fallback to SVN if GitHub is unavailable. |
| 122 | + retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ "$WP_TESTS_DIR/includes" |
| 123 | + retry_run svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ "$WP_TESTS_DIR/data" |
| 124 | + fi |
| 125 | + fi |
| 126 | + |
| 127 | + if [ ! -f wp-tests-config.php ]; then |
| 128 | + # download wp-tests-config-sample.php with retries |
| 129 | + if [ -f "$wp_develop_dir/wp-tests-config-sample.php" ]; then |
| 130 | + cp "$wp_develop_dir/wp-tests-config-sample.php" "$WP_TESTS_DIR/wp-tests-config.php" |
| 131 | + else |
| 132 | + retry_run curl -sSL https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php -o "$WP_TESTS_DIR/wp-tests-config.php" |
| 133 | + fi |
| 134 | + # remove all forward slashes in the end |
| 135 | + WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::") |
| 136 | + sed -i "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php |
| 137 | + sed -i "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 138 | + sed -i "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 139 | + sed -i "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 140 | + sed -i "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php |
| 141 | + fi |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +install_db() { |
| 146 | + # parse DB_HOST for port or socket references |
| 147 | + local PARTS=(${DB_HOST//\:/ }) |
| 148 | + local DB_HOSTNAME=${PARTS[0]}; |
| 149 | + local DB_SOCK_OR_PORT=${PARTS[1]}; |
| 150 | + local EXTRA="" |
| 151 | + |
| 152 | + if ! [ -z $DB_HOSTNAME ] ; then |
| 153 | + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then |
| 154 | + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" |
| 155 | + elif ! [ -z $DB_SOCK_OR_PORT ] ; then |
| 156 | + EXTRA=" --socket=$DB_SOCK_OR_PORT" |
| 157 | + elif ! [ -z $DB_HOSTNAME ] ; then |
| 158 | + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" |
| 159 | + fi |
| 160 | + fi |
| 161 | + |
| 162 | + # create database |
| 163 | + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA |
| 164 | +} |
| 165 | + |
| 166 | +install_wp_and_test_suite |
| 167 | +install_db |
0 commit comments