Skip to content

Commit 70201d7

Browse files
authored
Merge branch 'main' into copilot/enable-xdebug-in-behat-tests-again
2 parents ee5ccbc + 94ec63b commit 70201d7

32 files changed

Lines changed: 4131 additions & 30 deletions

‎.gherkin-lintrc‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"file-name": [
3+
"on",
4+
{
5+
"style": "kebab-case"
6+
}
7+
],
8+
"indentation": [
9+
"on",
10+
{
11+
"Feature": 0,
12+
"Background": 2,
13+
"Scenario": 2,
14+
"Examples": 4,
15+
"Step": 4,
16+
"given": 4,
17+
"example": 6,
18+
"and": 4
19+
}
20+
],
21+
"no-dupe-feature-names": "on",
22+
"no-dupe-scenario-names": "off",
23+
"no-empty-file": "on",
24+
"no-files-without-scenarios": "on",
25+
"no-multiple-empty-lines": "off",
26+
"no-partially-commented-tag-lines": "on",
27+
"no-trailing-spaces": "off",
28+
"no-unnamed-features": "on",
29+
"no-unnamed-scenarios": "on",
30+
"no-scenario-outlines-without-examples": "on",
31+
"use-and": "on"
32+
}

‎.github/actionlint.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Configuration for actionlint, run by the `actionlint` job in
2+
# `.github/workflows/reusable-code-quality.yml`.
3+
paths:
4+
.github/workflows/**/*.{yml,yaml}:
5+
ignore:
6+
# `copilot-requests` is a real permission scope - it is what lets a
7+
# workflow authenticate the Copilot CLI with the built-in `GITHUB_TOKEN`
8+
# instead of a personal access token - but actionlint's hard-coded scope
9+
# list has not caught up with it yet, so it reports every use as unknown.
10+
# Drop this entry once actionlint ships the scope, and it will go back to
11+
# catching genuine typos in permission names.
12+
# See https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli-in-actions
13+
- 'unknown permission scope "copilot-requests"'

‎.github/dependabot.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ updates:
99
- scope:distribution
1010
cooldown:
1111
default-days: 7
12+
# Only wp-cli-tests carries a package.json, holding the pinned version of the
13+
# Gherkin linter. This is a no-op in every other repository.
14+
- package-ecosystem: npm
15+
directory: "/"
16+
schedule:
17+
interval: daily
18+
open-pull-requests-limit: 10
19+
labels:
20+
- scope:distribution
21+
cooldown:
22+
default-days: 7
1223
- package-ecosystem: github-actions
1324
directory: "/"
1425
schedule:

‎.github/workflows/copilot-setup-steps.yml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ jobs:
1919
permissions:
2020
contents: read
2121

22+
env:
23+
MYSQL_HOST: 127.0.0.1
24+
MYSQL_TCP_PORT: 3306
25+
WP_CLI_TEST_DBROOTUSER: root
26+
WP_CLI_TEST_DBROOTPASS: root
27+
WP_CLI_TEST_DBNAME: wp_cli_test
28+
WP_CLI_TEST_DBUSER: wp_cli_test
29+
WP_CLI_TEST_DBPASS: password1
30+
WP_CLI_TEST_DBHOST: 127.0.0.1:3306
31+
2232
steps:
2333
- name: Checkout code
2434
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
@@ -45,3 +55,28 @@ jobs:
4555
uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0
4656
env:
4757
COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
58+
59+
# Without a database the Behat runner silently falls back to SQLite, so the
60+
# agent's test runs are not the ones CI will do. Mirrors the setup in
61+
# reusable-functional.yml.
62+
- name: Check existence of behat.yml file
63+
id: check_behat_file
64+
run: echo "files_exists=$(test -f behat.yml && echo true || echo false)" >> "$GITHUB_OUTPUT"
65+
66+
- name: Setup MySQL Server
67+
if: steps.check_behat_file.outputs.files_exists == 'true'
68+
uses: shogo82148/actions-setup-mysql@62da9377d83991fce27b6ed2a397d3306121e41d # v1
69+
with:
70+
mysql-version: '8.0' # Standard MySQL version for these tests
71+
auto-start: true
72+
root-password: ${{ env.WP_CLI_TEST_DBROOTPASS }}
73+
user: ${{ env.WP_CLI_TEST_DBUSER }}
74+
password: ${{ env.WP_CLI_TEST_DBPASS }}
75+
76+
- name: Remove system MySQL binary
77+
if: steps.check_behat_file.outputs.files_exists == 'true'
78+
run: sudo rm -f /usr/bin/mysql /usr/bin/mysqldump
79+
80+
- name: Prepare test database
81+
if: steps.check_behat_file.outputs.files_exists == 'true'
82+
run: composer prepare-tests

