|
| 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 | +WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib} |
| 15 | +WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/} |
| 16 | + |
| 17 | +download() { |
| 18 | + if [ `which curl` ]; then |
| 19 | + curl -s "$1" > "$2"; |
| 20 | + elif [ `which wget` ]; then |
| 21 | + wget -nv -O "$2" "$1" |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then |
| 26 | + WP_TESTS_TAG="tags/$WP_VERSION" |
| 27 | +elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then |
| 28 | + WP_TESTS_TAG="trunk" |
| 29 | +else |
| 30 | + # http serves a single offer, whereas https serves multiple. we only want one |
| 31 | + download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json |
| 32 | + grep '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json |
| 33 | + LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//') |
| 34 | + if [[ -z "$LATEST_VERSION" ]]; then |
| 35 | + echo "Latest WordPress version could not be found" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + WP_TESTS_TAG="tags/$LATEST_VERSION" |
| 39 | +fi |
| 40 | + |
| 41 | +set -ex |
| 42 | + |
| 43 | +install_wp() { |
| 44 | + |
| 45 | + if [ -d $WP_CORE_DIR ]; then |
| 46 | + return; |
| 47 | + fi |
| 48 | + |
| 49 | + mkdir -p $WP_CORE_DIR |
| 50 | + |
| 51 | + if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then |
| 52 | + mkdir -p /tmp/wordpress-nightly |
| 53 | + download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip |
| 54 | + unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/ |
| 55 | + mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR |
| 56 | + else |
| 57 | + if [ $WP_VERSION == 'latest' ]; then |
| 58 | + local ARCHIVE_NAME='latest' |
| 59 | + else |
| 60 | + local ARCHIVE_NAME="wordpress-$WP_VERSION" |
| 61 | + fi |
| 62 | + download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz |
| 63 | + tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR |
| 64 | + fi |
| 65 | + |
| 66 | + download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php |
| 67 | +} |
| 68 | + |
| 69 | +install_test_suite() { |
| 70 | + # portable in-place argument for both GNU sed and Mac OSX sed |
| 71 | + if [[ $(uname -s) == 'Darwin' ]]; then |
| 72 | + local ioption='-i .bak' |
| 73 | + else |
| 74 | + local ioption='-i' |
| 75 | + fi |
| 76 | + |
| 77 | + # set up testing suite if it doesn't yet exist |
| 78 | + if [ ! -d $WP_TESTS_DIR ]; then |
| 79 | + # set up testing suite |
| 80 | + mkdir -p $WP_TESTS_DIR |
| 81 | + svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes |
| 82 | + fi |
| 83 | + |
| 84 | + cd $WP_TESTS_DIR |
| 85 | + |
| 86 | + if [ ! -f wp-tests-config.php ]; then |
| 87 | + download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php |
| 88 | + sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" "$WP_TESTS_DIR"/wp-tests-config.php |
| 89 | + sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 90 | + sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 91 | + sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php |
| 92 | + sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php |
| 93 | + fi |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +install_db() { |
| 98 | + # parse DB_HOST for port or socket references |
| 99 | + local PARTS=(${DB_HOST//\:/ }) |
| 100 | + local DB_HOSTNAME=${PARTS[0]}; |
| 101 | + local DB_SOCK_OR_PORT=${PARTS[1]}; |
| 102 | + local EXTRA="" |
| 103 | + |
| 104 | + if ! [ -z $DB_HOSTNAME ] ; then |
| 105 | + if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then |
| 106 | + EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp" |
| 107 | + elif ! [ -z $DB_SOCK_OR_PORT ] ; then |
| 108 | + EXTRA=" --socket=$DB_SOCK_OR_PORT" |
| 109 | + elif ! [ -z $DB_HOSTNAME ] ; then |
| 110 | + EXTRA=" --host=$DB_HOSTNAME --protocol=tcp" |
| 111 | + fi |
| 112 | + fi |
| 113 | + |
| 114 | + # create database |
| 115 | + mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA |
| 116 | +} |
| 117 | + |
| 118 | +install_wp |
| 119 | +install_test_suite |
| 120 | +install_db |
0 commit comments