Skip to content

Commit 8fe2dcf

Browse files
committed
Add support for generating coverage reports for different Python versions and platforms
1 parent 20c8413 commit 8fe2dcf

File tree

2 files changed

+71
-8
lines changed

2 files changed

+71
-8
lines changed

coverage_pyver_pragma/grammar.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
#
120120

121121
# stdlib
122+
import os
122123
import platform
123124
import sys
124125

@@ -160,10 +161,14 @@
160161
# This ensures coverage.py records the correct coverage for these modules
161162
# when they are under test
162163

163-
for module in [m for m in sys.modules if m.startswith("domdf_python_tools")]:
164+
for module in [m for m in sys.modules if m.startswith("domdf_python_tools")]: # pragma: no cover (macOS)
164165
if module in sys.modules:
165166
del sys.modules[module]
166167

168+
PYTHON_VERSION = os.environ.get("COV_PYTHON_VERSION", '.'.join(platform.python_version_tuple()[:2]))
169+
PLATFORM = os.environ.get("COV_PLATFORM", platform.system()).casefold()
170+
PYTHON_IMPLEMENTATION = os.environ.get("COV_PYTHON_IMPLEMENTATION", platform.python_implementation()).casefold()
171+
167172

168173
@prettify_docstrings
169174
class VersionTag(packaging.specifiers.SpecifierSet):
@@ -208,8 +213,7 @@ def __repr__(self) -> str: # pragma: no cover
208213
return f"<{self.__class__.__name__}({str(self)!r})>"
209214

210215
def __bool__(self) -> bool:
211-
current_version = '.'.join(platform.python_version_tuple()[:2])
212-
return current_version in self
216+
return PYTHON_VERSION in self
213217

214218

215219
@prettify_docstrings
@@ -243,12 +247,10 @@ def __repr__(self) -> str: # pragma: no cover
243247
return f"<{self.__class__.__name__}({str(self)!r})>"
244248

245249
def __bool__(self) -> bool:
246-
current_platform = platform.system().casefold()
247-
248-
if not current_platform: # pragma: no cover
250+
if not PLATFORM: # pragma: no cover
249251
return True
250252

251-
return current_platform == self.casefold()
253+
return PLATFORM == self.casefold()
252254

253255

254256
@prettify_docstrings
@@ -280,7 +282,7 @@ def __repr__(self) -> str: # pragma: no cover
280282
return f"<{self.__class__.__name__}({str(self)!r})>"
281283

282284
def __bool__(self) -> bool:
283-
return platform.python_implementation().casefold() == self.casefold()
285+
return PYTHON_IMPLEMENTATION == self.casefold()
284286

285287

286288
@prettify_docstrings

doc-source/installation.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,64 @@ Alternatively you can put the configuration in the ``setup.cfg`` or ``tox.ini``
2525
[coverage:run]
2626
plugins =
2727
coverage_pyver_pragma
28+
29+
30+
Configuration
31+
-----------------
32+
33+
.. versionadded:: 0.3.0
34+
35+
When using Coverage.py's `reporting commands <https://coverage.readthedocs.io/en/coverage-5.5/cmd.html?highlight=report#coverage-summary-coverage-report>`_
36+
it may be desirable to generate a report for a different Python version / implementation / platform to the current one.
37+
For instance, you are generating a report from a .coverage file produced on PyPy 3.6 on Windows, but you are running CPython 3.8 on Linux.
38+
39+
``coverage_pyver_pragma`` provides three environment variables which can be used to set the target version and platform.
40+
41+
.. envvar:: COV_PYTHON_VERSION
42+
43+
Sets the Python version. Must be in the form :file:`{<major>}.{<minor>}`.
44+
45+
Defaults to the output of ``'.'.join(platform.python_version_tuple()[:2])``.
46+
47+
**Example:**
48+
49+
.. code-block:: bash
50+
51+
COV_PYTHON_VERSION=3.6 coverage report
52+
53+
.. envvar:: COV_PLATFORM
54+
55+
Sets the Python platform. Must be a string which matches the output of :func:`platform.system`.
56+
57+
Defaults to the output of :func:`platform.system`.
58+
59+
**Example:**
60+
61+
.. code-block:: bash
62+
63+
COV_PLATFORM=Windows coverage report
64+
65+
.. envvar:: COV_PYTHON_IMPLEMENTATION
66+
67+
Sets the Python implementation.
68+
Must be a string which matches the output of :func:`platform.python_implementation`.
69+
70+
Defaults to the output of :func:`platform.python_implementation`.
71+
72+
**Example:**
73+
74+
.. code-block:: bash
75+
76+
COV_PYTHON_IMPLEMENTATION=PyPy coverage report
77+
78+
If you generate your coverage reports through `tox <https://tox.readthedocs.io/en/latest/>`_
79+
you should configure `passenv <https://tox.readthedocs.io/en/latest/config.html?highlight=setenv#conf-passenv>`_
80+
to ensure the environment variables are passed through:
81+
82+
.. code-block:: ini
83+
84+
[testenv]
85+
passenv =
86+
COV_PYTHON_VERSION
87+
COV_PLATFORM
88+
COV_PYTHON_IMPLEMENTATION

0 commit comments

Comments
 (0)