Metadata-Version: 2.4
Name: pytest-coverage-gate
Version: 1.0.0
Summary: A pre-commit hook that enforces a coverage quality gate using coverage.xml and a baseline file
License: MIT License
        
        Copyright (c) 2026 kelsoncm
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Keywords: coverage,pre-commit,quality-gate,pytest
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# pytest-coverage-gate

A [pre-commit](https://pre-commit.com/) hook that enforces a **coverage quality gate** for Python projects.

It reads the line-rate from `coverage.xml` (generated by `pytest-cov`) and compares it against a baseline stored in `.coverage-baseline`. If coverage regresses the commit is blocked. When coverage improves, the baseline is updated automatically.

## How it works

| Situation | Exit code | Message |
|-----------|-----------|---------|
| `coverage.xml` or `.coverage-baseline` missing | `1` | `[coverage-gate] ERROR: …` |
| Current coverage < baseline | `1` | `[coverage-gate] FAIL: …` |
| Current coverage == baseline | `0` | `[coverage-gate] OK: …` |
| Current coverage > baseline | `0` | `[coverage-gate] SUCC: …` — baseline file updated |

## Requirements

- Python ≥ 3.9
- A `coverage.xml` file produced by `pytest --cov --cov-report=xml`
- A `.coverage-baseline` file containing a single floating-point number (e.g. `85.00`)

## Setup

### 1. Generate `coverage.xml`

Add a step to your test run (locally or in CI) that produces `coverage.xml`:

```bash
pytest --cov=<your_package> --cov-report=xml
```

### 2. Create `.coverage-baseline`

Set your initial baseline (you can use the current coverage percentage):

```bash
echo "0.00" > .coverage-baseline
git add .coverage-baseline
```

> **Tip:** commit `.coverage-baseline` to version control so the gate is enforced for every contributor.

### 3. Add the hook to `.pre-commit-config.yaml`

```yaml
repos:
  - repo: https://github.com/kelsoncm/pytest-coverage-gate
    rev: v1.0.1   # use the latest tag
    hooks:
      - id: pytest-coverage-gate
```

If `coverage.xml` or `.coverage-baseline` are **not** in the repository root (e.g. they live under `src/`), pass custom paths via `args`:

```yaml
repos:
  - repo: https://github.com/kelsoncm/pytest-coverage-gate
    rev: v1.0.1
    hooks:
      - id: pytest-coverage-gate
        args:
          - --coverage-xml=src/coverage.xml
          - --baseline=src/.coverage-baseline
```

### 4. Run pre-commit

```bash
pre-commit run pytest-coverage-gate --all-files
```

Or it runs automatically on every `git commit` once pre-commit is installed:

```bash
pre-commit install
```

## Standalone usage

The script can also be run directly without pre-commit:

```bash
pip install pytest-coverage-gate
pytest-coverage-gate
```

Use `--coverage-xml` and `--baseline` to point to files in non-default locations:

```bash
pytest-coverage-gate --coverage-xml src/coverage.xml --baseline src/.coverage-baseline
```

## Example output

```
[coverage-gate] SUCC: cobertura evoluiu de atual 82.00% para 85.50%
```

```
[coverage-gate] FAIL: cobertura regrediu de atual 85.50% para 79.00%
```

## License

MIT