‎.github/workflows/issue-triage.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ permissions:
1717
issues: write
1818
pull-requests: write
1919
contents: read
20-
models: read
20+
# A caller can only cap a reusable workflow's permissions, so the scope the
21+
# Copilot CLI needs has to be granted here too. `models: read` was for the
22+
# GitHub Models provider, which `actions/ai-inference` v3 removed.
23+
copilot-requests: write
2124
# A caller can only cap a reusable workflow's permissions, never raise them,
2225
# so `actions: write` has to be granted here for the dispatch job downstream
2326
# to work at all. The reusable workflow narrows it to that single job, so the

‎.readme-partials/USING.md‎

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ To make use of the WP-CLI testing framework, you need to complete the following
1111
"behat": "run-behat-tests",
1212
"behat-rerun": "rerun-behat-tests",
1313
"lint": "run-linter-tests",
14+
"lint-gherkin": "run-gherkin-lint-tests",
1415
"phpcs": "run-phpcs-tests",
1516
"phpcbf": "run-phpcbf-cleanup",
17+
"phpstan": "run-phpstan-tests",
1618
"phpunit": "run-php-unit-tests",
1719
"prepare-tests": "install-package-tests",
1820
"test": [
1921
"@lint",
22+
"@lint-gherkin",
2023
"@phpcs",
24+
"@phpstan",
2125
"@phpunit",
2226
"@behat"
2327
]
@@ -76,7 +80,11 @@ To make use of the WP-CLI testing framework, you need to complete the following
7680
```
7781
7882
All other [PHPCS configuration options](https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset) are, of course, available.
79-
6. Update your composer dependencies and regenerate your autoloader and binary folders:
83+
The PHP snippets embedded in your feature files are checked along with the rest of the package. See [Checking the code style of the PHP blocks in feature files](#checking-the-code-style-of-the-php-blocks-in-feature-files) below.
84+
85+
6. Optionally add a `phpstan-feature-files.neon.dist` file to the package root to also run PHPStan over the PHP snippets embedded in your feature files. See [Analysing the PHP blocks in feature files](#analysing-the-php-blocks-in-feature-files) below.
86+
87+
7. Update your composer dependencies and regenerate your autoloader and binary folders:
8088
```bash
8189
composer update
8290
```
@@ -90,11 +98,128 @@ You can use the following commands to control the tests:
9098
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
9199
* `composer test` - Run all test suites.
92100
* `composer lint` - Run only the linting test suite.
101+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
93102
* `composer phpcs` - Run only the code sniffer test suite.
94103
* `composer phpcbf` - Run only the code sniffer cleanup.
104+
* `composer phpstan` - Run only the static analysis.
95105
* `composer phpunit` - Run only the unit test suite.
96106
* `composer behat` - Run only the functional test suite.
97107
108+
### Analysing the PHP blocks in feature files
109+
110+
Feature files embed PHP snippets in docstrings, which none of the static analysis tools normally
111+
look at:
112+
113+
```gherkin
114+
Given a wp-content/mu-plugins/test-harness.php file:
115+
"""
116+
<?php
117+
WP_CLI::add_command( 'test-harness', 'Test_Harness' );
118+
"""
119+
```
120+
121+
Adding a `phpstan-feature-files.neon.dist` file to the package root makes `composer phpstan` analyse
122+
those snippets as well. The blocks are extracted into standalone PHP files that are padded so their
123+
line numbers match the feature file, which is what allows errors to be reported against the feature
124+
file itself:
125+
126+
```text
127+
features/command.feature
128+
438 Parameter #1 $message of static method WP_CLI::log() expects string, int<0, max> given.
129+
🪪 argument.type
130+
```
131+
132+
The defaults in `phpstan/feature-files.neon` are applied first, so the file only needs to hold what
133+
it wants to change. An empty file is enough to run with the defaults, and a level of its own looks
134+
like this:
135+
136+
```neon
137+
parameters:
138+
level: 4
139+
```
140+
141+
Do note that snippets in feature files are fixtures, not production code, and that they run inside a
142+
WordPress installation the analysis knows nothing about. Expect to have to ignore errors that are
143+
not actually wrong, such as functions a scenario deliberately leaves undefined.
144+
145+
An ignore matches against the extracted file rather than the feature file it came from. Those files
146+
are named `<feature file>_L<first line>_E<last line>.php`, with the feature file relative to the
147+
`features` directory, so ignoring an error for a whole feature file takes a pattern:
148+
149+
```neon
150+
parameters:
151+
ignoreErrors:
152+
-
153+
identifier: function.notFound
154+
path: */shutdown-handler.feature_L*.php
155+
```
156+
157+
Since the blocks are analysed in more than one run (see below), an ignore that no run matches is not
158+
reported. A pattern that matches nothing at all therefore goes unnoticed, so it is worth checking
159+
that the error it targets is really gone.
160+
161+
Two kinds of blocks are left out of the analysis, and are listed at the end of the run:
162+
163+
* Blocks that are not standalone PHP, such as snippets holding a placeholder that Behat substitutes
164+
(`get_the_title( {POST_ID} )`) or code that is deliberately broken to test error handling. PHPStan
165+
stops analysing altogether when a single file fails to parse, so these have to be skipped.
166+
* Docstrings that neither belong to a step creating a `.php` file nor open with `<?php`, since those
167+
are not necessarily PHP at all. The second rule covers PHP files that are not named `*.php`, such
168+
as the `.maintenance` file of a WordPress installation.
169+
170+
Blocks that declare the same class or function as another block are analysed separately from each
171+
other, so that PHPStan does not resolve a name to the wrong block's declaration.
172+
173+
### Checking the code style of the PHP blocks in feature files
174+
175+
`composer phpcs` also checks the PHP snippets that feature files embed in docstrings, and
176+
`composer phpcbf` fixes them in place. No configuration is needed, and like the analysis above the
177+
blocks are padded so that findings are reported against the feature file itself:
178+
179+
```text
180+
FILE: features/command.feature
181+
----------------------------------------------------------------------
182+
FOUND 1 ERROR AFFECTING 1 LINE
183+
----------------------------------------------------------------------
184+
438 | ERROR | [x] Expected 1 space after IF keyword; 0 found
185+
----------------------------------------------------------------------
186+
```
187+
188+
Only a docstring belonging to a step that creates a `.php` file is checked:
189+
190+
```gherkin
191+
Given a wp-content/mu-plugins/test-harness.php file:
192+
"""
193+
<?php
194+
WP_CLI::add_command( 'test-harness', 'Test_Harness' );
195+
"""
196+
```
197+
198+
Unlike the analysis above, a docstring that merely opens with `<?php` does not count. Those are
199+
routinely an expectation about the contents of a file rather than a file, and reformatting one would
200+
make it stop matching what it is checked against.
201+
202+
The defaults leave out the sniffs that look at a block as if it were a file of its own, along with
203+
those that ask of a fixture what is only worth asking of production code. They live in
204+
`phpcs/feature-files.sh` and are shared by the check and the fixer, so that the two cannot disagree
205+
over which sniff applies. A package replaces them wholesale by adding a `phpcs-feature-files.xml`
206+
(or `phpcs-feature-files.xml.dist`) ruleset to its root:
207+
208+
```xml
209+
<?xml version="1.0"?>
210+
<ruleset name="WP-CLI-PROJECT-NAME-feature-files">
211+
<arg name="warning-severity" value="0"/>
212+
213+
<rule ref="WP_CLI_CS">
214+
<exclude name="Generic.Files.InlineHTML"/>
215+
<exclude name="Squiz.Commenting.FileComment"/>
216+
</rule>
217+
</ruleset>
218+
```
219+
220+
The blocks are left alone when a run is narrowed down to a path, as in `composer phpcs -- src/`,
221+
since such an argument is about the files of the package itself.
222+
98223
### Controlling what to test
99224

100225
To send one or more arguments to one of the test tools, prepend the argument(s) with a double dash. As an example, here's how to run the functional tests for a specific feature file only:
@@ -104,6 +229,53 @@ composer behat -- features/cli-info.feature
104229
105230
Prepending with the double dash is needed because the arguments would otherwise be sent to Composer itself, not the tool that Composer executes.
106231
232+
The same mechanism works for narrowing a run down further, or for bailing out early:
233+
```bash
234+
# A single scenario, identified by the line it starts on.
235+
composer behat -- features/cli-info.feature:12
236+
237+
# Every scenario carrying a given tag.
238+
composer behat -- --tags=@require-wp-5.0
239+
240+
# Stop at the first failing scenario instead of running the whole suite.
241+
composer behat -- --stop-on-failure
242+
243+
# Re-run only the scenarios that failed the last time.
244+
composer behat-rerun
245+
```
246+
247+
### Linting the feature files
248+
249+
`composer lint-gherkin` checks `features/` with
250+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
251+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
252+
different rules can override it by committing its own `.gherkin-lintrc`.
253+
254+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
255+
later. Where `npx` is not available the check reports that it is skipping, rather
256+
than failing a suite that is otherwise entirely PHP. Its version is pinned in
257+
this package's `package.json`, which exists only to hold that pin.
258+
259+
### Controlling the amount of output
260+
261+
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
262+
263+
* `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) turns off the ANSI color codes in the output of every runner. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
264+
* `WP_CLI_TEST_QUIET` switches the reporters to their most compact form: PHP_CodeSniffer reports one `file:line:col` line per violation with no progress ticker, PHPStan reports one `file:line:message` line per error with no progress bar and no result table. This covers the analysis of the PHP files themselves; the checks over the PHP blocks embedded in feature files keep their own reports, which are rewritten to point back at the feature file a block came from. Behat's own output is already minimal, so it is unaffected.
265+
266+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
267+
268+
```bash
269+
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
270+
```
271+
272+
This is worth setting permanently in environments that read the output back rather than display it, such as an AI coding agent's shell:
273+
274+
```bash
275+
export NO_COLOR=1
276+
export WP_CLI_TEST_QUIET=1
277+
```
278+
107279
### Controlling the test environment
108280

