Skip to content

Commit 855b91b

Browse files
committed
Merge branch 'feature/report-filtered-scenarios' into feature/declare-required-tools
# Conflicts: # phpcs.xml.dist
2 parents 7f8918f + 0a5404b commit 855b91b

10 files changed

Lines changed: 1169 additions & 19 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,39 @@ composer behat -- --stop-on-failure
254254
composer behat-rerun
255255
```
256256
257+
### Running scenarios in parallel
258+
259+
Set `WP_CLI_TEST_PARALLEL` to a number of workers to spread the scenarios over that many Behat processes:
260+
```bash
261+
WP_CLI_TEST_PARALLEL=4 composer behat
262+
```
263+
264+
Every worker but the first gets its own temporary directory, and with it its own home directory, caches and run directories, as well as its own test database, named after the test database with a numeric suffix (`wp_cli_test_1`, `wp_cli_test_2`, ...). Run `composer prepare-tests` once to allow the test user to create these databases. Without that permission, the scenarios are run one after the other, as they are on Windows, when collecting code coverage, or when selecting scenarios by path or name.
265+
266+
Scenarios that rely on resources shared across the system, such as a fixed port or a fixed path in the system's temporary directory, are all run by the first worker, one after the other. `composer behat-rerun` re-runs exactly the scenarios that failed in the last parallel run.
267+
268+
Set `WP_CLI_TEST_DOCKER` to run the scenarios in throwaway Docker containers instead, every worker in its own:
269+
```bash
270+
WP_CLI_TEST_DOCKER=auto WP_CLI_TEST_PARALLEL=4 composer behat
271+
```
272+
273+
| `WP_CLI_TEST_DOCKER` | Effect |
274+
|-------------------------------------|--------------------------------------------------------------------------------------------------|
275+
| not set (default), `0`, `no`, `off` | The scenarios run without Docker. |
276+
| `auto` | The scenarios run in containers where possible, and without Docker otherwise, which is reported. |
277+
| `1`, `yes`, `on` | The scenarios run in containers, and the run fails where that is not possible. |
278+
279+
Docker is not used on Windows, when collecting code coverage, when Docker is not available, when the image cannot be built or when the database server cannot be identified.
280+
281+
Each worker gets its own network, temporary directory and database server, of the same type and version as the one configured through the `WP_CLI_TEST_DB*` variables (or set the image through `WP_CLI_TEST_DOCKER_DB`, e.g. `mysql:8.0`, so that the host needs neither a database server nor a client). Nothing is shared between workers, so they can run any scenario. The database servers keep their data in memory. Scenarios selected by path or name run in a single container.
282+
283+
The containers run the same PHP version as the host, unless `WP_CLI_TEST_DOCKER_PHP` sets another one:
284+
```bash
285+
WP_CLI_TEST_DOCKER=1 WP_CLI_TEST_DOCKER_PHP=7.4 WP_CLI_TEST_PARALLEL=4 composer behat
286+
```
287+
288+
The project's Composer dependencies are then installed for that PHP version in a separate directory, the way CI installs them without a lock file, and refreshed once a day. Dependencies that are linked to local checkouts, as in `wp-cli/wp-cli-dev`, come from their published versions in that case, which is reported.
289+
257290
### Linting the feature files
258291
259292
`composer lint-gherkin` checks `features/` with

‎bin/install-package-tests‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ install_mysql_db_8_0_plus() {
150150
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
151151
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
152152
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153+
# The databases of parallel test workers, named after the test database with a numeric suffix.
154+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153155
{ set +ex; } 2> /dev/null # stop printing the commands
154156
}
155157

@@ -159,6 +161,8 @@ install_mysql_db_lower_than_8_0() {
159161
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
160162
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
161163
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
164+
# The databases of parallel test workers, named after the test database with a numeric suffix.
165+
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
162166
{ set +ex; } 2> /dev/null # stop printing the commands
163167
}
164168

@@ -169,6 +173,8 @@ install_mariadb() {
169173
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
170174
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
171175
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
176+
# The databases of parallel test workers, named after the test database with a numeric suffix.
177+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
172178
}
173179

174180
if [ "${TYPE}" = "MariaDB" ]; then

‎bin/run-behat-tests‎

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,50 @@ is_numeric() {
2727
esac
2828
}
2929

30+
# With WP_CLI_TEST_DOCKER set, the scenarios run in throwaway containers instead, every worker in
31+
# its own, with a database server of the same type and version, so they share nothing.
32+
# "auto" runs them without Docker where it cannot be used, "1" (or "yes", "true", "on") fails instead.
33+
case "${WP_CLI_TEST_DOCKER-}" in
34+
""|0|no|false|off) DOCKER="";;
35+
auto) DOCKER="auto";;
36+
1|yes|true|on) DOCKER="force";;
37+
*)
38+
echo "Warning: WP_CLI_TEST_DOCKER is neither \"auto\" nor a yes or no, running the scenarios without Docker." >&2
39+
DOCKER="";;
40+
esac
41+
42+
without_docker() {
43+
if [ "${DOCKER}" = "force" ]; then
44+
echo "Error: $1 Set WP_CLI_TEST_DOCKER to \"auto\" to run the scenarios without Docker in that case." >&2
45+
exit 1
46+
fi
47+
echo "$1 Running the scenarios without Docker." >&2
48+
DOCKER=""
49+
}
50+
51+
case "$(uname -s)" in
52+
MINGW*|MSYS*|CYGWIN*)
53+
if [ -n "${DOCKER}" ]; then
54+
without_docker "The scenarios cannot run in Docker containers on Windows."
55+
fi;;
56+
esac
57+
58+
if [ -n "${DOCKER}" ]; then
59+
if ! command -v docker > /dev/null 2>&1 || ! docker info > /dev/null 2>&1; then
60+
without_docker "Docker is not available."
61+
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
62+
without_docker "Code coverage cannot be collected in Docker containers."
63+
fi
64+
fi
65+
66+
export WP_CLI_TEST_DOCKER="${DOCKER}"
67+
3068
# If DB type is already set to SQLite, there's nothing to do.
69+
# Neither is there when the containers bring their own database server of a given image.
3170
if [ "${WP_CLI_TEST_DBTYPE-}" = "sqlite" ]; then
3271
echo "WP_CLI_TEST_DBTYPE is set to 'sqlite', skipping database check."
72+
elif [ -n "${DOCKER}" ] && [ -n "${WP_CLI_TEST_DOCKER_DB-}" ]; then
73+
echo "The database servers run in Docker containers of ${WP_CLI_TEST_DOCKER_DB}, skipping database check."
3374
else
3475
# Check for database client and connectivity.
3576
DB_CLIENT=""
@@ -41,10 +82,6 @@ else
4182

4283
if [ -z "${DB_CLIENT}" ]; then
4384
echo "Warning: Could not find 'mysql' or 'mariadb' client."
44-
if [ -n "${WP_CLI_TEST_STRICT_REQUIREMENTS-}" ]; then
45-
echo "Stopping, as WP_CLI_TEST_STRICT_REQUIREMENTS is set."
46-
exit 1
47-
fi
4885
echo "The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
4986
export WP_CLI_TEST_DBTYPE=sqlite
5087
else
@@ -83,20 +120,20 @@ else
83120
DBNAME=${WP_CLI_TEST_DBNAME:-wp_cli_test}
84121

85122
# We need to test the connection, and that the test user may create the test database.
86-
# The database itself does not need to exist, as the scenarios drop and create it.
123+
# The database itself does not need to exist, as the scenarios drop and create it, which
124+
# is also why a rerun after a run in parallel workers usually finds it missing.
87125
if ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="CREATE DATABASE IF NOT EXISTS \`${DBNAME}\`;" 2>/dev/null; then
88126
echo "Warning: Could not connect to the MySQL/MariaDB database."
89127
echo "Please make sure the database is running and run 'composer prepare-tests' once to set it up."
90-
if [ -n "${WP_CLI_TEST_STRICT_REQUIREMENTS-}" ]; then
91-
echo "Stopping, as WP_CLI_TEST_STRICT_REQUIREMENTS is set."
92-
exit 1
93-
fi
94128
echo "The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
95129
export WP_CLI_TEST_DBTYPE=sqlite
96130
fi
97131
fi
98132
fi
99133

134+
# The version as requested, before it is resolved below, for a run that starts over without Docker.
135+
export WP_CLI_TEST_REQUESTED_WP_VERSION="${WP_VERSION-}"
136+
100137
# When installing from an arbitrary archive, there is no version to resolve against
101138
# WordPress.org. Default to "trunk" so that no @require-wp tags are filtered out, as
102139
# such an archive is usually a development build. Set WP_VERSION explicitly alongside
@@ -257,8 +294,7 @@ WP_CLI_TESTS_ROOT="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
257294
export WP_CLI_TESTS_ROOT
258295

259296
# Generate the tags to apply environment-specific filters.
260-
# This also reports the scenarios that are left out, and stops when strict requirements are not met.
261-
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php) || exit $?
297+
BEHAT_TAGS=$(php "$WP_CLI_TESTS_ROOT"/utils/behat-tags.php)
262298

263299
BEHAT_EXTRA_ARGS=()
264300
if [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]] && vendor/bin/behat --help 2>/dev/null | grep -q 'xdebug'; then
@@ -279,4 +315,44 @@ for arg in "$@"; do
279315
fi
280316
done
281317

318+
# Run the scenarios in parallel workers when WP_CLI_TEST_PARALLEL is set to their number.
319+
# Every worker but the first uses its own test database, which the test user needs
320+
# to be allowed to create, as set up by "composer prepare-tests".
321+
PARALLEL="${WP_CLI_TEST_PARALLEL-}"
322+
323+
case "$(uname -s)" in
324+
MINGW*|MSYS*|CYGWIN*) PARALLEL="";;
325+
esac
326+
327+
328+
# A rerun after a parallel run covers exactly the scenarios that failed in it, which only the
329+
# parallel runner knows, whether or not WP_CLI_TEST_PARALLEL is set for the rerun as well.
330+
for arg in "$@"; do
331+
if [ "$arg" = "--rerun" ] && [ -f "${TMPDIR:-/tmp}/wp-cli-test-parallel-failures.scenarios" ]; then
332+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php 1 --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
333+
fi
334+
done
335+
336+
if [ -n "${DOCKER}" ]; then
337+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php "${PARALLEL:-1}" --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
338+
fi
339+
340+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
341+
if ! is_numeric "${PARALLEL}"; then
342+
echo "Warning: WP_CLI_TEST_PARALLEL is not a number of workers, running the scenarios one after the other." >&2
343+
PARALLEL=""
344+
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
345+
echo "Code coverage is collected, running the scenarios one after the other." >&2
346+
PARALLEL=""
347+
elif [ "${WP_CLI_TEST_DBTYPE-}" != "sqlite" ] && ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="CREATE DATABASE IF NOT EXISTS \`${DBNAME}_1\`; DROP DATABASE \`${DBNAME}_1\`;" 2>/dev/null; then
348+
echo "Warning: The test user cannot create the databases of parallel workers, running the scenarios one after the other." >&2
349+
echo "Run 'composer prepare-tests' once to allow it." >&2
350+
PARALLEL=""
351+
fi
352+
fi
353+
354+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
355+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php "${PARALLEL}" --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
356+
fi
357+
282358
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"

‎features/behat-steps.feature‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ Feature: Test that WP-CLI Behat steps work as expected
1919
Given an empty directory
2020
Then the {RUN_DIR} directory should exist
2121

22+
Scenario: Name the run directory after the scenario it belongs to
23+
Given an empty directory
24+
25+
When I run `echo {RUN_DIR}`
26+
Then STDOUT should match /wp-cli-test-run-behat-steps\.feature\.\d+-/
27+
2228
Scenario: Test "Given a specific directory" steps
2329
Given an empty directory
2430
And an empty test-dir directory
@@ -27,6 +33,30 @@ Feature: Test that WP-CLI Behat steps work as expected
2733
Given a non-existent test-dir directory
2834
Then the test-dir directory should not exist
2935

36+
Scenario: Always run the current version of files created by a scenario
37+
Given an empty directory
38+
And a script.php file:
39+
"""
40+
<?php echo 'first';
41+
"""
42+
43+
When I run `wp --require=script.php --skip-wordpress eval ";"`
44+
Then STDOUT should be:
45+
"""
46+
first
47+
"""
48+
49+
Given a script.php file:
50+
"""
51+
<?php echo 'other';
52+
"""
53+
54+
When I run `wp --require=script.php --skip-wordpress eval ";"`
55+
Then STDOUT should be:
56+
"""
57+
other
58+
"""
59+
3060
Scenario: Test "Given an empty cache" step
3161
Given a WP installation
3262
And an empty cache

‎phpcs.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@
7979
so this file does not have to comply with WP naming conventions. -->
8080
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
8181
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
82+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8283
<exclude-pattern>*/utils/check-required-tools\.php$</exclude-pattern>
8384
</rule>
8485
<rule ref="WordPress.WP.GlobalVariablesOverride">
8586
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
87+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8688
<exclude-pattern>*/utils/check-required-tools\.php$</exclude-pattern>
8789
</rule>
8890

0 commit comments

Comments
 (0)