|
| 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 "$@" |
0 commit comments