From a54be8fefa5e663cb8f87ea19bf3826abfc99858 Mon Sep 17 00:00:00 2001 From: Victor Miti Date: Sun, 4 Jun 2023 14:36:54 +0100 Subject: [PATCH] ci: generate coverage badge --- .github/workflows/main.yml | 45 ++++++++++++++++++++++++++++++++++++++ .gitignore | 1 + README.md | 1 + pyproject.toml | 9 ++++++++ requirements-dev.txt | 4 +++- tasks.py | 4 +++- 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 22aaa548..bae6bae1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -175,6 +175,51 @@ jobs: # Run tests invoke test + - name: "Upload coverage data" + uses: actions/upload-artifact@v3 + with: + name: covdata + path: .coverage.* + + coverage: + name: Coverage + needs: test + runs-on: ubuntu-22.04 + steps: + - name: Checkout Code Repository + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + cache: "pip" # caching pip dependencies + + - name: "Download coverage data" + uses: actions/download-artifact@v3 + with: + name: covdata + + - name: "Extract total coverage" + run: | + export TOTAL=$(python -c "import json;print(json.load(open('coverage.json'))['totals']['percent_covered_display'])") + echo "total=$TOTAL" >> $GITHUB_ENV + echo "### Total coverage: ${TOTAL}%" >> $GITHUB_STEP_SUMMARY + + - name: "Create coverage badge" + if: (github.repository == 'engineervix/zed-news') && (github.ref == 'refs/heads/main') + uses: schneegans/dynamic-badges-action@v1.6.0 + with: + # GIST_TOKEN is a GitHub personal access token with scope "gist". + auth: ${{ secrets.GIST_TOKEN }} + gistID: f4b1128b188c4e29722bc879e4ab6574 + filename: covbadge.json + label: Coverage + message: ${{ env.total }}% + minColorRange: 50 + maxColorRange: 90 + valColorRange: ${{ env.total }} + # Runs the next steps on the VM # Creates a GitHub Release when the lint & test jobs succeeds, and only on pushes to tags. release: diff --git a/.gitignore b/.gitignore index 24248b5f..4283193f 100644 --- a/.gitignore +++ b/.gitignore @@ -236,6 +236,7 @@ htmlcov/ .coverage.* nosetests.xml coverage.xml +coverage.json *.cover *.py,cover .hypothesis/ diff --git a/README.md b/README.md index 17baa3c4..8e8bbf74 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](https://forthebadge.com) [![CI/CD](https://github.com/engineervix/zed-news/actions/workflows/main.yml/badge.svg)](https://github.com/engineervix/zed-news/actions/workflows/main.yml) +[![Coverage](https://img.shields.io/endpoint?url=https://gist.github.com/engineervix/f4b1128b188c4e29722bc879e4ab6574/raw/covbadge.json)](https://github.com/engineervix/zed-news/actions?query=workflow%3A%22CI%2FCD%22) [![healthchecks.io](https://healthchecks.io/badge/24d88c7e-cc91-4dac-b9a5-d50e52/ewRXZ-TO/zed-news.svg)](https://healthchecks.io) [![python3](https://img.shields.io/badge/python-3.10-brightgreen.svg)](https://www.python.org/downloads/) diff --git a/pyproject.toml b/pyproject.toml index 9cd82fa3..947db260 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ dependencies = [ dev = [ "black", "commitizen", + "coverage[toml]", "DSLR", "flake8", "isort", @@ -80,6 +81,14 @@ known_first_party = "app" sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"] default_section = "THIRDPARTY" +[tool.coverage.run] +include = ["app/core/*"] +branch = true # Measure branch coverage + +[tool.coverage.report] +skip_covered = true +show_missing = true + [tool.commitizen] version_provider = "pep621" version_files = [ diff --git a/requirements-dev.txt b/requirements-dev.txt index df2ce224..17c40365 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -76,7 +76,9 @@ commitizen==3.2.2 commonmark==0.9.1 # via rich coverage[toml]==5.5 - # via eyed3 + # via + # eyed3 + # zed-news (pyproject.toml) curtsies==0.4.1 # via bpython cwcwidth==0.1.8 diff --git a/tasks.py b/tasks.py index 6ccf6e0f..c2e8970a 100644 --- a/tasks.py +++ b/tasks.py @@ -367,4 +367,6 @@ def toolchain(c): @task def test(c): """Run tests""" - c.run("python -m unittest discover app/tests", pty=True) + c.run("coverage run -m unittest discover app/tests", pty=True) + c.run("coverage json", pty=True) + c.run("coverage report -m", pty=True)