Skip to content

Commit 4322dee

Browse files
authored
Merge pull request #286 from wp-cli/fix/285-sqlite
2 parents 6d3fef9 + 240a89e commit 4322dee

File tree

1 file changed

+82
-12
lines changed

1 file changed

+82
-12
lines changed

bin/run-behat-tests

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,90 @@
33
# Run the Behat tests only if a Behat config file is found.
44
if [ ! -f "behat.yml" ]; then
55
echo 'Did not detect "behat.yml" file, skipping Behat tests.'
6-
exit 0;
6+
exit 0;
77
fi
88

99
if ! command -v jq &> /dev/null
1010
then
11-
echo 'The required "jq" command was not found, please install it to run the Behat tests.'
12-
echo "See https://stedolan.github.io/jq/download/ for installation instructions."
13-
exit 1;
11+
echo 'The required "jq" command was not found, please install it to run the Behat tests.'
12+
echo "See https://stedolan.github.io/jq/download/ for installation instructions."
13+
exit 1;
1414
fi
1515

1616
if [[ "$@" == *"--help"* ]]; then
17-
vendor/bin/behat "$@"
18-
ret=$?
19-
exit $ret
17+
vendor/bin/behat "$@"
18+
ret=$?
19+
exit $ret
20+
fi
21+
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
2090
fi
2191

2292
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
@@ -30,11 +100,11 @@ SOURCE="${BASH_SOURCE[0]}"
30100

31101
# Resolve $SOURCE until the file is no longer a symlink.
32102
while [ -h "$SOURCE" ]; do
33-
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
34-
SOURCE="$(readlink "$SOURCE")"
35-
# If $SOURCE was a relative symlink, we need to resolve it relative to the
36-
# path where the symlink file was located.
37-
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
103+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
104+
SOURCE="$(readlink "$SOURCE")"
105+
# If $SOURCE was a relative symlink, we need to resolve it relative to the
106+
# path where the symlink file was located.
107+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
38108
done
39109

40110
# Fetch the root folder of the WP-CLI tests package.

0 commit comments

Comments
 (0)