A command-line tool to format Gherkin .feature files.
Project description
Gherkin Formatter
A Python command-line utility to format Gherkin (.feature) files, similar to how black formats Python files. This tool helps maintain consistent styling in your BDD feature specifications, ensuring readability and uniformity across your projects.
Features
- Consistent Indentation: Applies uniform indentation for all Gherkin keywords (e.g.,
Feature,Scenario,Given,When,Then). - Keyword Alignment: Aligns keywords within step blocks (Given/When/Then) for improved readability.
- Flexible Tag Formatting: Supports single-line or multi-line tag styles.
- Data Table Formatting: Automatically aligns columns in Data Tables.
- DocString Formatting: Pretty-prints JSON and YAML content within DocStrings.
- Comprehensive Gherkin Support: Handles
Rule,Background,Scenario Outline,Examples, and comments. - Non-Destructive: Preserves all your data; only formatting is changed.
Installation
Install the Gherkin Formatter using pip:
pip install gherkin-formatter
Usage
To format your Gherkin files, run:
gherkin-formatter [OPTIONS] [FILES_OR_DIRECTORIES...]
Arguments:
FILES_OR_DIRECTORIES...: One or more.featurefiles or directories containing.featurefiles to format. The tool will recursively search for.featurefiles in directories.
Options:
--tab-width INTEGER: Number of spaces for indentation if not using tabs (default:2).--use-tabs: Indent using tabs instead of spaces. If set,--tab-widthis generally ignored for the indentation character.-a, --alignment [left|right]: Alignment for Gherkin keywords (e.g.,Given,When,Then) within a block of steps.left: Left-aligns keywords (default).right: Right-aligns keywords based on the longest keyword in the current block, providing a visually structured layout.
--multi-line-tags: Formats each tag on a new line, indented under the associated element. Default is single-line formatting where all tags appear on one line.--dry-run: Preview the changes that would be made without modifying the actual files. The formatted output (if different) will be printed to the console.--check: Check if files are formatted correctly according to the specified options. No files are changed.- Exits with code
0if all files are well-formatted. - Exits with code
1if one or more files would be reformatted or had file-specific processing issues (e.g., parsing errors). - Exits with code
123if an internal error occurred in the formatter.
- Exits with code
--version: Show the program's version number and exit.--help: Show this help message and exit.
Examples:
-
Format a single file with 4 spaces for indentation:
gherkin-formatter --tab-width 4 my_feature.feature
-
Check if all
.featurefiles in thefeatures/directory are formatted correctly:gherkin-formatter --check features/
-
Preview formatting for
my_feature.feature, aligning keywords to the right:gherkin-formatter --dry-run --alignment right my_feature.feature
-
Format a file using tabs for indentation and multi-line tags:
gherkin-formatter --use-tabs --multi-line-tags my_feature.feature
Using Gherkin Formatter as a pre-commit hook
You can use gherkin-formatter to automatically format your .feature files in your projects using pre-commit.
-
Install pre-commit If not already installed, you can install it with pip:
pip install pre-commit
-
Configure the pre-commit hook In your own project's repository, create or update the
.pre-commit-config.yamlfile with the following:# .pre-commit-config.yaml repos: - repo: https://github.com/musthafak/gherkin-formatter rev: <tag-or-sha> # e.g., v0.1.1 or a specific commit SHA hooks: - id: format-feature-files # You can override or add arguments here if needed: # args: [--tab-width, "4"]
Replace
<tag-or-sha>with the latest version tag from this repository or a specific commit SHA. -
Install the git hooks Navigate to your project's root directory and run:
pre-commit install
Now, gherkin-formatter will automatically run on your .feature files each time you make a commit, ensuring they are consistently formatted.
Development
This project uses pytest for running tests and pre-commit for code quality checks.
Setup:
After cloning the repository, it's recommended to use a virtual environment:
# Clone the repository (if you haven't already)
# git clone git@github.com:musthafak/gherkin-formatter.git
# cd gherkin-formatter
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -e .[dev]
pre-commit install # Installs pre-commit hooks for auto formatting and linting
Running Tests:
To execute the test suite:
pytest
Code Style & Linting:
This project uses ruff and pyrefly for formatting and linting, managed via pre-commit hooks.
Contributing
Contributions are welcome! If you'd like to contribute, please follow these general steps:
- Fork the repository and clone it to your local machine.
- Create a new branch for your feature or bug fix:
git checkout -b my-new-feature. - Make your changes and ensure they are well-tested.
- Ensure pre-commit checks pass:
pre-commit run --all-files. - Run tests using
pytestto confirm everything passes. - Commit your changes:
git commit -am 'Add some feature'. - Push to the branch:
git push origin my-new-feature. - Open a Pull Request on GitHub.
Please ensure your code adheres to existing styling and that any new functionality is covered by tests.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gherkin_formatter-0.1.2.tar.gz.
File metadata
- Download URL: gherkin_formatter-0.1.2.tar.gz
- Upload date:
- Size: 27.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0829564e11fc901d8b6b65c2ea5e0fd67deadbcf24c0551ccfe2473e77e64d09
|
|
| MD5 |
1a5e3e92eef42f427e0b7fe3b2360cc1
|
|
| BLAKE2b-256 |
fbc183e7c6f70394dabd970541ae91b8a0fae4afd8906831436bce25ed5fe188
|
Provenance
The following attestation bundles were made for gherkin_formatter-0.1.2.tar.gz:
Publisher:
publish-to-pypi.yml on musthafak/gherkin-formatter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gherkin_formatter-0.1.2.tar.gz -
Subject digest:
0829564e11fc901d8b6b65c2ea5e0fd67deadbcf24c0551ccfe2473e77e64d09 - Sigstore transparency entry: 264386987
- Sigstore integration time:
-
Permalink:
musthafak/gherkin-formatter@3ce86a497cf2d064ed46ab2d82c77531c3a6565b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/musthafak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@3ce86a497cf2d064ed46ab2d82c77531c3a6565b -
Trigger Event:
release
-
Statement type:
File details
Details for the file gherkin_formatter-0.1.2-py3-none-any.whl.
File metadata
- Download URL: gherkin_formatter-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
765c70d1b8125ef9ef04cabff63347ae49b0a56f920eee7261cb0dca8bf00339
|
|
| MD5 |
657240e5a9f65a37750ef46479b7eab7
|
|
| BLAKE2b-256 |
b7a3f97942e6f06c3e405f4a99537d76bf4c328703e378e18b57215448c05241
|
Provenance
The following attestation bundles were made for gherkin_formatter-0.1.2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on musthafak/gherkin-formatter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
gherkin_formatter-0.1.2-py3-none-any.whl -
Subject digest:
765c70d1b8125ef9ef04cabff63347ae49b0a56f920eee7261cb0dca8bf00339 - Sigstore transparency entry: 264386988
- Sigstore integration time:
-
Permalink:
musthafak/gherkin-formatter@3ce86a497cf2d064ed46ab2d82c77531c3a6565b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/musthafak
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@3ce86a497cf2d064ed46ab2d82c77531c3a6565b -
Trigger Event:
release
-
Statement type: