Skip to content

Commit f530260

Browse files
committed
ci: update install-wp-tests.sh to match wp-cli generated script
1 parent 3dc19f4 commit f530260

File tree

1 file changed

+88
-24
lines changed

1 file changed

+88
-24
lines changed

bin/install-wp-tests.sh

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,43 @@ DB_HOST=${4-localhost}
1212
WP_VERSION=${5-latest}
1313
SKIP_DB_CREATE=${6-false}
1414

15-
WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
16-
WP_CORE_DIR=${WP_CORE_DIR-/tmp/wordpress/}
15+
TMPDIR=${TMPDIR-/tmp}
16+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
17+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
18+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
1719

18-
command_exists() {
19-
type -t "$1" >/dev/null 2>&1
20-
}
2120
download() {
22-
if command_exists "curl"; then
23-
curl -s -o "${2:--}" "$1"
24-
elif command_exists "wget"; then
25-
wget -nv -O "${2:--}" "$1"
21+
if [ `which curl` ]; then
22+
curl -s "$1" > "$2";
23+
elif [ `which wget` ]; then
24+
wget -nv -O "$2" "$1"
25+
else
26+
echo "Error: Neither curl nor wget is installed."
27+
exit 1
2628
fi
2729
}
28-
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+(\.[0-9]+)? ]]; then
29-
WP_TESTS_TAG="tags/$WP_VERSION"
30+
31+
# Check if svn is installed
32+
check_svn_installed() {
33+
if ! command -v svn > /dev/null; then
34+
echo "Error: svn is not installed. Please install svn and try again."
35+
exit 1
36+
fi
37+
}
38+
39+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
40+
WP_BRANCH=${WP_VERSION%\-*}
41+
WP_TESTS_TAG="branches/$WP_BRANCH"
42+
43+
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
44+
WP_TESTS_TAG="branches/$WP_VERSION"
45+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
46+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
47+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
48+
WP_TESTS_TAG="tags/${WP_VERSION%??}"
49+
else
50+
WP_TESTS_TAG="tags/$WP_VERSION"
51+
fi
3052
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
3153
WP_TESTS_TAG="trunk"
3254
else
@@ -40,10 +62,8 @@ else
4062
fi
4163
WP_TESTS_TAG="tags/$LATEST_VERSION"
4264
fi
43-
4465
set -ex
4566

46-
4767
install_wp() {
4868

4969
if [ -d $WP_CORE_DIR ]; then
@@ -53,27 +73,44 @@ install_wp() {
5373
mkdir -p $WP_CORE_DIR
5474

5575
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
56-
mkdir -p /tmp/wordpress-nightly
57-
download https://wordpress.org/nightly-builds/wordpress-latest.zip /tmp/wordpress-nightly/wordpress-nightly.zip
58-
unzip -q /tmp/wordpress-nightly/wordpress-nightly.zip -d /tmp/wordpress-nightly/
59-
mv /tmp/wordpress-nightly/wordpress/* $WP_CORE_DIR
76+
mkdir -p $TMPDIR/wordpress-trunk
77+
rm -rf $TMPDIR/wordpress-trunk/*
78+
check_svn_installed
79+
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
80+
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
6081
else
6182
if [ $WP_VERSION == 'latest' ]; then
6283
local ARCHIVE_NAME='latest'
84+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
85+
# https serves multiple offers, whereas http serves single.
86+
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
87+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
88+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
89+
LATEST_VERSION=${WP_VERSION%??}
90+
else
91+
# otherwise, scan the releases and get the most up to date minor version of the major release
92+
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
93+
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
94+
fi
95+
if [[ -z "$LATEST_VERSION" ]]; then
96+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
97+
else
98+
local ARCHIVE_NAME="wordpress-$LATEST_VERSION"
99+
fi
63100
else
64101
local ARCHIVE_NAME="wordpress-$WP_VERSION"
65102
fi
66-
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz /tmp/wordpress.tar.gz
67-
tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
103+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
104+
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
68105
fi
69106

70-
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
107+
download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
71108
}
72109

73110
install_test_suite() {
74111
# portable in-place argument for both GNU sed and Mac OSX sed
75112
if [[ $(uname -s) == 'Darwin' ]]; then
76-
local ioption='-i .bak'
113+
local ioption='-i.bak'
77114
else
78115
local ioption='-i'
79116
fi
@@ -82,15 +119,18 @@ install_test_suite() {
82119
if [ ! -d $WP_TESTS_DIR ]; then
83120
# set up testing suite
84121
mkdir -p $WP_TESTS_DIR
85-
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
86-
svn co --quiet https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
122+
rm -rf $WP_TESTS_DIR/{includes,data}
123+
check_svn_installed
124+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
125+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
87126
fi
88127

89128
if [ ! -f wp-tests-config.php ]; then
90129
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
91130
# remove all forward slashes in the end
92131
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
93132
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
133+
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
94134
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
95135
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
96136
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
@@ -99,6 +139,23 @@ install_test_suite() {
99139

100140
}
101141

142+
recreate_db() {
143+
shopt -s nocasematch
144+
if [[ $1 =~ ^(y|yes)$ ]]
145+
then
146+
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
147+
create_db
148+
echo "Recreated the database ($DB_NAME)."
149+
else
150+
echo "Leaving the existing database ($DB_NAME) in place."
151+
fi
152+
shopt -u nocasematch
153+
}
154+
155+
create_db() {
156+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
157+
}
158+
102159
install_db() {
103160

104161
if [ ${SKIP_DB_CREATE} = "true" ]; then
@@ -122,7 +179,14 @@ install_db() {
122179
fi
123180

124181
# create database
125-
mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute "CREATE DATABASE IF NOT EXISTS $DB_NAME;"
182+
if [ $(mysql --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep ^$DB_NAME$) ]
183+
then
184+
echo "Reinstalling will delete the existing test database ($DB_NAME)"
185+
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
186+
recreate_db $DELETE_EXISTING_DB
187+
else
188+
create_db
189+
fi
126190
}
127191

128192
install_wp

0 commit comments

Comments
 (0)