Skip to content

Commit f6490e6

Browse files
authored
Merge pull request #261 from wp-cli/add/209-updated
2 parents ccd1643 + 3770e88 commit f6490e6

File tree

2 files changed

+111
-20
lines changed

2 files changed

+111
-20
lines changed

bin/install-package-tests

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
is_numeric() {
1313
case $1 in
1414
''|*[!0-9]*) return 1;; # returns 1 if not numeric
15-
*) return 0;; # returns 0 if numeric
15+
*) return 0;; # returns 0 if numeric
1616
esac
1717
}
18+
# Promt color vars.
19+
C_RED="\033[31m"
20+
C_BLUE="\033[34m"
21+
NO_FORMAT="\033[0m"
1822

1923
HOST=localhost
2024
PORT=""
@@ -28,36 +32,57 @@ if [ -n "${WP_CLI_TEST_DBHOST}" ]; then
2832
if [ -n "${PORT}" ]; then
2933
# If the port is not numeric, then we assume it is a socket path.
3034
if is_numeric "${PORT}"; then
35+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on port ${C_BLUE}${PORT}${NO_FORMAT}"
3136
HOST_STRING="${HOST_STRING} --port=${PORT} --protocol=tcp"
3237
else
38+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT} on socket ${C_BLUE}${PORT}${NO_FORMAT}"
3339
HOST_STRING="${HOST_STRING} --socket=${PORT} --protocol=socket"
3440
fi
41+
else
42+
echo "Connecting to custom host: ${C_BLUE}${HOST}${NO_FORMAT}"
3543
fi
44+
else
45+
echo "Connecting to default host: ${C_BLUE}${HOST}${NO_FORMAT}"
3646
fi
3747

3848
USER=root
3949
if [ -n "${WP_CLI_TEST_DBROOTUSER}" ]; then
40-
USER="${WP_CLI_TEST_DBROOTUSER}"
50+
echo "Connecting with custom root user: ${C_BLUE}${WP_CLI_TEST_DBROOTUSER}${NO_FORMAT}"
51+
USER="${WP_CLI_TEST_DBROOTUSER}"
52+
else
53+
echo "Connecting with default root user: ${C_BLUE}${USER}${NO_FORMAT}"
4154
fi
4255

4356
PASSWORD_STRING=""
4457
if [ -n "${WP_CLI_TEST_DBROOTPASS}" ]; then
45-
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
58+
echo "Connecting with custom root password: ${C_BLUE}${WP_CLI_TEST_DBROOTPASS}${NO_FORMAT}"
59+
PASSWORD_STRING="-p${WP_CLI_TEST_DBROOTPASS}"
60+
else
61+
echo "Connecting with default root password: ${C_BLUE}empty${NO_FORMAT}"
4662
fi
4763

4864
TEST_DB=wp_cli_test
4965
if [ -n "${WP_CLI_TEST_DBNAME}" ]; then
50-
TEST_DB="${WP_CLI_TEST_DBNAME}"
66+
echo "Using custom test database: ${C_BLUE}${WP_CLI_TEST_DBNAME}${NO_FORMAT}"
67+
TEST_DB="${WP_CLI_TEST_DBNAME}"
68+
else
69+
echo "Using default test database: ${C_BLUE}${TEST_DB}${NO_FORMAT}"
5170
fi
5271

5372
TEST_USER=wp_cli_test
5473
if [ -n "${WP_CLI_TEST_DBUSER}" ]; then
55-
TEST_USER="${WP_CLI_TEST_DBUSER}"
74+
echo "Using custom test user: ${C_BLUE}${WP_CLI_TEST_DBUSER}${NO_FORMAT}"
75+
TEST_USER="${WP_CLI_TEST_DBUSER}"
76+
else
77+
echo "Using default test user: ${C_BLUE}${TEST_USER}${NO_FORMAT}"
5678
fi
5779

