Skip to content

Weird-Sheep-Labs/coverage-pre-commit

Repository files navigation

coverage-pre-commit

pre-commit hooks to generate coverage XML and badge

by Weird Sheep Labs

Description

coverage-pre-commit exposes two pre-commit hooks:

  1. coverage-xml generates a coverage XML report.
  2. coverage-badge generates a code coverage badge using genbadge

This allows users to display a code coverage badge like Coverage Status in a repository README file without the need to rely on 3rd party tools.

Prerequisites

A .coverage file is expected at the root of the project, generated by running coverage run directly or using a plugin such as pytest-cov when running tests.

Important

Make sure your .coverage file is up to date, otherwise the generated badge will display an incorrect percentage.

Usage

Add the following to your .pre-commit-config.yaml file:

- repo: https://github.com/Weird-Sheep-Labs/coverage-pre-commit
    rev: 0.1.1
    hooks:
      - id: coverage-xml
      - id: coverage-badge

This will run the hooks with the default configuration, equivalent to:

- repo: https://github.com/Weird-Sheep-Labs/coverage-pre-commit
    rev: 0.1.1
    hooks:
      - id: coverage-xml
        args: ["-o", "reports/coverage/coverage.xml"]
      - id: coverage-badge
        args: ["-i", "reports/coverage/coverage.xml", "-o", "reports/coverage/coverage-badge.svg"]

Assuming you have used the default configuration, you can then display the coverage badge in your README.md:

![Coverage Status](./reports/coverage/coverage-badge.svg)

Important

The file paths to the generated XML file and badge must be trackable by git i.e not excluded in .gitignore.

Stages

In order to conform with the behaviour of the vast majority of pre-commit hooks, the tool will run on every available (installed) git hook type. However, in order to prevent frequent failed commits, it may be desirable to confine the tool execution hook type to git push by adding to each hook:

stage: ["pre-push"]

Note

You will have to run pre-commit install --hook-type pre-push to activate pre-push hooks in your local repo.

Why no coverage-run hook?

Because pre-commit runs each set of hooks in isolated environments, it does not have access to your local virtual environment. Running tests with coverage (using coverage run pytest for example) requires using the dependencies in the virtual environment, making it impossible to run through pre-commit.

About

pre-commit hooks to generate coverage XML and badge

Resources

License

Stars

Watchers

Forks

Packages

No packages published