Skip to content

Commit 1d6afbc

Browse files
committed
Add a Gherkin linter to the test suite
The feature files are linted on every pull request, but the check lives entirely in the reusable CI workflow and its ruleset lives in wp-cli/.github, so contributors cannot run it locally at all. A green `composer test` is therefore not a green build, and the way to find out is to push. Moves the check to where the other suites are: `.gherkin-lintrc` ships with this package as the shared default, a project can override it by committing its own, and `composer lint-gherkin` runs it. CI can then call the same script rather than reimplementing the invocation. Uses gherkin-lint-plus, pinned, and overridable through WP_CLI_TEST_GHERKIN_LINT_VERSION. Being a Node package, it is invoked through npx and skips with a message where npx is absent, rather than failing a suite that is otherwise entirely PHP. The linter colors its report unconditionally and offers no plain output format, so NO_COLOR strips the escape sequences from its output. Refs wp-cli/wp-cli#6161 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VcGmbu6CYzGGhP3jXuWDkJ
1 parent f38e2e6 commit 1d6afbc

5 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎.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+
}

‎.readme-partials/USING.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ You can use the following commands to control the tests:
9090
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
9191
* `composer test` - Run all test suites.
9292
* `composer lint` - Run only the linting test suite.
93+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
9394
* `composer phpcs` - Run only the code sniffer test suite.
9495
* `composer phpcbf` - Run only the code sniffer cleanup.
9596
* `composer phpunit` - Run only the unit test suite.
@@ -119,13 +120,27 @@ composer behat -- --stop-on-failure
119120
composer behat-rerun
120121
```
121122

123+
### Linting the feature files
124+
125+
`composer lint-gherkin` checks `features/` with
126+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
127+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
128+
different rules can override it by committing its own `.gherkin-lintrc`.
129+
130+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
131+
later. Where `npx` is not available the check reports that it is skipping, rather
132+
than failing a suite that is otherwise entirely PHP. Pin a different release of
133+
the linter with `WP_CLI_TEST_GHERKIN_LINT_VERSION`.
134+
122135
### Controlling the amount of output
123136

124137
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
125138

126139
- `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) stops the runners from forcing ANSI color codes on, and leaves the decision to each tool's own terminal detection. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
127140
- `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, and Behat stops printing step definition snippets for undefined steps.
128141
142+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
143+
129144
```bash
130145
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
131146
```

‎README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ You can use the following commands to control the tests:
101101
* `composer prepare-tests` - Set up the database that is needed for running the functional tests. This is only needed once.
102102
* `composer test` - Run all test suites.
103103
* `composer lint` - Run only the linting test suite.
104+
* `composer lint-gherkin` - Run only the Gherkin linter over the feature files.
104105
* `composer phpcs` - Run only the code sniffer test suite.
105106
* `composer phpcbf` - Run only the code sniffer cleanup.
106107
* `composer phpunit` - Run only the unit test suite.
@@ -130,13 +131,27 @@ composer behat -- --stop-on-failure
130131
composer behat-rerun
131132
```
132133

