Skip to content

Commit 81a17c9

Browse files
Support beta versions in install-wp-tests.sh and fix shellcheck findings (#408)
Co-authored-by: nextgenthemes <nextgenthemes@users.noreply.github.com>
1 parent 8c3e69a commit 81a17c9

1 file changed

Lines changed: 52 additions & 43 deletions

File tree

‎templates/install-wp-tests.sh‎

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ WP_VERSION=${5-latest}
2222
SKIP_DB_CREATE=${6-false}
2323

2424
TMPDIR=${TMPDIR-/tmp}
25-
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
25+
TMPDIR=$(echo "$TMPDIR" | sed -e "s/\/$//")
2626
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
2727
WP_TESTS_FILE="$WP_TESTS_DIR"/includes/functions.php
2828
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
@@ -82,7 +82,12 @@ if [ "${WP_INSTALL_TESTS_SKIP_UPDATE_CHECK:-false}" != "true" ]; then
8282
check_for_updates
8383
fi
8484

85-
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
85+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-beta[0-9]+$ ]]; then
86+
# The release branch (e.g. 7.1) is only created at RC1, so betas use the
87+
# test suite from trunk. WordPress itself is still downloaded from
88+
# wordpress.org, which hosts beta archives.
89+
WP_TESTS_TAG="trunk"
90+
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-RC[0-9]+$ ]]; then
8691
WP_BRANCH=${WP_VERSION%\-*}
8792
WP_TESTS_TAG="branches/$WP_BRANCH"
8893
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
@@ -115,32 +120,33 @@ set -ex
115120