109281
#### WordPress Version
@@ -119,6 +291,13 @@ Here's how to run your tests against the latest trunk version of WordPress:
119291
WP_VERSION=trunk composer behat
120292
```
121293
294+
Resolving `latest`, or a `X.Y` version without a patch number, needs the
295+
WordPress versions data, which is fetched once and cached in the system temp
296+
directory for a day. Repeated runs do not repeat the request, and a run without
297+
connectivity falls back to the last known copy.
298+
`WP_CLI_TEST_WP_VERSION_CACHE_TTL` sets the lifetime of that cache in seconds;
299+
`0` fetches it every time.
300+
122301
#### WordPress Archive
123302
124303
Instead of downloading WordPress from WordPress.org, you can run the tests against an arbitrary
@@ -148,6 +327,20 @@ WP_VERSION=6.4.2 WP_CLI_TEST_CORE_ZIP=~/Downloads/wordpress-6.4.2.zip composer b
148327
Note that steps requesting an explicit version, such as `Given a WP 6.4.2 installation`, keep
149328
downloading that version from WordPress.org and ignore the archive.
150329
330+
#### Environment-specific scenarios
331+
332+
Some scenarios can only run in certain environments. Tagging them makes the test framework
333+
filter them out everywhere else, rather than having them fail for reasons unrelated to what
334+
they test.
335+
336+
* `@require-wp-stable` — the scenario needs a version of WordPress that WordPress.org knows
337+
about, such as one verifying an installation against the published checksums. It is
338+
skipped when `WP_CLI_TEST_CORE_ZIP` is set, and when `WP_VERSION` is `trunk` or `nightly`.
339+
* `@require-mysql-socket` — the scenario connects to the database through a socket. It is
340+
skipped when there is none, which is the case when the database server runs in a
341+
container and is only reachable over TCP. Set `WP_CLI_TEST_DBSOCKET` to point at the
342+
socket if it lives somewhere unusual.
343+
151344
#### WP-CLI Binary
152345
153346
You can run the tests against a specific WP-CLI binary, instead of using the one that has been built in your project's `vendor/bin` folder.

0 commit comments

Comments
 (0)