11#! /usr/bin/env bash
22
33if [ $# -lt 3 ]; then
4- echo " usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
4+ echo " usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [force download] [skip-database-creation] "
55 exit 1
66fi
77
@@ -10,56 +10,102 @@ DB_USER=$2
1010DB_PASS=$3
1111DB_HOST=${4-localhost}
1212WP_VERSION=${5-latest}
13+ FORCE=${6-false}
14+ SKIP_DB_CREATE=${7-false}
1315
1416WP_TESTS_DIR=${WP_TESTS_DIR-/ tmp/ wordpress-tests-lib}
1517WP_CORE_DIR=${WP_CORE_DIR-/ tmp/ wordpress/ }
1618
19+ # Placeholder for download agent
20+ # @todo replace back to wget everywhere in this file
1721download () {
22+
1823 if [ ` which curl` ]; then
1924 curl -s " $1 " > " $2 " ;
20- elif [ ` which wget` ]; then
25+ elif [ ` which wget` ]; then
2126 wget -nv -O " $2 " " $1 "
22- fi
27+ fi
28+
2329}
2430
31+ # Detect version tag
32+ # Format: N.N.N
2533if [[ $WP_VERSION =~ [0-9]+\. [0-9]+ (\. [0-9]+)? ]]; then
2634 WP_TESTS_TAG=" tags/$WP_VERSION "
35+ elif [[ $WP_VERSION == ' nightly' || $WP_VERSION == ' trunk' ]]; then
36+ WP_TESTS_TAG=" trunk"
2737else
2838 # http serves a single offer, whereas https serves multiple. we only want one
2939 download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
3040 grep ' [0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
3141 LATEST_VERSION=$( grep -o ' "version":"[^"]*' /tmp/wp-latest.json | sed ' s/"version":"//' )
42+
3243 if [[ -z " $LATEST_VERSION " ]]; then
3344 echo " Latest WordPress version could not be found"
3445 exit 1
3546 fi
47+
3648 WP_TESTS_TAG=" tags/$LATEST_VERSION "
3749fi
3850
51+ if [[ $WP_TESTS_TAG == * " beta" * ]]
52+ then
53+ WP_TESTS_TAG=" trunk"
54+ fi
55+
3956set -ex
4057
4158install_wp () {
4259
60+ echo " Installing WordPress for Unit Tests"
61+
62+ if [[ $FORCE == ' true' || -z TRAVIS_JOB_ID ]]; then
63+ echo " Removing existing core WordPress directory"
64+
65+ rm -Rf $WP_CORE_DIR
66+ fi
67+
4368 if [ -d $WP_CORE_DIR ]; then
4469 return ;
4570 fi
4671
72+ echo " Downloading WordPress"
73+
4774 mkdir -p $WP_CORE_DIR
4875
49- if [ $WP_VERSION == ' latest' ]; then
50- local ARCHIVE_NAME=' latest'
76+ if [[ $WP_VERSION == ' nightly' || $WP_VERSION == ' trunk' ]]; then
77+ mkdir -p /tmp/wordpress-nightly
78+ download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
79+ unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
80+ mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
5181 else
52- local ARCHIVE_NAME=" wordpress-$WP_VERSION "
53- fi
82+ if [ $WP_VERSION == ' latest' ]; then
83+ local ARCHIVE_NAME=' latest'
84+ else
85+ local ARCHIVE_NAME=" wordpress-$WP_VERSION "
86+ fi
5487
55- download https://wordpress.org/${ARCHIVE_NAME} .tar.gz /tmp/wordpress.tar.gz
56- tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
88+ download https://wordpress.org/${ARCHIVE_NAME} .tar.gz /tmp/wordpress.tar.gz
89+ tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
90+ fi
5791
5892 download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR /wp-content/db.php
93+
94+ echo " WordPress installed"
95+
5996}
6097
6198install_test_suite () {
62- # portable in-place argument for both GNU sed and Mac OSX sed
99+
100+ echo " Installing Tests Suite for Unit Tests"
101+
102+ if [[ $FORCE == ' true' || -z TRAVIS_JOB_ID ]]; then
103+ echo " Removing existing Tests Suite directory"
104+
105+ rm -Rf $WP_TESTS_DIR
106+ fi
107+
108+ # portable in-place argument for both GNU sed and Mac OS X sed
63109 if [[ $( uname -s) == ' Darwin' ]]; then
64110 local ioption=' -i .bak'
65111 else
@@ -68,25 +114,39 @@ install_test_suite() {
68114
69115 # set up testing suite if it doesn't yet exist
70116 if [ ! -d $WP_TESTS_DIR ]; then
117+ echo " Downloading Tests Suite"
118+
71119 # set up testing suite
72120 mkdir -p $WP_TESTS_DIR
73- svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/includes/ $WP_TESTS_DIR /includes
121+ svn export --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/includes $WP_TESTS_DIR /includes
122+ svn export --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG} /tests/phpunit/data $WP_TESTS_DIR /data
74123 fi
75124
76125 cd $WP_TESTS_DIR
77126
78127 if [ ! -f wp-tests-config.php ]; then
79128 download https://develop.svn.wordpress.org/${WP_TESTS_TAG} /wp-tests-config-sample.php " $WP_TESTS_DIR " /wp-tests-config.php
80- sed $ioption " s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR ':" " $WP_TESTS_DIR " /wp-tests-config.php
129+ # remove all forward slashes in the end
130+ WP_CORE_DIR=$( echo $WP_CORE_DIR | sed " s:/\+$::" )
131+ sed $ioption " s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR /':" " $WP_TESTS_DIR " /wp-tests-config.php
81132 sed $ioption " s/youremptytestdbnamehere/$DB_NAME /" " $WP_TESTS_DIR " /wp-tests-config.php
82133 sed $ioption " s/yourusernamehere/$DB_USER /" " $WP_TESTS_DIR " /wp-tests-config.php
83134 sed $ioption " s/yourpasswordhere/$DB_PASS /" " $WP_TESTS_DIR " /wp-tests-config.php
84135 sed $ioption " s|localhost|${DB_HOST} |" " $WP_TESTS_DIR " /wp-tests-config.php
85136 fi
86137
138+ echo " Tests Suite installed"
139+
87140}
88141
89142install_db () {
143+
144+ if [ ${SKIP_DB_CREATE} = " true" ]; then
145+ return 0
146+ fi
147+
148+ echo " Setting up Database for Unit Tests"
149+
90150 # parse DB_HOST for port or socket references
91151 local PARTS=(${DB_HOST// \: / } )
92152 local DB_HOSTNAME=${PARTS[0]} ;
@@ -103,8 +163,20 @@ install_db() {
103163 fi
104164 fi
105165
166+ if [[ $FORCE == ' true' || -z TRAVIS_JOB_ID ]]; then
167+ echo " Removing existing Database"
168+
169+ # drop database
170+ mysql --user=" $DB_USER " --password=" $DB_PASS " $EXTRA -e " DROP DATABASE IF EXISTS $DB_NAME "
171+ fi
172+
173+ echo " Creating Database"
174+
106175 # create database
107176 mysqladmin create $DB_NAME --user=" $DB_USER " --password=" $DB_PASS " $EXTRA
177+
178+ echo " Database set up"
179+
108180}
109181
110182install_wp
0 commit comments