134+
### Linting the feature files
135+
136+
`composer lint-gherkin` checks `features/` with
137+
[gherkin-lint-plus](https://www.npmjs.com/package/gherkin-lint-plus), against the
138+
`.gherkin-lintrc` ruleset shipped with this package. A project that needs
139+
different rules can override it by committing its own `.gherkin-lintrc`.
140+
141+
The linter is a Node package, so it is run through `npx` and needs Node.js 20 or
142+
later. Where `npx` is not available the check reports that it is skipping, rather
143+
than failing a suite that is otherwise entirely PHP. Pin a different release of
144+
the linter with `WP_CLI_TEST_GHERKIN_LINT_VERSION`.
145+
133146
### Controlling the amount of output
134147

135148
Two environment variables make the test tools less chatty. Both are unset by default, which leaves the output exactly as it has always been.
136149

137150
- `NO_COLOR` (the [no-color.org](https://no-color.org/) convention) stops the runners from forcing ANSI color codes on, and leaves the decision to each tool's own terminal detection. Set this when capturing output to a file or a pipe, where the escape sequences are noise.
138151
- `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, and Behat stops printing step definition snippets for undefined steps.
139152
153+
`NO_COLOR` also covers the Gherkin linter, which colors its report unconditionally and has no plain output format of its own.
154+
140155
```bash
141156
NO_COLOR=1 WP_CLI_TEST_QUIET=1 composer phpstan
142157
```

‎bin/run-gherkin-lint-tests‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/sh
2+
3+
# Run the Gherkin linter only if there are feature files to lint.
4+
if [ ! -d "features" ]
5+
then
6+
exit 0;
7+
fi
8+
9+
# The linter is a Node package, which a PHP project cannot assume is present.
10+
# Skip rather than fail, the same way the Behat runner skips a package without a
11+
# behat.yml. The check still runs in CI, where Node is always available.
12+
if ! command -v npx > /dev/null 2>&1
13+
then
14+
echo 'Did not detect the "npx" command, skipping the Gherkin linting.'
15+
echo "It is part of Node.js 20 or later, see https://nodejs.org/ for installation instructions."
16+
exit 0;
17+
fi
18+
19+
# To retrieve the WP-CLI tests package root folder, we start with this scripts
20+
# location.
21+
SOURCE="$0"
22+
23+
# Resolve $SOURCE until the file is no longer a symlink.
24+
while [ -h "$SOURCE" ]; do
25+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
26+
SOURCE="$( readlink "$SOURCE" )"
27+
# If $SOURCE was a relative symlink, we need to resolve it relative to the
28+
# path where the symlink file was located.
29+
case $SOURCE in
30+
/*) ;;
31+
*) SOURCE="$DIR/$SOURCE";;
32+
esac
33+
done
34+
35+
# Fetch the root folder of the WP-CLI tests package.
36+
WP_CLI_TESTS_ROOT="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )"
37+
38+
# A project can override the shared ruleset by committing its own .gherkin-lintrc.
39+
CONFIG_FILE=".gherkin-lintrc"
40+
if [ ! -f "${CONFIG_FILE}" ]
41+
then
42+
CONFIG_FILE="${WP_CLI_TESTS_ROOT}/.gherkin-lintrc"
43+
fi
44+
45+
# Pinned so that a new release of the linter cannot turn a green build red
46+
# without a commit in this repository.
47+
GHERKIN_LINT_VERSION="${WP_CLI_TEST_GHERKIN_LINT_VERSION:-1.0.2}"
48+
49+
# Without an explicit path the linter walks the whole working directory, vendor
50+
# included. The feature files are the target.
51+
if [ $# -eq 0 ]
52+
then
53+
set -- features
54+
fi
55+
56+
run_linter() {
57+
npx --yes "gherkin-lint-plus@${GHERKIN_LINT_VERSION}" --config "${CONFIG_FILE}" "$@"
58+
}
59+
60+
if [ -n "${NO_COLOR}" ]
61+
then
62+
# The linter colors its report unconditionally: it honors neither NO_COLOR
63+
# nor the absence of a terminal, and "stylish" is its only output format.
64+
# It writes the report to STDERR, so strip the escape sequences from that
65+
# stream while leaving STDOUT and the exit code alone.
66+
STDERR_FILE=$( mktemp )
67+
68+
run_linter "$@" 2> "${STDERR_FILE}"
69+
STATUS=$?
70+
71+
if [ -s "${STDERR_FILE}" ]
72+
then
73+
ESC=$( printf '\033' )
74+
sed "s/${ESC}\[[0-9;]*m//g" "${STDERR_FILE}" >&2
75+
fi
76+
77+
rm -f "${STDERR_FILE}"
78+
79+
exit ${STATUS};
80+
fi
81+
82+
run_linter "$@"

‎composer.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"bin/install-package-tests",
8080
"bin/rerun-behat-tests",
8181
"bin/run-behat-tests",
82+
"bin/run-gherkin-lint-tests",
8283
"bin/run-linter-tests",
8384
"bin/run-php-unit-tests",
8485
"bin/run-phpcs-tests",
@@ -89,13 +90,15 @@
8990
"behat": "run-behat-tests",
9091
"behat-rerun": "rerun-behat-tests",
9192
"lint": "run-linter-tests",
93+
"lint-gherkin": "run-gherkin-lint-tests",
9294
"phpcs": "run-phpcs-tests",
9395
"phpcbf": "run-phpcbf-cleanup",
9496
"phpstan": "run-phpstan-tests",
9597
"phpunit": "run-php-unit-tests",
9698
"prepare-tests": "install-package-tests",
9799
"test": [
98100
"@lint",
101+
"@lint-gherkin",
99102
"@phpcs",
100103
"@phpstan",
101104
"@phpunit",

0 commit comments

Comments
 (0)