Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 59 additions & 25 deletions bin/install-package-tests
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,54 @@ if [ -n "${WP_CLI_TEST_DBPASS}" ]; then
TEST_PASSWORD="${WP_CLI_TEST_DBPASS}"
fi

echo 'Checking if MySQL is ready...'
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
do
echo 'Waiting for MySQL...'
sleep 5
i=$((i+1))
if [ $i -gt 36 ]; then
echo 'MySQL failed to start. Aborting.'
exit 1
fi
done
echo "Detecting database version..."

TYPE="MySQL"
CLIENT_VERSION=$(mysql --version 2>/dev/null)

case "${CLIENT_VERSION}" in
*"MariaDB"*)
TYPE="MariaDB"
;;
esac

if [ "${TYPE}" = "MySQL" ]; then
SERVER_VERSION=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
else
SERVER_VERSION=$(mariadb -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
fi

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...'

if [ "${TYPE}" = "MySQL" ]; then
while ! mysql ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
do
echo 'Waiting for database...'
sleep 5
i=$((i+1))
if [ $i -gt 36 ]; then
echo 'Database failed to start. Aborting.'
exit 1
fi
done
else
while ! mariadb ${HOST_STRING} --user="${USER}" "${PASSWORD_STRING}" --execute="SHOW DATABASES;" | grep 'information_schema' >/dev/null;
do
echo 'Waiting for database...'
sleep 5
i=$((i+1))
if [ $i -gt 36 ]; then
echo 'Database failed to start. Aborting.'
exit 1
fi
done
fi

# Prepare the database for running the tests with a MySQL version 8.0 or higher.
install_mysql_db_8_0_plus() {
Expand All @@ -89,21 +126,18 @@ install_mysql_db_lower_than_8_0() {
mysql -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
}

VERSION_STRING=$(mysql -e "SELECT VERSION()" --skip-column-names ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}")
VERSION=$(echo "${VERSION_STRING}" | grep -o '^[^-]*')
MAJOR=$(echo "${VERSION}" | cut -d. -f1)
MINOR=$(echo "${VERSION}" | cut -d. -f2)
TYPE="MySQL"
case "${VERSION_STRING}" in
*"MariaDB"*)
TYPE="MariaDB"
;;
esac

echo "Detected ${TYPE} at version ${MAJOR}.${MINOR}"

# Prepare the database for running the tests with MariaDB
install_mariadb() {
set -ex
mariadb -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mariadb -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
mariadb -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
}

if [ "${TYPE}" != "MariaDB" ] && [ "${MAJOR}" -ge 8 ]; then
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
Expand Down
8 changes: 8 additions & 0 deletions features/steps.feature
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,11 @@ Feature: Make sure "Given", "When", "Then" steps work as expected
When I run `echo {WP_VERSION-latest}`
Then STDOUT should match /\d\.\d/
And STDERR should be empty

@require-mysql-or-mariadb
Scenario: SQL related variables
When I run `echo {MYSQL_BINARY}`
Then STDOUT should match /(mysql|mariadb)/

When I run `echo {SQL_DUMP_COMMAND}`
Then STDOUT should match /(mysqldump|mariadb-dump)/
25 changes: 19 additions & 6 deletions src/Context/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
*/
private static $db_type = 'mysql';

/**
* Name of mysql binary to use (mysql or mariadb). Default to mysql
*/
private static $mysql_binary = 'mysql';

/**
* Array of background process ids started by the current scenario. Used to terminate them at the end of the scenario.
*/
Expand Down Expand Up @@ -552,6 +557,7 @@
self::log_run_times_before_suite( $scope );
}
self::$behat_run_dir = getcwd();
self::$mysql_binary = Utils\get_mysql_binary_path();

Check warning on line 560 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L560

Added line #L560 was not covered by tests

$result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check();
echo "{$result->stdout}\n";
Expand Down Expand Up @@ -603,6 +609,12 @@
self::get_behat_internal_variables()
);

$mysql_binary = Utils\get_mysql_binary_path();
$sql_dump_command = Utils\get_sql_dump_command();

Check warning on line 613 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L612-L613

Added lines #L612 - L613 were not covered by tests

$this->variables['MYSQL_BINARY'] = $mysql_binary;
$this->variables['SQL_DUMP_COMMAND'] = $sql_dump_command;

Check warning on line 616 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L615-L616

Added lines #L615 - L616 were not covered by tests

// Used in the names of the RUN_DIR and SUITE_CACHE_DIR directories.
self::$temp_dir_infix = null;
$file = self::get_event_file( $scope, $line );
Expand Down Expand Up @@ -980,15 +992,15 @@
}

$dbname = self::$db_settings['dbname'];
self::run_sql( 'mysql --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "CREATE DATABASE IF NOT EXISTS $dbname" ] );

Check warning on line 995 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L995

Added line #L995 was not covered by tests
}