116121
install_wp() {
117122

118-
if [ -f $WP_CORE_FILE ]; then
123+
if [ -f "$WP_CORE_FILE" ]; then
119124
echo -e "${CYAN}WordPress is already installed.${RESET}"
120-
return;
125+
return
121126
fi
122127

123128
echo -e "${CYAN}Installing WordPress...${RESET}"
124129

125-
rm -rf $WP_CORE_DIR
126-
mkdir -p $WP_CORE_DIR
130+
rm -rf "$WP_CORE_DIR"
131+
mkdir -p "$WP_CORE_DIR"
127132

128133
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
129-
download https://github.com/WordPress/wordpress/archive/refs/heads/master.tar.gz $TMPDIR/wordpress.tar.gz
130-
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
134+
download https://github.com/WordPress/wordpress/archive/refs/heads/master.tar.gz "$TMPDIR"/wordpress.tar.gz
135+
tar --strip-components=1 -zxmf "$TMPDIR"/wordpress.tar.gz -C "$WP_CORE_DIR"
131136
else
132-
if [ $WP_VERSION == 'latest' ]; then
137+
if [ "$WP_VERSION" == 'latest' ]; then
133138
local ARCHIVE_NAME='latest'
134139
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+ ]]; then
135140
# https serves multiple offers, whereas http serves single.
136-
download https://api.wordpress.org/core/version-check/1.7/ $TMPDIR/wp-latest.json
141+
download https://api.wordpress.org/core/version-check/1.7/ "$TMPDIR"/wp-latest.json
137142
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
138143
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
139144
LATEST_VERSION=${WP_VERSION%??}
140145
else
141146
# otherwise, scan the releases and get the most up to date minor version of the major release
142-
local VERSION_ESCAPED=`echo $WP_VERSION | sed 's/\./\\\\./g'`
143-
LATEST_VERSION=$(grep -o '"version":"'$VERSION_ESCAPED'[^"]*' $TMPDIR/wp-latest.json | sed 's/"version":"//' | head -1)
147+
local VERSION_ESCAPED
148+
VERSION_ESCAPED=${WP_VERSION//./\\.}
149+
LATEST_VERSION=$(grep -o '"version":"'"$VERSION_ESCAPED"'[^"]*' "$TMPDIR"/wp-latest.json | sed 's/"version":"//' | head -1)
144150
fi
145151
if [[ -z "$LATEST_VERSION" ]]; then
146152
local ARCHIVE_NAME="wordpress-$WP_VERSION"
@@ -150,8 +156,8 @@ install_wp() {
150156
else
151157
local ARCHIVE_NAME="wordpress-$WP_VERSION"
152158
fi
153-
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
154-
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
159+
download https://wordpress.org/"${ARCHIVE_NAME}".tar.gz "$TMPDIR"/wordpress.tar.gz
160+
tar --strip-components=1 -zxmf "$TMPDIR"/wordpress.tar.gz -C "$WP_CORE_DIR"
155161
fi
156162
echo -e "${GREEN}WordPress installed successfully.${RESET}"
157163
}
@@ -165,11 +171,11 @@ install_test_suite() {
165171
fi
166172

167173
# set up testing suite if it doesn't yet exist or only partially exists
168-
if [ ! -f $WP_TESTS_FILE ]; then
174+
if [ ! -f "$WP_TESTS_FILE" ]; then
169175
echo -e "${CYAN}Installing test suite...${RESET}"
170176
# set up testing suite
171-
rm -rf $WP_TESTS_DIR
172-
mkdir -p $WP_TESTS_DIR
177+
rm -rf "$WP_TESTS_DIR"
178+
mkdir -p "$WP_TESTS_DIR"
173179

174180
if [[ $WP_TESTS_TAG == 'trunk' ]]; then
175181
ref=trunk
@@ -225,17 +231,19 @@ install_test_suite() {
225231
exit 1
226232
fi
227233

228-
download https://raw.githubusercontent.com/WordPress/wordpress-develop/${ref}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
234+
download "https://raw.githubusercontent.com/WordPress/wordpress-develop/${ref}/wp-tests-config-sample.php" "$WP_TESTS_DIR"/wp-tests-config.php
229235
# remove all forward slashes in the end
230-
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
236+
while [[ $WP_CORE_DIR == */ ]]; do
237+
WP_CORE_DIR=${WP_CORE_DIR%/}
238+
done
231239
# escape special sed replacement characters in $WP_CORE_DIR (backslash, pipe, ampersand)
232240
WP_CORE_DIR_ESCAPED=$(printf '%s' "$WP_CORE_DIR" | sed 's/[\\|&]/\\&/g')
233-
sed $ioption "s|dirname( __FILE__ ) . '/src/'|'${WP_CORE_DIR_ESCAPED}/'|" "$WP_TESTS_DIR"/wp-tests-config.php
234-
sed $ioption "s|__DIR__ . '/src/'|'${WP_CORE_DIR_ESCAPED}/'|" "$WP_TESTS_DIR"/wp-tests-config.php
235-
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
236-
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
237-
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
238-
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
241+
sed "$ioption" "s|dirname( __FILE__ ) . '/src/'|'${WP_CORE_DIR_ESCAPED}/'|" "$WP_TESTS_DIR"/wp-tests-config.php
242+
sed "$ioption" "s|__DIR__ . '/src/'|'${WP_CORE_DIR_ESCAPED}/'|" "$WP_TESTS_DIR"/wp-tests-config.php
243+
sed "$ioption" "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
244+
sed "$ioption" "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
245+
sed "$ioption" "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
246+
sed "$ioption" "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
239247
echo -e "${GREEN}Test suite configured.${RESET}"
240248
else
241249
echo -e "${CYAN}Test suite is already configured.${RESET}"
@@ -249,9 +257,9 @@ recreate_db() {
249257
then
250258
echo -e "${CYAN}Recreating the database ($DB_NAME)...${RESET}"
251259
if command -v mariadb-admin > /dev/null 2>&1; then
252-
mariadb-admin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
260+
mariadb-admin drop "$DB_NAME" -f --user="$DB_USER" --password="$DB_PASS" "${EXTRA[@]}"
253261
else
254-
mysqladmin drop $DB_NAME -f --user="$DB_USER" --password="$DB_PASS"$EXTRA
262+
mysqladmin drop "$DB_NAME" -f --user="$DB_USER" --password="$DB_PASS" "${EXTRA[@]}"
255263
fi
256264
create_db
257265
echo -e "${GREEN}Database ($DB_NAME) recreated.${RESET}"
@@ -263,32 +271,33 @@ recreate_db() {
263271

264272
create_db() {
265273
if command -v mariadb-admin > /dev/null 2>&1; then
266-
mariadb-admin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
274+
mariadb-admin create "$DB_NAME" --user="$DB_USER" --password="$DB_PASS" "${EXTRA[@]}"
267275
else
268-
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
276+
mysqladmin create "$DB_NAME" --user="$DB_USER" --password="$DB_PASS" "${EXTRA[@]}"
269277
fi
270278
}
271279

272280
install_db() {
273281

274-
if [ ${SKIP_DB_CREATE} = "true" ]; then
282+
if [ "${SKIP_DB_CREATE}" = "true" ]; then
275283
echo -e "${YELLOW}Skipping database creation.${RESET}"
276284
return 0
277285
fi
278286

279287
# parse DB_HOST for port or socket references
280-
local PARTS=(${DB_HOST//\:/ })
288+
local PARTS
289+
read -ra PARTS <<< "${DB_HOST//:/ }"
281290
local DB_HOSTNAME=${PARTS[0]};
282291
local DB_SOCK_OR_PORT=${PARTS[1]};
283-
local EXTRA=""
284-
285-
if ! [ -z $DB_HOSTNAME ] ; then
286-
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
287-
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
288-
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
289-
EXTRA=" --socket=$DB_SOCK_OR_PORT"
290-
elif ! [ -z $DB_HOSTNAME ] ; then
291-
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
292+
local EXTRA=()
293+
294+
if [ -n "$DB_HOSTNAME" ] ; then
295+
if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]]; then
296+
EXTRA=(--host="$DB_HOSTNAME" --port="$DB_SOCK_OR_PORT" --protocol=tcp)
297+
elif [ -n "$DB_SOCK_OR_PORT" ] ; then
298+
EXTRA=(--socket="$DB_SOCK_OR_PORT")
299+
elif [ -n "$DB_HOSTNAME" ] ; then
300+
EXTRA=(--host="$DB_HOSTNAME" --protocol=tcp)
292301
fi
293302
fi
294303

@@ -298,11 +307,11 @@ install_db() {
298307
else
299308
local DB_CLIENT='mysql'
300309
fi
301-
if $DB_CLIENT --user="$DB_USER" --password="$DB_PASS"$EXTRA --execute='show databases;' | grep -q "^$DB_NAME$";
310+
if $DB_CLIENT --user="$DB_USER" --password="$DB_PASS" "${EXTRA[@]}" --execute='show databases;' | grep -q "^$DB_NAME$";
302311
then
303312
echo -e "${YELLOW}Reinstalling will delete the existing test database ($DB_NAME)${RESET}"
304-
read -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
305-
recreate_db $DELETE_EXISTING_DB
313+
read -r -p 'Are you sure you want to proceed? [y/N]: ' DELETE_EXISTING_DB
314+
recreate_db "$DELETE_EXISTING_DB"
306315
else
307316
echo -e "${CYAN}Creating database ($DB_NAME)...${RESET}"
308317
create_db

0 commit comments

Comments
 (0)