install-wp-tests.sh 5.2 KB

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