pawamoy/copier-pdm

Feature: coverage badge

Opened this issue · 1 comments

  • add a docs macro that outputs a JSON badge:

    @env.macro
    def get_coverage():
        data = json.load(Path("htmlcov/coverage.json").read_text())
        value = int(data["totals"]["percent_covered"])
        for threshold, hex in (
            (95, "#4c1"),  # brightgreen
            (90, "#97CA00"),  # green
            (75, "#a4a61d"),  # yellowgreen
            (60, "#dfb317"),  # yellow
            (40, "#fe7d37"),  # orange
            (0, "#e05d44"),  # red
        ):
            if value >= threshold:
                color = hex
                break
        return {"value": value, "color": color}
  • add a templated file coverage_badge.json to the docs:

    {% with get_coverage() as coverage %}
    {"schemaVersion": 1, "label": "coverage", "message": "{{ coverage.value }}", "color": "{{ coverage.color }}"}
    {% endwith %}
    
  • add a badge to the readme pointing to that published JSON file:

    [![coverage](https://img.shields.io/endpoint?url=https://namespace.github.io/project/coverage_badge.json)](https://namespace.github.io/project/coverage)

Not possible to render a JSON file through MkDocs + macros unfortunately... Maybe I'll implement this in mkdocs-coverage.