Metadata-Version: 2.4
Name: pytest-coverage-gate
Version: 1.1.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
License-File: AUTHORS.md
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 |

## Installation via PyPI

[![PyPI version](https://img.shields.io/pypi/v/pytest-coverage-gate.svg)](https://pypi.org/project/pytest-coverage-gate/)
[![Docs](https://github.com/python-by-kelsoncm/pytest-coverage-gate/actions/workflows/docs.yml/badge.svg)](https://python-by-kelsoncm.github.io/pytest-coverage-gate/)

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

## 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/python-by-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/python-by-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
```

## GitHub Action

Use the gate as a step in your workflow. It runs from the action's own checkout (standard library only, no `pip install`), so pinning `@v1` pins the gate version too.

```yaml
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: "3.x"
      - run: pip install pytest pytest-cov
      - run: pytest --cov=<your_package> --cov-report=xml

      - name: Coverage gate
        id: gate
        uses: python-by-kelsoncm/pytest-coverage-gate@v1
        # with:
        #   coverage-xml: src/coverage.xml
        #   baseline: src/.coverage-baseline
```

The step fails when coverage drops below the committed baseline and writes a summary table to the job page.

| Input | Default | Description |
|-------|---------|-------------|
| `coverage-xml` | `coverage.xml` | Path to the Cobertura report |
| `baseline` | `.coverage-baseline` | File holding the baseline percentage |
| `update-baseline` | `false` | When `true`, rewrites the baseline in the workspace if coverage improved |
| `python-version` | `3.x` | Python used to run the gate (3.9+) |

| Output | Description |
|--------|-------------|
| `current` | Current coverage percentage |
| `baseline` | Baseline percentage |
| `delta` | `current - baseline`, in percentage points (e.g. `+1.25`) |
| `status` | `ok`, `improved` or `fail` |

> **CI never rewrites the baseline by default** (`update-baseline: false`): a change made in the runner is lost when the job ends. To raise the baseline, run the hook/CLI locally (it updates `.coverage-baseline` for you) and commit the result, or set `update-baseline: true` and commit/open a PR from a later step.

## 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: coverage improved from 82.00% to 85.50%
```

```
[coverage-gate] FAIL: coverage regressed from 85.50% to 79.00%
```

Pass `--check-only` to never rewrite the baseline (used by the GitHub Action by default).

## License

MIT
