-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathinstall-package-tests
More file actions
executable file
·183 lines (160 loc) · 7.08 KB
/
install-package-tests
File metadata and controls
executable file
·183 lines (160 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/sh
# Database credentials can be provided via environment variables:
# - WP_CLI_TEST_DBHOST is the host to use and can include a port or a socket after a colon, i.e "127.0.0.1:33060" (defaults to "localhost")
# - WP_CLI_TEST_DBROOTUSER is the user that has permission to administer databases and users (defaults to "root").
# - WP_CLI_TEST_DBROOTPASS is the password to use for the above user (defaults to an empty password).
# - WP_CLI_TEST_DBNAME is the database that the tests run under (defaults to "wp_cli_test").
# - WP_CLI_TEST_DBUSER is the user that the tests run under (defaults to "wp_cli_test").
# - WP_CLI_TEST_DBPASS is the password to use for the above user (defaults to "password1").
# POSIX compliant function to check if a string is numeric.
is_numeric() {
case $1 in
''|*[!0-9]*) return 1;; # returns 1 if not numeric
*) return 0;; # returns 0 if numeric
esac
}
# Prompt color vars.
C_RED="\033[0;31m"
C_BLUE="\033[0;34m"
NO_FORMAT="\033[0m"
HOST=localhost
PORT=""
HOST_STRING=''
if [ -n "${WP_CLI_TEST_DBHOST}" ]; then
case ${WP_CLI_TEST_DBHOST##*[]]} in
(*:*) HOST=${WP_CLI_TEST_DBHOST%:*} PORT=${WP_CLI_TEST_DBHOST##*:};;
(*) HOST=${WP_CLI_TEST_DBHOST};;
esac
HOST_STRING="-h${HOST}"
if [ -n "${PORT}" ]; then
# If the port is not numeric, then we assume it is a socket path.
if is_numeric "${PORT}"; then
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on port ${C_BLUE}${PORT}${NO_FORMAT}"
HOST_STRING="${HOST_STRING} --port=${PORT} --protocol=tcp"
else
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on socket ${C_BLUE}${PORT}${NO_FORMAT}"
HOST_STRING="${HOST_STRING} --socket=${PORT} --protocol=socket"
fi
else
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT}"
fi
else
echo "Connecting to default host: ${C_BLUE}${HOST}${NO_FORMAT}"
fi
USER=root
if [ -n "${WP_CLI_TEST_DBROOTUSER}" ]; then
echo "Connecting with custom root user: ${C_BLUE}${WP_CLI_TEST_DBROOTUSER}${NO_FORMAT}"
USER="${WP_CLI_TEST_DBROOTUSER}"
else
echo "Connecting with default root user: ${C_BLUE}${USER}${NO_FORMAT}"
fi
PASSWORD_STRING=""
if [ -n "${WP_CLI_TEST_DBROOTPASS}" ]; then
echo "Connecting with custom root password: ${C_BLUE}${WP_CLI_TEST_DBROOTPASS}${NO_FORMAT}"
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
else
echo "Connecting with default root password: ${C_BLUE}empty${NO_FORMAT}"
fi
TEST_DB=wp_cli_test
if [ -n "${WP_CLI_TEST_DBNAME}" ]; then
echo "Using custom test database: ${C_BLUE}${WP_CLI_TEST_DBNAME}${NO_FORMAT}"
TEST_DB="${WP_CLI_TEST_DBNAME}"
else
echo "Using default test database: ${C_BLUE}${TEST_DB}${NO_FORMAT}"
fi
TEST_USER=wp_cli_test
if [ -n "${WP_CLI_TEST_DBUSER}" ]; then
echo "Using custom test user: ${C_BLUE}${WP_CLI_TEST_DBUSER}${NO_FORMAT}"
TEST_USER="${WP_CLI_TEST_DBUSER}"
else
echo "Using default test user: ${C_BLUE}${TEST_USER}${NO_FORMAT}"
fi
TEST_PASSWORD=password1
if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
echo "Using custom test password: ${C_BLUE}${WP_CLI_TEST_DBPASS}${NO_FORMAT}"
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
else
echo "Using default test password: ${C_BLUE}${TEST_PASSWORD}${NO_FORMAT}"
fi
echo "Detecting database version..."
# Detect which client binary is available.
CLIENT_BINARY=""
if command -v mysql >/dev/null 2>&1; then
CLIENT_BINARY="mysql"
elif command -v mariadb >/dev/null 2>&1; then
CLIENT_BINARY="mariadb"
else
echo "${C_RED}Error: Neither 'mysql' nor 'mariadb' client binary found.${NO_FORMAT}"
exit 1
fi
if [ -z "$PS1" ]; then
# These vars are because github actions gave problems in the past.
MYSQL_TRIES=36
MYSQL_WAIT=5
else
MYSQL_TRIES=1
MYSQL_WAIT=0
fi
# Use the detected client binary to query the server version.
SERVER_VERSION=$(${CLIENT_BINARY} -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}" 2>/dev/null)
# Detect the database type from the server version string.
TYPE="MySQL"
case "${SERVER_VERSION}" in
*"MariaDB"*|*"mariadb"*)
TYPE="MariaDB"
;;
esac
VERSION=$(echo "${SERVER_VERSION}" | grep -o '^[^-]*')
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
MINOR=$(echo "${VERSION}" | cut -d. -f2)
echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"
echo 'Checking if database is ready...'
i=0
while ! ${CLIENT_BINARY} ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" 2>/dev/null | grep 'information_schema' >/dev/null;
do
i=$((i+1))
if [ "${MYSQL_TRIES}" -gt 1 ]; then
echo "Waiting for database server (${i}/${MYSQL_TRIES})..."
sleep ${MYSQL_WAIT}
fi
if [ $i -ge $MYSQL_TRIES ]; then
echo "${C_RED}Database server failed to start. Aborting.${NO_FORMAT}"
echo "Cannot connect to database server. For all available variables, check the documentation at:"
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
exit 1
fi
done
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
install_mysql_db_8_0_plus() {
set -ex # print all the commands.
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
{ set +ex; } 2> /dev/null # stop printing the commands
}
# Prepare the database for running the tests with a MySQL version lower than 8.0.
install_mysql_db_lower_than_8_0() {
set -ex # print all the commands.
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
{ set +ex; } 2> /dev/null # stop printing the commands
}
# Prepare the database for running the tests with MariaDB
install_mariadb() {
set -ex
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
}
if [ "${TYPE}" = "MariaDB" ]; then
install_mariadb
elif [ "${MAJOR}" -ge 8 ]; then
install_mysql_db_8_0_plus
else
install_mysql_db_lower_than_8_0
fi
echo "Successfully prepared the database for running tests."
echo "This command does not have to be run again."