diff --git a/bin/install-package-tests b/bin/install-package-tests index 27fb98de2..304744214 100755 --- a/bin/install-package-tests +++ b/bin/install-package-tests @@ -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() { @@ -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 diff --git a/features/steps.feature b/features/steps.feature index 3933aed46..d8ad66dc7 100644 --- a/features/steps.feature +++ b/features/steps.feature @@ -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)/ diff --git a/src/Context/FeatureContext.php b/src/Context/FeatureContext.php index 2dc4e3348..b8f4502fb 100644 --- a/src/Context/FeatureContext.php +++ b/src/Context/FeatureContext.php @@ -92,6 +92,11 @@ class FeatureContext implements SnippetAcceptingContext { */ 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. */ @@ -552,6 +557,7 @@ public static function prepare( BeforeSuiteScope $scope ) { self::log_run_times_before_suite( $scope ); } self::$behat_run_dir = getcwd(); + self::$mysql_binary = Utils\get_mysql_binary_path(); $result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check(); echo "{$result->stdout}\n"; @@ -603,6 +609,12 @@ public function beforeScenario( BeforeScenarioScope $scope ) { self::get_behat_internal_variables() ); + $mysql_binary = Utils\get_mysql_binary_path(); + $sql_dump_command = Utils\get_sql_dump_command(); + + $this->variables['MYSQL_BINARY'] = $mysql_binary; + $this->variables['SQL_DUMP_COMMAND'] = $sql_dump_command; + // 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 ); @@ -980,7 +992,7 @@ public function create_db() { } $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" ] ); } public function drop_db() { @@ -988,7 +1000,7 @@ public function drop_db() { 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" ] ); } public function proc( $command, $assoc_args = [], $path = '' ) { @@ -1149,7 +1161,7 @@ public function install_wp( $subdir = '' ) { // 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 ) { $this->create_db(); } $this->create_run_dir(); @@ -1179,7 +1191,7 @@ public function install_wp( $subdir = '' ) { 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*/ ); } } else { $this->proc( 'wp core install', $install_args, $subdir )->run_check(); @@ -1189,8 +1201,9 @@ public function install_wp( $subdir = '' ) { 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 ); $support_column_statistics = exec( "{$mysqldump_binary} --help | grep 'column-statistics'" ); $command = "{$mysqldump_binary} --no-defaults --no-tablespaces"; if ( $support_column_statistics ) { diff --git a/tests/tests/TestBehatTags.php b/tests/tests/TestBehatTags.php index d819a181e..115e7f899 100644 --- a/tests/tests/TestBehatTags.php +++ b/tests/tests/TestBehatTags.php @@ -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 ); @@ -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" ); } @@ -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'; } diff --git a/utils/behat-tags.php b/utils/behat-tags.php index e36a6e4fa..5571fe996 100644 --- a/utils/behat-tags.php +++ b/utils/behat-tags.php @@ -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();