Skip to content

Commit

Permalink
Add support for conditional coverage rules during unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Jun 11, 2024
1 parent 9a8d7a9 commit f121e9c
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
merge-multiple: true

- name: Generate Coverage Report
run: tox -e coverage-html-fail
run: tox -e coverage-html-fail-platform

- name: Upload HTML Coverage Report
uses: actions/upload-artifact@v4.3.3
Expand Down
1 change: 1 addition & 0 deletions changes/2640.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for conditional coverage based on the Python version was added for unit testing.
1 change: 1 addition & 0 deletions core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies = [
# ensure environment consistency.
dev = [
"coverage[toml] == 7.5.3",
"coverage-conditional-plugin == 0.9.0",
"Pillow == 10.3.0",
# Pre-commit 3.6.0 deprecated support for Python 3.8
"pre-commit == 3.5.0 ; python_version < '3.9'",
Expand Down
5 changes: 4 additions & 1 deletion core/src/toga/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def _package_version(file: Path | str | None, name: str) -> str:
# Excluded from coverage because a pure test environment (such as the one
# used by tox in CI) won't have setuptools_scm
return get_version(root="../../..", relative_to=file) # pragma: no cover
except (ModuleNotFoundError, LookupError):
except (
ModuleNotFoundError,
LookupError,
): # pragma: no-cover-if-missing-setuptools_scm
# If setuptools_scm isn't in the environment, the call to import will fail.
# If it *is* in the environment, but the code isn't a git checkout (e.g.,
# it's been pip installed non-editable) the call to get_version() will fail.
Expand Down
4 changes: 2 additions & 2 deletions core/src/toga/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import toga
from toga.platform import get_platform_factory

if sys.version_info >= (3, 10):
if sys.version_info >= (3, 10): # pragma: no-cover-if-lt-py310
from importlib.metadata import entry_points
else:
else: # pragma: no-cover-if-gte-py310
# Before Python 3.10, entry_points did not support the group argument;
# so, the backport package must be used on older versions.
from importlib_metadata import entry_points
Expand Down
4 changes: 2 additions & 2 deletions core/src/toga/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from functools import lru_cache
from types import ModuleType

if sys.version_info >= (3, 10): # pragma: no cover
if sys.version_info >= (3, 10): # pragma: no-cover-if-lt-py310
from importlib.metadata import entry_points
else: # pragma: no cover
else: # pragma: no-cover-if-gte-py310
# Before Python 3.10, entry_points did not support the group argument;
# so, the backport package must be used on older versions.
from importlib_metadata import entry_points
Expand Down
4 changes: 2 additions & 2 deletions core/src/toga/plugins/image_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
try:
import PIL.Image

PIL_imported = True
PIL_imported = True # pragma: no-cover-if-missing-PIL

except ImportError: # pragma: no cover
except ImportError: # pragma: no-cover-if-PIL-installed
PIL_imported = False


Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ignore-words-list = "te,crate,re-use,MapPin"
# with `coverage combine`.

[tool.coverage.run]
plugins = ["coverage_conditional_plugin"]
relative_files = true

[tool.coverage.report]
Expand All @@ -33,6 +34,14 @@ exclude_lines = [
"@overload",
]

[tool.coverage.coverage_conditional_plugin.rules]
no-cover-if-missing-setuptools_scm = "not is_installed('setuptools_scm')"
no-cover-if-missing-PIL = "not is_installed('PIL')"
no-cover-if-PIL-installed = "is_installed('PIL')"
no-cover-if-lt-py310 = "sys_version_info < (3, 10) and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"
no-cover-if-gte-py310 = "sys_version_info > (3, 10) and os_environ.get('COVERAGE_EXCLUDE_PYTHON_VERSION') != 'disable'"


[tool.isort]
profile = "black"
split_on_trailing_comma = true
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ labels =
test311 = py311-cov,coverage311
test312 = py312-cov,coverage312
test313 = py313-cov,coverage313
ci = towncrier-check,docs-lint,pre-commit,py{38,39,310,311,312}-cov,coverage
ci = towncrier-check,docs-lint,pre-commit,py{38,39,310,311,312}-cov,coverage-fail-platform
skip_missing_interpreters = True

[testenv:pre-commit]
Expand All @@ -42,7 +42,7 @@ commands =
!cov: python -m pytest {posargs:-vv --color yes}
cov : python -m coverage run -m pytest {posargs:-vv --color yes}

[testenv:coverage{,38,39,310,311,312,313}{,-html}{,-keep}{,-fail}]
[testenv:coverage{,38,39,310,311,312,313}{,-html}{,-keep}{,-fail}{,-platform}]
depends = pre-commit,py{,38,39,310,311,312,313}{,-cov}
changedir = core
skip_install = True
Expand All @@ -62,6 +62,8 @@ setenv =
fail: REPORT_FAIL_COND = --fail-under=100
CORE_RCFILE = --rcfile {tox_root}{/}core{/}pyproject.toml
PROJECT_RCFILE = --rcfile {tox_root}{/}pyproject.toml
# disable conditional coverage exclusions for Python version
{platform}: COVERAGE_EXCLUDE_PYTHON_VERSION=disable
commands_pre = python --version
commands =
-python -m coverage combine {env:CORE_RCFILE} {env:COMBINE_KEEP}
Expand Down

0 comments on commit f121e9c

Please sign in to comment.