Skip to content

Commit

Permalink
[#505] Added Gherkin linter for Brhat tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Aug 31, 2024
1 parent 7d5f966 commit 90cc688
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 11 deletions.
7 changes: 6 additions & 1 deletion .ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ commands:
lint:
usage: Lint back-end and front-end code.
cmd: ahoy lint-be && ahoy lint-fe
cmd: ahoy lint-be && ahoy lint-fe && lint-tests

lint-be:
usage: Lint back-end code.
Expand All @@ -195,6 +195,11 @@ commands:
ahoy cli vendor/bin/twig-cs-fixer lint
ahoy cli "npm run --prefix \${DREVOPS_WEBROOT}/themes/custom/\${DRUPAL_THEME} lint"
lint-tests:
usage: Lint tests code.
cmd: |
ahoy cli vendor/bin/gherkinlint lint tests/behat/features
lint-fix:
usage: Fix lint issues of back-end and front-end code.
cmd: ahoy lint-be-fix && ahoy lint-fe-fix
Expand Down
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ jobs:
name: Lint code with Twig CS Fixer
command: docker compose exec -T cli vendor/bin/twig-cs-fixer || [ "${DREVOPS_CI_TWIG_CS_FIXER_IGNORE_FAILURE:-0}" -eq 1 ]

- run:
name: Lint code with Gherkin Lint
command: docker compose exec -T cli vendor/bin/gherkinlint lint tests/behat/features || [ "${DREVOPS_CI_GHERKIN_LINT_IGNORE_FAILURE:-0}" -eq 1 ]

- run:
name: Lint code with NPM linters
command: docker compose exec -T cli bash -c "npm run --prefix \${DREVOPS_WEBROOT}/themes/custom/\${DRUPAL_THEME} lint" || [ "${DREVOPS_CI_NPM_LINT_IGNORE_FAILURE:-0}" -eq 1 ]
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ drush/contrib/
!.eslintrc.json
!.sass-lint.yml
!.twig-cs-fixer.php
!gherkinlint.json
!Gruntfile.js
!auth.json
!behat.yml
Expand Down
2 changes: 1 addition & 1 deletion .scaffold/.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ commands:
cmd: |
ahoy lint-scripts
ahoy lint-dockerfiles
ahoy lint-docs
ahoy test-docs
lint-scripts:
cmd: ./tests/lint.scripts.sh
Expand Down
3 changes: 3 additions & 0 deletions .scaffold/docs/.utils/variables/extra/ci.variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ DREVOPS_CI_TWIG_CS_FIXER_IGNORE_FAILURE=0
# Ignore NPM linters failures.
DREVOPS_CI_NPM_LINT_IGNORE_FAILURE=0

# Ignore Gherkin Lint failures.
DREVOPS_CI_GHERKIN_LINT_IGNORE_FAILURE=0

# Ignore PHPUnit test failures.
DREVOPS_CI_PHPUNIT_IGNORE_FAILURE=0

Expand Down
1 change: 1 addition & 0 deletions .scaffold/docs/content/tools/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Head over to the tool-specific documentation to learn more.
| [Docker](docker.mdx) | A platform for containerizing and running applications |
| [Doctor](doctor.mdx) | Check DrevOps Scaffold project requirements or print info |
| [Drush](drush.mdx) | Command line shell and Unix scripting interface for Drupal |
| [Gherkin Lint](gherkin-lint.mdx) | Provides a Gherkin linter for PHP |
| [Git artifact](git-artifact.mdx) | Package and push files to remote repositories |
| [PHPCS](phpcs.mdx) | Check that code adheres to coding standards |
| [PHPMD](phpmd.mdx) | Detect code smells and possible errors |
Expand Down
63 changes: 63 additions & 0 deletions .scaffold/docs/content/tools/gherkin-lint.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Gherkin Lint

https://github.com/dantleech/gherkin-lint-php

> Provides a Gherkin linter for PHP.
DrevOps Scaffold comes with [Gherkin Lint configuration](https://github.com/drevops/scaffold/blob/develop/gherkinlint.json) for Behat tests.

## Usage

```shell
vendor/bin/gherkinlint lint tests/behat/features
```
or
```shell
ahoy lint-tests
```

## Configuration

See [configuration reference](https://github.com/dantleech/gherkin-lint-php?tab=readme-ov-file#configuration) and [rules](https://github.com/dantleech/gherkin-lint-php/blob/master/docs/rules.md).

All global configuration takes place in the [`gherkinlint.json`](https://github.com/drevops/scaffold/blob/develop/gherkinlint.json) file.
The values are merged with the default configuration.

To check enabled rules, run

```shell
vendor/bin/gherkinlint rules
```

## Ignoring

Ignoring rules **globally** takes place in the [`gherkinlint.json`](https://github.com/drevops/scaffold/blob/develop/gherkinlint.json) file.
```json
{
"rules": {
"filename": {
"enabled": false
}
}
}
```

Gherkin Lint does not support ignoring of all rules in the file.

To ignore **a specific rule** within a file, place in the file header:
```yaml
# @gherkinlint-disable-rule keyword-order, someother-rule
```

Gherkin Lint does not support ignoring of the **code blocks**.

Gherkin Lint does not support ignoring rules on the **current line**.

Gherkin Lint does not support ignoring rules on the **next line**.

## Ignoring fail in CI

This tool runs in CI by default and fails the build if there are any violations.

Set `DREVOPS_CI_GHERKIN_LINT_IGNORE_FAILURE` environment variable to `1` to
ignore failures. The tool will still run and report violations, if any.
8 changes: 8 additions & 0 deletions .scaffold/docs/content/workflows/variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Default value: `UNDEFINED`

Defined in: `CI config`

### `DREVOPS_CI_GHERKIN_LINT_IGNORE_FAILURE`

Ignore Gherkin Lint failures.

Default value: `UNDEFINED`

Defined in: `CI config`

### `DREVOPS_CI_HADOLINT_IGNORE_FAILURE`

Ignore Hadolint failures.
Expand Down
3 changes: 2 additions & 1 deletion .scaffold/docs/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dealerdirect",
"drevops",
"drush",
"gherkinlint",
"hadolint",
"initialise",
"lagooncli",
Expand All @@ -46,8 +47,8 @@
"phpstorm",
"pyrech",
"ruleset",
"shellvar",
"sanitised",
"shellvar",
"standardise",
"utilising",
"vlucas",
Expand Down
5 changes: 3 additions & 2 deletions .scaffold/tests/bats/_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,13 @@ assert_files_present_drevops() {
assert_file_exists ".editorconfig"
assert_file_exists ".env"
assert_file_not_exists ".gitattributes"
assert_file_exists ".ahoy.local.example.yml"
assert_file_exists ".env.local.default"
assert_file_exists ".gitignore"
assert_file_exists "behat.yml"
assert_file_exists "composer.json"
assert_file_exists ".ahoy.local.example.yml"
assert_file_exists ".env.local.default"
assert_file_exists "docker-compose.yml"
assert_file_exists "gherkinlint.json"
assert_file_exists "phpcs.xml"
assert_file_exists "phpstan.neon"
assert_file_exists "phpunit.xml"
Expand Down
9 changes: 5 additions & 4 deletions .scaffold/tests/bats/_helper.deployment.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ assert_deployment_files_present() {
assert_dir_not_exists .docker
assert_dir_not_exists .github
assert_dir_not_exists .gitignore.artifact
assert_dir_not_exists .logs/screenshots
assert_dir_not_exists node_modules
assert_dir_not_exists patches
assert_dir_not_exists .logs/screenshots
assert_dir_not_exists tests
assert_file_not_exists .ahoy.yml
assert_file_not_exists .dockerignore
assert_file_not_exists .editorconfig
assert_file_not_exists .eslintrc.json
assert_file_not_exists .lagoon.yml
assert_file_not_exists .stylelintrc.json
assert_file_not_exists LICENSE
assert_file_not_exists README.md
assert_file_not_exists behat.yml
assert_file_not_exists composer.lock
assert_file_not_exists renovate.json
assert_file_not_exists docker-compose.yml
assert_file_not_exists LICENSE
assert_file_not_exists gherkinlint.json
assert_file_not_exists phpcs.xml
assert_file_not_exists phpstan.neon
assert_file_not_exists README.md
assert_file_not_exists renovate.json

assert_dir_exists scripts
assert_dir_exists vendor
Expand Down
18 changes: 18 additions & 0 deletions .scaffold/tests/bats/_helper.workflow.bash
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ assert_ahoy_lint() {

assert_ahoy_lint_be "${webroot}"
assert_ahoy_lint_fe "${webroot}"
assert_ahoy_lint_test
}

assert_ahoy_lint_be() {
Expand Down Expand Up @@ -391,6 +392,23 @@ assert_ahoy_lint_fe() {
assert_failure
}

assert_ahoy_lint_test() {
step "Run Test linter checks"

substep "Assert that Test lint works for Gherkin Lint"
run ahoy lint-test
assert_success

substep "Assert that Test lint failure works for Gherkin Lint"
echo "Feature:" >> "tests/behat/features/test.feature"
sync_to_container
run ahoy lint-test
assert_failure
rm -f "tests/behat/features/test.feature"
ahoy cli rm -f "tests/behat/features/test.feature"
sync_to_container
}

assert_ahoy_test() {
local webroot="${1:-web}"
local is_fast="${2:-0}"
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"require-dev": {
"behat/behat": "^3.14",
"dantleech/gherkin-lint": "^0.2.3",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"drevops/behat-format-progress-fail": "^1.2",
"drevops/behat-screenshot": "^1.5",
Expand Down
16 changes: 16 additions & 0 deletions gherkinlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"rules": {
"indentation": {
"width": 2
},
"keyword-order": {
"enabled": false
},
"no-homogenous-tags": {
"enabled": false
},
"scenario-size": {
"enabled": false
}
}
}
2 changes: 1 addition & 1 deletion tests/behat/features/homepage.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Homepage
Then I save screenshot

@api @javascript
Scenario: Anonymous user visits homepage
Scenario: Anonymous user visits homepage using a real browser
Given I go to the homepage
And I should be in the "<front>" path
Then I save screenshot
2 changes: 1 addition & 1 deletion tests/behat/features/login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Login
Then I save screenshot

@api @javascript
Scenario: Administrator user logs in
Scenario: Administrator user logs in using a real browser
Given I am logged in as a user with the "administer site configuration, access administration pages" permissions
When I go to "admin"
Then I save screenshot

1 comment on commit 90cc688

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.