@@ -19,6 +19,76 @@ if [[ "$@" == *"--help"* ]]; then
1919 exit $ret
2020fi
2121
22+ # POSIX compliant function to check if a string is numeric.
23+ is_numeric () {
24+ case $1 in
25+ ' ' |* [!0-9]* ) return 1;; # returns 1 if not numeric
26+ * ) return 0;; # returns 0 if numeric
27+ esac
28+ }
29+
30+ # If DB type is already set to SQLite, there's nothing to do.
31+ if [ " ${WP_CLI_TEST_DBTYPE-} " = " sqlite" ]; then
32+ echo " WP_CLI_TEST_DBTYPE is set to 'sqlite', skipping database check."
33+ else
34+ # Check for database client and connectivity.
35+ DB_CLIENT=" "
36+ if command -v mysql & > /dev/null; then
37+ DB_CLIENT=" mysql"
38+ elif command -v mariadb & > /dev/null; then
39+ DB_CLIENT=" mariadb"
40+ fi
41+
42+ if [ -z " ${DB_CLIENT} " ]; then
43+ echo " Warning: Could not find 'mysql' or 'mariadb' client."
44+ echo " The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
45+ export WP_CLI_TEST_DBTYPE=sqlite
46+ else
47+ HOST_STRING=' '
48+ if [ -n " ${WP_CLI_TEST_DBHOST} " ]; then
49+ case ${WP_CLI_TEST_DBHOST##* []]} in
50+ (* :* ) HOST=${WP_CLI_TEST_DBHOST%:* } PORT=${WP_CLI_TEST_DBHOST##*: } ;;
51+ (* ) HOST=${WP_CLI_TEST_DBHOST} ;;
52+ esac
53+ HOST_STRING=" -h${HOST} "
54+ if [ -n " ${PORT} " ]; then
55+ # If the port is not numeric, then we assume it is a socket path.
56+ if is_numeric " ${PORT} " ; then
57+ HOST_STRING=" ${HOST_STRING} --port=${PORT} --protocol=tcp"
58+ else
59+ HOST_STRING=" ${HOST_STRING} --socket=${PORT} --protocol=socket"
60+ fi
61+ fi
62+ fi
63+
64+ USER=${WP_CLI_TEST_DBUSER:- wp_cli_test}
65+
66+ if [ -z " ${WP_CLI_TEST_DBPASS+x} " ]; then
67+ # not set, use default
68+ PASSWORD=" password1"
69+ else
70+ # is set, use its value (could be empty)
71+ PASSWORD=" ${WP_CLI_TEST_DBPASS} "
72+ fi
73+
74+ PASSWORD_ARG=" "
75+ if [ -n " ${PASSWORD} " ]; then
76+ PASSWORD_ARG=" -p${PASSWORD} "
77+ fi
78+
79+ DBNAME=${WP_CLI_TEST_DBNAME:- wp_cli_test}
80+
81+ # We need to test the connection.
82+ # Let's try to connect to the specific test database.
83+ if ! ${DB_CLIENT} ${HOST_STRING} --user=" ${USER} " ${PASSWORD_ARG} --execute=" USE \` ${DBNAME} \` ;" 2> /dev/null; then
84+ echo " Warning: Could not connect to the MySQL/MariaDB database."
85+ echo " Please make sure the database is running and run 'composer prepare-tests' once to set it up."
86+ echo " The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
87+ export WP_CLI_TEST_DBTYPE=sqlite
88+ fi
89+ fi
90+ fi
91+
2292# Turn WP_VERSION into an actual number to make sure our tags work correctly.
2393if [ " ${WP_VERSION-latest} " = " latest" ]; then
2494 export WP_VERSION=$( curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r " .offers[0].current" )
0 commit comments