From ef753c996d6af91ec2f4f2e4cd2863304f09828e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 18 Nov 2026 06:29:38 +0000 Subject: [PATCH] Derive MySQL host and port from `{DB_HOST}` in tests The parallel Docker workers of wp-cli-tests only receive `WP_CLI_TEST_DBHOST`, not `MYSQL_HOST` and `MYSQL_TCP_PORT`, so `{MYSQL_HOST}` and `{MYSQL_PORT}` stayed empty. The `DROP DATABASE` step then failed silently and `install-wp-tests.sh` stopped at its "reinstall?" prompt, and the `SHOW DATABASES` check failed with "Unknown server host '-P'". Use a small helper script that splits `{DB_HOST}` into host and port (or socket) instead, and prefers the `mariadb` client when available. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5pQ8Dw6arxXZ4GdZrc --- features/install-wp-tests.feature | 95 ++++++++++++++++++++++++--- features/scaffold-theme-tests.feature | 17 ++++- 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/features/install-wp-tests.feature b/features/install-wp-tests.feature index 6c076d14..c91bddf1 100644 --- a/features/install-wp-tests.feature +++ b/features/install-wp-tests.feature @@ -47,8 +47,23 @@ Feature: Scaffold install-wp-tests.sh tests And save STDOUT as {PHPUNIT_PHAR_URL} And I run `curl -sS -L --fail -o phpunit {PHPUNIT_PHAR_URL}` And I run `wp scaffold plugin hello-world` + And a mysql-client.sh file: + """ + # {DB_HOST} can be a host, a host:port or a host:/path/to/socket. + host={DB_HOST} + case "$host" in + *:/*) set -- -h"${host%%:*}" --socket="${host#*:}" "$@" ;; + *:*) set -- -h"${host%%:*}" -P"${host#*:}" --protocol=tcp "$@" ;; + *) set -- -h"$host" --protocol=tcp "$@" ;; + esac + client=mysql + if command -v mariadb > /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest` Then the return code should be 0 @@ -93,7 +108,7 @@ Feature: Scaffold install-wp-tests.sh tests """ # This throws a warning for the password provided via command line. - When I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "SHOW DATABASES"` + When I try `bash mysql-client.sh -e "SHOW DATABASES"` Then STDOUT should contain: """ wp_cli_test_scaffold @@ -163,8 +178,23 @@ Feature: Scaffold install-wp-tests.sh tests And save STDOUT as {PHPUNIT_PHAR_URL} And I run `curl -sS --fail -L -o phpunit {PHPUNIT_PHAR_URL}` And I run `wp scaffold plugin hello-world` + And a mysql-client.sh file: + """ + # {DB_HOST} can be a host, a host:port or a host:/path/to/socket. + host={DB_HOST} + case "$host" in + *:/*) set -- -h"${host%%:*}" --socket="${host#*:}" "$@" ;; + *:*) set -- -h"${host%%:*}" -P"${host#*:}" --protocol=tcp "$@" ;; + *) set -- -h"$host" --protocol=tcp "$@" ;; + esac + client=mysql + if command -v mariadb > /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest` Then the return code should be 0 @@ -209,7 +239,7 @@ Feature: Scaffold install-wp-tests.sh tests """ # This throws a warning for the password provided via command line. - When I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "SHOW DATABASES"` + When I try `bash mysql-client.sh -e "SHOW DATABASES"` Then STDOUT should contain: """ wp_cli_test_scaffold @@ -291,8 +321,23 @@ Feature: Scaffold install-wp-tests.sh tests And save STDOUT as {PHPUNIT_PHAR_URL} And I run `curl -sS -L --fail -o phpunit {PHPUNIT_PHAR_URL}` And I run `wp scaffold plugin hello-world` + And a mysql-client.sh file: + """ + # {DB_HOST} can be a host, a host:port or a host:/path/to/socket. + host={DB_HOST} + case "$host" in + *:/*) set -- -h"${host%%:*}" --socket="${host#*:}" "$@" ;; + *:*) set -- -h"${host%%:*}" -P"${host#*:}" --protocol=tcp "$@" ;; + *) set -- -h"$host" --protocol=tcp "$@" ;; + esac + client=mysql + if command -v mariadb > /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest` Then the return code should be 0 @@ -337,7 +382,7 @@ Feature: Scaffold install-wp-tests.sh tests """ # This throws a warning for the password provided via command line. - When I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "SHOW DATABASES"` + When I try `bash mysql-client.sh -e "SHOW DATABASES"` Then STDOUT should contain: """ wp_cli_test_scaffold @@ -399,8 +444,23 @@ Feature: Scaffold install-wp-tests.sh tests And save STDOUT as {PHPUNIT_PHAR_URL} And I run `curl -sS -f -L -o phpunit {PHPUNIT_PHAR_URL}` And I run `wp scaffold plugin hello-world` + And a mysql-client.sh file: + """ + # {DB_HOST} can be a host, a host:port or a host:/path/to/socket. + host={DB_HOST} + case "$host" in + *:/*) set -- -h"${host%%:*}" --socket="${host#*:}" "$@" ;; + *:*) set -- -h"${host%%:*}" -P"${host#*:}" --protocol=tcp "$@" ;; + *) set -- -h"$host" --protocol=tcp "$@" ;; + esac + client=mysql + if command -v mariadb > /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} trunk` Then the return code should be 0 @@ -458,7 +518,7 @@ Feature: Scaffold install-wp-tests.sh tests """ # This throws a warning for the password provided via command line. - When I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "SHOW DATABASES"` + When I try `bash mysql-client.sh -e "SHOW DATABASES"` Then STDOUT should contain: """ wp_cli_test_scaffold @@ -477,8 +537,23 @@ Feature: Scaffold install-wp-tests.sh tests Scenario: Install WordPress 3.7 and phpunit will not run Given a WP install And I run `wp scaffold plugin hello-world` + And a mysql-client.sh file: + """ + # {DB_HOST} can be a host, a host:port or a host:/path/to/socket. + host={DB_HOST} + case "$host" in + *:/*) set -- -h"${host%%:*}" --socket="${host#*:}" "$@" ;; + *:*) set -- -h"${host%%:*}" -P"${host#*:}" --protocol=tcp "$@" ;; + *) set -- -h"$host" --protocol=tcp "$@" ;; + esac + client=mysql + if command -v mariadb > /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` When I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress /usr/bin/env bash {PLUGIN_DIR}/hello-world/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} 3.7` Then the return code should be 0 @@ -526,7 +601,7 @@ Feature: Scaffold install-wp-tests.sh tests """ # This throws a warning for the password provided via command line. - When I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "SHOW DATABASES"` + When I try `bash mysql-client.sh -e "SHOW DATABASES"` And STDOUT should contain: """ wp_cli_test_scaffold diff --git a/features/scaffold-theme-tests.feature b/features/scaffold-theme-tests.feature index 9f31dd61..8d24ca32 100644 --- a/features/scaffold-theme-tests.feature +++ b/features/scaffold-theme-tests.feature @@ -74,8 +74,23 @@ Feature: Scaffold theme unit tests """ /dev/null 2>&1; then + client=mariadb + fi + "$client" -u{DB_USER} -p{DB_PASSWORD} "$@" + """ # This throws a warning for the password provided via command line. - And I try `mysql -u{DB_USER} -p{DB_PASSWORD} -h{MYSQL_HOST} -P{MYSQL_PORT} --protocol=tcp -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` + And I try `bash mysql-client.sh -e "DROP DATABASE IF EXISTS wp_cli_test_scaffold"` And I try `WP_TESTS_DIR={RUN_DIR}/wordpress-tests-lib WP_CORE_DIR={RUN_DIR}/wordpress {THEME_DIR}/t12child/bin/install-wp-tests.sh wp_cli_test_scaffold {DB_USER} {DB_PASSWORD} {DB_HOST} latest` Then the return code should be 0