5880
TEST_PASSWORD=password1
5981
if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
60-
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
82+
echo "Using custom test password: ${C_BLUE}${WP_CLI_TEST_DBPASS}${NO_FORMAT}"
83+
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
84+
else
85+
echo "Using default test password: ${C_BLUE}${TEST_PASSWORD}${NO_FORMAT}"
6186
fi
6287

6388
echo "Detecting database version..."
@@ -71,6 +96,15 @@ case "${CLIENT_VERSION}" in
7196
;;
7297
esac
7398

99+
if [ -z "$PS1" ]; then
100+
# These vars are because github actions gave problems in the past.
101+
MYSQL_TRIES=36
102+
MYSQL_WAIT=5
103+
else
104+
MYSQL_TRIES=1
105+
MYSQL_WAIT=0
106+
fi
107+
74108
if [ "${TYPE}" = "MySQL" ]; then
75109
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
76110
else
@@ -88,42 +122,54 @@ echo 'Checking if database is ready...'
88122
if [ "${TYPE}" = "MySQL" ]; then
89123
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
90124
do
91-
echo 'Waiting for database...'
92-
sleep 5
93125
i=$((i+1))
94-
if [ $i -gt 36 ]; then
95-
echo 'Database failed to start. Aborting.'
96-
exit 1
126+
if [ "${MYSQL_TRIES}" -gt 1 ]; then
127+
echo "Waiting for MySQL(${i}/${MYSQL_TRIES})..."
128+
sleep ${MYSQL_WAIT}
129+
fi
130+
131+
if [ $i -ge $MYSQL_TRIES ]; then
132+
echo "${C_RED}MySQL failed to start. Aborting.${NO_FORMAT}"
133+
echo "Cannot connect to MySQL server. For all available variables, check the documentation at:"
134+
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
135+
exit 1
97136
fi
98137
done
99138
else
100139
while ! mariadb ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
101140
do
102-
echo 'Waiting for database...'
103-
sleep 5
104141
i=$((i+1))
105-
if [ $i -gt 36 ]; then
106-
echo 'Database failed to start. Aborting.'
107-
exit 1
142+
if [ "${MYSQL_TRIES}" -gt 1 ]; then
143+
echo "Waiting for MariaDB(${i}/${MYSQL_TRIES})..."
144+
sleep ${MYSQL_WAIT}
145+
fi
146+
147+
if [ $i -ge $MYSQL_TRIES ]; then
148+
echo "${C_RED}MariaDB failed to start. Aborting.${NO_FORMAT}"
149+
echo "Cannot connect to MariaDB server. For all available variables, check the documentation at:"
150+
echo " ${C_BLUE}https://github.com/wp-cli/wp-cli-tests?tab=readme-ov-file#the-database-credentials${NO_FORMAT}"
151+
exit 1
108152
fi
109153
done
110154
fi
111155

112156
# Prepare the database for running the tests with a MySQL version 8.0 or higher.
113157
install_mysql_db_8_0_plus() {
114-
set -ex
158+
set -ex # print all the commands.
115159
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
116160
mysql -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
117161
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
118162
mysql -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
163+
{ set +ex; } 2> /dev/null # stop printing the commands
119164
}
120165

121166
# Prepare the database for running the tests with a MySQL version lower than 8.0.
122167
install_mysql_db_lower_than_8_0() {
123-
set -ex
168+
set -ex # print all the commands.
124169
mysql -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
125170
mysql -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
126171
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
172+
{ set +ex; } 2> /dev/null # stop printing the commands
127173
}
128174

129175
# Prepare the database for running the tests with MariaDB
@@ -142,3 +188,6 @@ elif [ "${MAJOR}" -ge 8 ]; then
142188
else
143189
install_mysql_db_lower_than_8_0
144190
fi
191+
192+
echo "Succesfully prepared the database for running tests."
193+
echo "This command does not have to be run again."