public function drop_db() {
if ( 'sqlite' === self::$db_type ) {
return;
}
$dbname = self::$db_settings['dbname'];
self::run_sql( 'mysql --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "DROP DATABASE IF EXISTS $dbname" ] );

Check warning on line 1003 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L1003

Added line #L1003 was not covered by tests
}

public function proc( $command, $assoc_args = [], $path = '' ) {
Expand Down Expand Up @@ -1149,7 +1161,7 @@
// Disable WP Cron by default to avoid bogus HTTP requests in CLI context.
$config_extra_php = "if ( ! defined( 'DISABLE_WP_CRON' ) ) { define( 'DISABLE_WP_CRON', true ); }\n";

if ( 'mysql' === self::$db_type ) {
if ( 'sqlite' !== self::$db_type ) {

Check warning on line 1164 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L1164

Added line #L1164 was not covered by tests
$this->create_db();
}
$this->create_run_dir();
Expand Down Expand Up @@ -1179,7 +1191,7 @@
if ( 'sqlite' === self::$db_type ) {
copy( "{$install_cache_path}.sqlite", "$run_dir/wp-content/database/.ht.sqlite" );
} else {
self::run_sql( 'mysql --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );
self::run_sql( self::$mysql_binary . ' --no-defaults', [ 'execute' => "source {$install_cache_path}.sql" ], true /*add_database*/ );

Check warning on line 1194 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L1194

Added line #L1194 was not covered by tests
}
} else {
$this->proc( 'wp core install', $install_args, $subdir )->run_check();
Expand All @@ -1189,8 +1201,9 @@

self::dir_diff_copy( $run_dir, self::$cache_dir, $install_cache_path );

if ( 'mysql' === self::$db_type ) {
$mysqldump_binary = Utils\force_env_on_nix_systems( 'mysqldump' );
if ( 'sqlite' !== self::$db_type ) {
$mysqldump_binary = Utils\get_sql_dump_command();
$mysqldump_binary = Utils\force_env_on_nix_systems( $mysqldump_binary );

Check warning on line 1206 in src/Context/FeatureContext.php

View check run for this annotation

Codecov / codecov/patch

src/Context/FeatureContext.php#L1204-L1206

Added lines #L1204 - L1206 were not covered by tests
$support_column_statistics = exec( "{$mysqldump_binary} --help | grep 'column-statistics'" );
$command = "{$mysqldump_binary} --no-defaults --no-tablespaces";
if ( $support_column_statistics ) {
Expand Down
43 changes: 32 additions & 11 deletions tests/tests/TestBehatTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ public function test_behat_tags_wp_version_github_token( $env, $expected ) {
$expected .= '&&~@broken-trunk';
}

if ( 'sqlite' !== $db_type ) {
$expected .= '&&~@require-sqlite';
}
if ( 'sqlite' === $db_type ) {
$expected .= '&&~@require-mysql';
switch ( $db_type ) {
case 'mariadb':
$expected .= '&&~@require-mysql';
$expected .= '&&~@require-sqlite';
break;
case 'sqlite':
$expected .= '&&~@require-mariadb';
$expected .= '&&~@require-mysql';
$expected .= '&&~@require-mysql-or-mariadb';
break;
case 'mysql':
default:
$expected .= '&&~@require-mariadb';
$expected .= '&&~@require-sqlite';
break;
}

$this->assertSame( '--tags=' . $expected, $output );
Expand Down Expand Up @@ -131,7 +141,7 @@ public function test_behat_tags_php_version() {
file_put_contents( $this->temp_dir . '/features/php_version.feature', $contents );

$output = exec( "cd {$this->temp_dir}; php $behat_tags" );
$this->assertSame( '--tags=' . $expected . '&&~@github-api&&~@broken&&~@require-sqlite', $output );
$this->assertSame( '--tags=' . $expected . '&&~@github-api&&~@broken&&~@require-mariadb&&~@require-sqlite', $output );

putenv( false === $env_github_token ? 'GITHUB_TOKEN' : "GITHUB_TOKEN=$env_github_token" );
}
Expand All @@ -148,12 +158,23 @@ public function test_behat_tags_extension() {

$expecteds = array();

if ( 'sqlite' !== $db_type ) {
$expecteds[] = '~@require-sqlite';
}
if ( 'sqlite' === $db_type ) {
$expecteds[] = '~@require-mysql';
switch ( $db_type ) {
case 'mariadb':
$expecteds[] = '~@require-mysql';
$expecteds[] = '~@require-sqlite';
break;
case 'sqlite':
$expecteds[] = '~@require-mariadb';
$expecteds[] = '~@require-mysql';
$expecteds[] = '~@require-mysql-or-mariadb';
break;
case 'mysql':
default:
$expecteds[] = '~@require-mariadb';
$expecteds[] = '~@require-sqlite';
break;
}

if ( ! extension_loaded( 'imagick' ) ) {
$expecteds[] = '~@require-extension-imagick';
}
Expand Down
22 changes: 15 additions & 7 deletions utils/behat-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,23 @@ function version_tags(
$skip_tags[] = '@broken-trunk';
}

if ( 'sqlite' === getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
$skip_tags[] = '@require-mysql';
switch ( getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
case 'mariadb':
$skip_tags[] = '@require-mysql';
$skip_tags[] = '@require-sqlite';
break;
case 'sqlite':
$skip_tags[] = '@require-mariadb';
$skip_tags[] = '@require-mysql';
$skip_tags[] = '@require-mysql-or-mariadb';
break;
case 'mysql':
default:
$skip_tags[] = '@require-mariadb';
$skip_tags[] = '@require-sqlite';
break;
}

if ( 'sqlite' !== getenv( 'WP_CLI_TEST_DBTYPE' ) ) {
$skip_tags[] = '@require-sqlite';
}


# Require PHP extension, eg 'imagick'.
function extension_tags( $features_folder = 'features' ) {
$extension_tags = array();
Expand Down