Skip to content

Commit

Permalink
Add background-setup-only rule (#64)
Browse files Browse the repository at this point in the history
* add background-setup-only rule

* add background-setup-only rule
  • Loading branch information
cjmarkham authored Sep 25, 2024
1 parent 93f7644 commit 85d8161
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 15 deletions.
27 changes: 27 additions & 0 deletions src/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ If a rule does not specify a severity, it will default to `warn`.
* [no-full-stop](#no-full-stop)
* [no-scenario-splat](#no-scenario-splat)
* [no-typographer-quotes](#no-typographer-quotes)
* [background-setup-only](#background-setup-only)

### Allowed Tags

Expand Down Expand Up @@ -500,3 +501,29 @@ export default {
}
}
```

### Background Setup Only

Background should be used for set up only, so should only include "Given" or splats (*).

**Examples**

Enable the rule

```typescript
export default {
rules: {
'background-setup-only': 'on',
}
}
```


Enable the rule and set severity
```typescript
export default {
rules: {
'background-setup-only': 'error',
}
}
```
35 changes: 35 additions & 0 deletions src/rules/background-setup-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { switchOrSeveritySchema } from '../schemas'
import Schema from '../schema'
import Rule from '../rule'
import { RawSchema, AcceptedSchema } from '../types'
import Document from '../document'

export default class BackgroundSetupOnly implements Rule {
public readonly name: string = 'background-setup-only'

public readonly acceptedSchema: AcceptedSchema = switchOrSeveritySchema

public readonly schema: Schema

public constructor(rawSchema: RawSchema) {
this.schema = new Schema(rawSchema)
}

public async run(document: Document): Promise<void> {
document.feature.children.forEach((child) => {
if (!child.background) {
return
}

child.background.steps.forEach((step): void => {
if (!['Given', '*'].includes(step.keyword.trim())) {
document.addError(
this.name,
`Background should only be used for set up. Found "${step.keyword.trim()}".`,
step.location,
)
}
})
})
}
}
2 changes: 1 addition & 1 deletion tests/acceptance/features/allowed-tags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Allowed Tags
When Gherklin is ran with the following configuration
| rules |
| {"allowed-tags": ["@development"]} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 1, "column": 1} | warn | allowed-tags | Found a feature tag that is not allowed. Got @invalid-tag, wanted @development |
Expand Down
34 changes: 34 additions & 0 deletions tests/acceptance/features/background-setup-only.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: Background Setup Only

Scenario: Invalid
Given the following feature file
"""
Feature: Invalid
Background: One
Given I set something up
Then something should be set up
And I confirm it was set up
"""
When Gherklin is ran with the following configuration
| rules |
| {"background-setup-only": "error"} |
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 4, "column": 5} | error | background-setup-only | Background should only be used for set up. Found "Then". |
| {"line": 5, "column": 5} | error | background-setup-only | Background should only be used for set up. Found "And". |

Scenario: Valid
Given the following feature file
"""
Feature: Valid
Background: One
Given I set something up
Scenario:
When I do something
Then something has been done
"""
When Gherklin is ran with the following configuration
| rules |
| {"background-setup-only": "error"} |
Then there are 0 files with errors
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: Keywords in Logical Order
When Gherklin is ran with the following configuration
| rules |
| {"keywords-in-logical-order": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 4, "column": 5} | warn | keywords-in-logical-order | Expected "When" to be followed by "Then", got "Given" |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/max-scenarios.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: Max Scenarios
When Gherklin is ran with the following configuration
| rules |
| {"max-scenarios": ["error", 1]} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 1, "column": 1} | error | max-scenarios | Expected max 1 scenarios per file. Found 2. |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-background-only.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: No Background Only
When Gherklin is ran with the following configuration
| rules |
| {"no-background-only": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 1, "column": 1} | warn | no-background-only | File contains only a background. |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-dupe-features.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: No Dupe Features
When Gherklin is ran with the following configuration
| rules |
| {"no-dupe-features": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 1, "column": 1} | warn | no-dupe-features | Found duplicate feature "Invalid" in "invalid-2.feature, invalid-1.feature". |
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-dupe-scenarios.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: No Dupe Scenarios
When Gherklin is ran with the following configuration
| rules |
| {"no-dupe-scenarios": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 3} | warn | no-dupe-scenarios | Found duplicate scenario "One" in "invalid.feature". |
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-empty-file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: No Empty File
When Gherklin is ran with the following configuration
| rules |
| {"no-empty-file": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 0, "column": 0} | warn | no-empty-file | Feature file is empty. |
4 changes: 2 additions & 2 deletions tests/acceptance/features/no-full-stop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Feature: No Full Stop
When Gherklin is ran with the following configuration
| rules |
| {"no-full-stop": "error"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 28} | error | no-full-stop | Line ends with a full stop. |
Expand All @@ -28,7 +28,7 @@ Feature: No Full Stop
When Gherklin is ran with the following configuration
| rules |
| {"no-full-stop": "error"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 28} | error | no-full-stop | Line ends with a full stop. |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-scenario-splat.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Feature: No Scenario Splat
When Gherklin is ran with the following configuration
| rules |
| {"no-scenario-splat": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 4, "column": 5} | warn | no-scenario-splat | Found a splat (*) inside a scenario. |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-similar-scenarios.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Feature: No Similar Scenarios
When Gherklin is ran with the following configuration
| rules |
| {"no-similar-scenarios": ["error", 90]} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 3} | error | no-similar-scenarios | Scenario "Scenario 1" is too similar (> 90%) to scenario "Scenario 2". |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: No Single Example Outline
When Gherklin is ran with the following configuration
| rules |
| {"no-single-example-outline": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 3} | warn | no-single-example-outline | Scenario Outline has only one example. Consider converting to a simple Scenario. |
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-trailing-spaces.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Feature: No Trailing Spaces
When Gherklin is ran with the following configuration
| rules |
| {"no-trailing-spaces": "error"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 25} | error | no-trailing-spaces | Found trailing whitespace. |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-typographer-quotes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: No Typographer Quotes
When Gherklin is ran with the following configuration
| rules |
| {"no-typographer-quotes": "error"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 3, "column": 15} | error | no-typographer-quotes | Found typographer quote |
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/features/no-unnamed-scenarios.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: No Unnamed Scenarios
When Gherklin is ran with the following configuration
| rules |
| {"no-unnamed-scenarios": "on"} |
Then there is 1 files with errors
Then there is 1 file with errors
And the errors are
| location | severity | rule | message |
| {"line": 2, "column": 3} | warn | no-unnamed-scenarios | Found scenario with no name. |

0 comments on commit 85d8161

Please sign in to comment.