Skip to content

Commit cc972cf

Browse files
Enable coverage tracking
- Only run coverage on one platform (Cython line tracing seems to have some issues with Python 3.13, to be determined) - There are a few paradoxical red spots in the Cython coverage analysis (code that must run for other blocks to be green, but nevertheless showing up as red) but the analysis does give a good indication of where the blind spots are already.
1 parent 20e3b48 commit cc972cf

6 files changed

Lines changed: 224 additions & 2 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Coverage
2+
on:
3+
pull_request: {}
4+
workflow_dispatch: {}
5+
env:
6+
UV_PYTHON_PREFERENCE: only-system
7+
UV_NO_SYNC: "1"
8+
PKCS11_TOKEN_LABEL: "TEST"
9+
PKCS11_TOKEN_PIN: "1234"
10+
PKCS11_TOKEN_SO_PIN: "5678"
11+
jobs:
12+
# For now, we run the coverage as a separate job.
13+
# At the time of writing, the latest version of Cython's line tracing
14+
# seems to lead to segfaults in Python 3.13 -> TODO: investigate
15+
pytest-coverage:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Acquire sources
19+
uses: actions/checkout@v4
20+
21+
- name: Setup Python
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: 3.12
25+
- uses: ./.github/actions/install-softhsm
26+
id: softhsm
27+
with:
28+
os: ubuntu-latest
29+
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
30+
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
31+
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
32+
- uses: ./.github/actions/install-opencryptoki
33+
# only run opencryptoki tests on ubuntu
34+
# (macos and windows don't seem to be supported)
35+
id: opencryptoki
36+
with:
37+
os: ubuntu-latest
38+
token-label: ${{ env.PKCS11_TOKEN_LABEL }}
39+
token-so-pin: ${{ env.PKCS11_TOKEN_SO_PIN }}
40+
token-user-pin: ${{ env.PKCS11_TOKEN_PIN }}
41+
- name: Install uv
42+
uses: astral-sh/setup-uv@v4
43+
with:
44+
enable-cache: true
45+
python-version: 3.12
46+
- name: Install testing dependencies
47+
run: uv sync --no-dev --exact --group coverage
48+
env:
49+
CFLAGS: "-DCYTHON_TRACE_NOGIL=1"
50+
EXT_BUILD_DEBUG: "1"
51+
- name: Run tests with SoftHSM
52+
run: uv run pytest -v --cov=pkcs11 --cov-branch --cov-report=xml:python-softhsm-coverage.xml
53+
env:
54+
PKCS11_MODULE: ${{ steps.softhsm.outputs.module }}
55+
- name: Run tests with opencryptoki
56+
run: uv run pytest -v --cov=pkcs11 --cov-branch --cov-report=xml:python-opencryptoki-coverage.xml
57+
env:
58+
PKCS11_MODULE: ${{ steps.opencryptoki.outputs.module }}
59+
# For testing logic around swapping PKCS#11 libs
60+
PKCS11_MODULE2: ${{ steps.softhsm.outputs.module }}
61+
- name: Stash coverage report
62+
uses: actions/upload-artifact@v4
63+
with:
64+
name: coverage
65+
path: "*-coverage.xml"
66+
codecov-upload:
67+
permissions:
68+
actions: write
69+
contents: read
70+
runs-on: ubuntu-latest
71+
needs: [pytest-coverage]
72+
steps:
73+
# checkout necessary to ensure the uploaded report contains the correct paths
74+
- uses: actions/checkout@v4
75+
- name: Retrieve coverage reports
76+
uses: actions/download-artifact@v4
77+
with:
78+
name: coverage
79+
path: ./reports/
80+
- name: Upload all coverage reports to Codecov
81+
uses: codecov/codecov-action@v5
82+
with:
83+
token: ${{ secrets.CODECOV_TOKEN }}
84+
directory: ./reports/
85+
flags: unittests
86+
env_vars: OS,PYTHON
87+
name: codecov-umbrella

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ __pycache__
66
/dist/
77
/docs/_build
88
/python_pkcs11.egg-info/
9-
/.eggs/
9+
/.eggs/
10+
.coverage
11+
*coverage.xml
12+
*.html

pkcs11/_pkcs11.pxd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Definitions imported from PKCS11 C headers.
44

55
from cython.view cimport array
66

7-
from pkcs11.defaults import *
87
from pkcs11.exceptions import *
98

109
cdef extern from '../extern/cryptoki.h':

pkcs11/_pkcs11.pyx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!python
22
#cython: language_level=3
3+
#cython: linetrace=True
34
"""
45
High-level Python PKCS#11 Wrapper.
56

pyproject.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,35 @@ archs = ["universal2"]
6767
[tool.setuptools.packages.find]
6868
include = ["pkcs11*"]
6969

70+
[tool.coverage.run]
71+
plugins = ["Cython.Coverage"]
72+
73+
[tool.coverage.report]
74+
exclude_lines = [
75+
"pragma: no cover",
76+
"pragma: nocover",
77+
"raise AssertionError",
78+
"raise NotImplementedError",
79+
"raise MemoryError",
80+
"raise TypeError",
81+
"TYPE_CHECKING",
82+
"^\\s*\\.\\.\\.",
83+
"noqa"
84+
]
85+
precision = 2
86+
7087
[dependency-groups]
7188
testing = [
7289
"cryptography>=44.0.0",
7390
"parameterized>=0.9.0",
7491
"pytest>=8.3.4",
7592
]
93+
coverage = [
94+
{ include-group = "testing" },
95+
"coverage>=7.9.1",
96+
"pytest-cov>=4.0,<6.3",
97+
"cython",
98+
]
7699
docs = [
77100
"sphinx>=7.4.7",
78101
"sphinx-rtd-theme>=3.0.2",

0 commit comments

Comments
 (0)