-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrun-behat-tests
More file actions
executable file
·149 lines (129 loc) · 4.92 KB
/
run-behat-tests
File metadata and controls
executable file
·149 lines (129 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# Run the Behat tests only if a Behat config file is found.
if [ ! -f "behat.yml" ]; then
echo 'Did not detect "behat.yml" file, skipping Behat tests.'
exit 0;
fi
if ! command -v jq &> /dev/null
then
echo 'The required "jq" command was not found, please install it to run the Behat tests.'
echo "See https://stedolan.github.io/jq/download/ for installation instructions."
exit 1;
fi
if [[ "$@" == *"--help"* ]]; then
vendor/bin/behat "$@"
ret=$?
exit $ret
fi
# POSIX compliant function to check if a string is numeric.
is_numeric() {
case $1 in
''|*[!0-9]*) return 1;; # returns 1 if not numeric
*) return 0;; # returns 0 if numeric
esac
}
# If DB type is already set to SQLite, there's nothing to do.
if [ "${WP_CLI_TEST_DBTYPE-}" = "sqlite" ]; then
echo "WP_CLI_TEST_DBTYPE is set to 'sqlite', skipping database check."
else
# Check for database client and connectivity.
DB_CLIENT=""
if command -v mysql &> /dev/null; then
DB_CLIENT="mysql"
elif command -v mariadb &> /dev/null; then
DB_CLIENT="mariadb"
fi
if [ -z "${DB_CLIENT}" ]; then
echo "Warning: Could not find 'mysql' or 'mariadb' client."
echo "The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
export WP_CLI_TEST_DBTYPE=sqlite
else
HOST_STRING=''
if [ -n "${WP_CLI_TEST_DBHOST}" ]; then
case ${WP_CLI_TEST_DBHOST##*[]]} in
(*:*) HOST=${WP_CLI_TEST_DBHOST%:*} PORT=${WP_CLI_TEST_DBHOST##*:};;
(*) HOST=${WP_CLI_TEST_DBHOST};;
esac
HOST_STRING="-h${HOST}"
if [ -n "${PORT}" ]; then
# If the port is not numeric, then we assume it is a socket path.
if is_numeric "${PORT}"; then
HOST_STRING="${HOST_STRING} --port=${PORT} --protocol=tcp"
else
HOST_STRING="${HOST_STRING} --socket=${PORT} --protocol=socket"
fi
fi
fi
USER=${WP_CLI_TEST_DBUSER:-wp_cli_test}
if [ -z "${WP_CLI_TEST_DBPASS+x}" ]; then
# not set, use default
PASSWORD="password1"
else
# is set, use its value (could be empty)
PASSWORD="${WP_CLI_TEST_DBPASS}"
fi
PASSWORD_ARG=""
if [ -n "${PASSWORD}" ]; then
PASSWORD_ARG="-p${PASSWORD}"
fi
DBNAME=${WP_CLI_TEST_DBNAME:-wp_cli_test}
# We need to test the connection.
# Let's try to connect to the specific test database.
if ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="USE \`${DBNAME}\`;" 2>/dev/null; then
echo "Warning: Could not connect to the MySQL/MariaDB database."
echo "Please make sure the database is running and run 'composer prepare-tests' once to set it up."
echo "The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
export WP_CLI_TEST_DBTYPE=sqlite
fi
fi
fi
# Turn WP_VERSION into an actual number to make sure our tags work correctly.
if [ "${WP_VERSION-latest}" = "latest" ]; then
export WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current")
fi
# Normalize WP_VERSION=X.Y.0 to X.Y (WordPress uses X.Y for the initial release, not X.Y.0).
# If WP_VERSION=X.Y (major.minor only), resolve to the latest available patch release.
if [[ "${WP_VERSION}" =~ ^([0-9]+\.[0-9]+)\.0$ ]]; then
export WP_VERSION="${BASH_REMATCH[1]}"
elif [[ "${WP_VERSION}" =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_VERSIONS_JSON=$(curl -s https://raw.githubusercontent.com/wp-cli/wp-cli-tests/artifacts/wp-versions.json)
if [ -n "${WP_VERSIONS_JSON}" ]; then
RESOLVED_VERSION=$(echo "${WP_VERSIONS_JSON}" | jq -r --arg prefix "${WP_VERSION}." 'keys | map(select(startswith($prefix))) | sort_by(split(".") | map(tonumber)) | last // empty')
if [ -n "${RESOLVED_VERSION}" ]; then
export WP_VERSION="${RESOLVED_VERSION}"
fi
fi
fi
# To retrieve the WP-CLI tests package root folder, we start with this scripts
# location.
SOURCE="${BASH_SOURCE[0]}"
# Resolve $SOURCE until the file is no longer a symlink.
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# If $SOURCE was a relative symlink, we need to resolve it relative to the
# path where the symlink file was located.
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
# Fetch the root folder of the WP-CLI tests package.
WP_CLI_TESTS_ROOT="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
export WP_CLI_TESTS_ROOT
# Generate the tags to apply environment-specific filters.
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php)
BEHAT_EXTRA_ARGS=()
if [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]] && vendor/bin/behat --help 2>/dev/null | grep -q 'xdebug'; then
BEHAT_EXTRA_ARGS+=('--xdebug')
fi
# Run the functional tests.
FORMAT_SPECIFIED=false
for arg in "$@"; do
if [[ "$arg" == "--format"* ]]; then
FORMAT_SPECIFIED=true
break
fi
done
if [ "$FORMAT_SPECIFIED" = true ]; then
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"
else
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" --format progress "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"
fi