Skip to content

Commit

Permalink
Merge pull request #1290 from egbertbouman/coverage
Browse files Browse the repository at this point in the history
Added workflow for generating code coverage reports
  • Loading branch information
egbertbouman authored Mar 11, 2024
2 parents 52d5add + 80b3544 commit 7086f5f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
22 changes: 22 additions & 0 deletions .github/actions/coverage/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: 'Generate coverage report'
inputs:
html_report_name:
description: 'Artifact name for storing HTML coverage report. When omitted no report is stored.'
required: false
runs:
using: "composite"
steps:
- name: Generate coverage report
shell: bash
run: |
pip install coverage
python create_test_coverage_report.py
echo -e "<details><summary>Coverage report</summary>\n" >> $GITHUB_STEP_SUMMARY
cat coverage.md >> $GITHUB_STEP_SUMMARY
echo '</details>' >> $GITHUB_STEP_SUMMARY
- name: Upload coverage report
if: ${{ inputs.html_report_name }}
uses: actions/upload-artifact@v4
with:
name: ${{ inputs.html_report_name }}
path: coverage/
53 changes: 53 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Coverage
on:
push:
branches:
- master
workflow_dispatch:
jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.7'
cache: 'pip'
- run: python -m pip install -r requirements.txt
- uses: ./.github/actions/coverage
with:
html_report_name: coverage-linux
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.8'
cache: 'pip'
- uses: actions/cache/restore@v4
id: restore_cache
with:
path: libsodium.dll
key: cache_libsodium_dll
- run: python -m pip install -r requirements.txt
- uses: ./.github/actions/coverage
with:
html_report_name: coverage-windows
- uses: actions/cache/save@v4
if: steps.restore_cache.outputs.cache-hit != 'true'
with:
path: libsodium.dll
key: cache_libsodium_dll
macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
cache: 'pip'
- run: python -m pip install -r requirements.txt
- uses: ./.github/actions/coverage
with:
html_report_name: coverage-macos
6 changes: 3 additions & 3 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ jobs:
with:
python-version: '3.8'
cache: 'pip'
- uses: actions/cache/restore@v3
- uses: actions/cache/restore@v4
id: restore_cache
with:
path: libsodium.dll
key: cache_libsodium_dll
- run: python -m pip install -r requirements.txt
- name: Run unit tests
run: python run_all_tests.py -a
- uses: actions/cache/save@v3
id: save_cache
- uses: actions/cache/save@v4
if: steps.restore_cache.outputs.cache-hit != 'true'
with:
path: libsodium.dll
key: cache_libsodium_dll
Expand Down
11 changes: 10 additions & 1 deletion create_test_coverage_report.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import pathlib
import platform
import shutil
import sys
from io import StringIO
Expand All @@ -13,12 +14,16 @@
from coverage.results import Analysis
from packaging.version import Version

from run_all_tests import find_all_test_class_names
from run_all_tests import find_all_test_class_names, install_libsodium, windows_missing_libsodium

if __name__ != '__main__':
print(__file__, "should be run stand-alone! Instead, it is being imported!", file=sys.stderr) # noqa: T201
sys.exit(1)

if platform.system() == 'Windows' and windows_missing_libsodium():
print("Failed to locate libsodium (libnacl requirement), downloading latest dll!") # noqa: T201
install_libsodium()

data_file = os.path.join('coverage', 'raw', 'coverage_file')
logging.basicConfig(level=logging.CRITICAL)
logging.disable(logging.CRITICAL)
Expand Down Expand Up @@ -71,6 +76,10 @@ def clean_directory(prepare: bool = False) -> None:
print("Generating HTML report") # noqa: T201
cov.html_report(directory='coverage')

print("Generating markdown report") # noqa: T201
with open('coverage.md', 'w') as fp:
cov.report(output_format='markdown', file=fp, show_missing=True)

print("Aggregating package stats") # noqa: T201
total_numbers = {} # Package name -> (Numbers: package coverage stats, dict: files per coverage bin)
for filename in cov.get_data().measured_files():
Expand Down

0 comments on commit 7086f5f

Please sign in to comment.