src/Context/FeatureContext.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use SebastianBergmann\CodeCoverage\CodeCoverage;
2222
use SebastianBergmann\Environment\Runtime;
2323
use RuntimeException;
24+
use WP_CLI;
2425
use DirectoryIterator;
2526
use WP_CLI\Process;
2627
use WP_CLI\ProcessRun;
@@ -866,6 +867,7 @@ public function __construct() {
866867

867868
$this->variables['CORE_CONFIG_SETTINGS'] = Utils\assoc_args_to_str( self::$db_settings );
868869

870+
$this->test_connection();
869871
$this->drop_db();
870872
$this->set_cache_dir();
871873
}
@@ -1110,8 +1112,9 @@ private function set_cache_dir(): void {
11101112
* @param string $sql_cmd Command to run.
11111113
* @param array<string, string> $assoc_args Optional. Associative array of options. Default empty.
11121114
* @param bool $add_database Optional. Whether to add dbname to the $sql_cmd. Default false.
1115+
* @return array{stdout: string, stderr: string, exit_code: int}
11131116
*/
1114-
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ): void {
1117+
private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = false ) {
11151118
$default_assoc_args = [
11161119
'host' => self::$db_settings['dbhost'],
11171120
'user' => self::$db_settings['dbuser'],
@@ -1120,11 +1123,19 @@ private static function run_sql( $sql_cmd, $assoc_args = [], $add_database = fal
11201123
if ( $add_database ) {
11211124
$sql_cmd .= ' ' . escapeshellarg( self::$db_settings['dbname'] );
11221125
}
1126+
$send_to_shell = true;
1127+
if ( isset( $assoc_args['send_to_shell'] ) ) {
1128+
$send_to_shell = (bool) $assoc_args['send_to_shell'];
1129+
unset( $assoc_args['send_to_shell'] );
1130+
}
1131+
11231132
$start_time = microtime( true );
1124-
Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ) );
1133+
$result = Utils\run_mysql_command( $sql_cmd, array_merge( $assoc_args, $default_assoc_args ), null, $send_to_shell );
11251134
if ( self::$log_run_times ) {
11261135
self::log_proc_method_run_time( 'run_sql ' . $sql_cmd, $start_time );
11271136
}
1137+
1138+
return array_combine( [ 'stdout', 'stderr', 'exit_code' ], $result );
11281139
}
11291140

11301141
public function create_db(): void {
@@ -1136,6 +1147,37 @@ public function create_db(): void {
11361147
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
11371148
}
11381149

1150+
/**
1151+
* Test if the database connection is working.
1152+
*/
1153+
public function test_connection(): void {
1154+
if ( 'sqlite' === self::$db_type ) {
1155+
return;
1156+
}
1157+
1158+
$sql_result = self::run_sql(
1159+
self::$mysql_binary . ' --no-defaults',
1160+
[
1161+
'execute' => 'SELECT 1',
1162+
'send_to_shell' => false,
1163+
]
1164+
);
1165+
1166+
if ( 0 !== $sql_result['exit_code'] ) {
1167+
# WP_CLI output functions are suppressed in behat context.
1168+
echo 'There was an error connecting to the database:' . \PHP_EOL;
1169+
if ( ! empty( $sql_result['stderr'] ) ) {
1170+
echo ' ' . trim( $sql_result['stderr'] ) . \PHP_EOL;
1171+
}
1172+
echo 'run `composer prepare-tests` to connect to the database.' . \PHP_EOL;
1173+
die( $sql_result['exit_code'] );
1174+
} elseif ( ! empty( $sql_result['stderr'] ) ) {
1175+
// There is "error" output but not an exit code.
1176+
// Probably a warning, still display it.
1177+
echo trim( $sql_result['stderr'] ) . \PHP_EOL;
1178+
}
1179+
}
1180+
11391181
public function drop_db(): void {
11401182
if ( 'sqlite' === self::$db_type ) {
11411183
return;

0 commit comments

Comments
 (0)