Skip to content

Commit 61e772d

Browse files
committed
Finishing tests
1 parent 00328e5 commit 61e772d

File tree

8 files changed

+363
-60
lines changed

8 files changed

+363
-60
lines changed

coverage_pyver_pragma/__init__.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#
2626

2727
# stdlib
28+
import platform
2829
import re
2930
import sys
3031
from typing import TYPE_CHECKING, Any, List, NamedTuple, Pattern, Union
@@ -35,7 +36,7 @@
3536
if TYPE_CHECKING:
3637

3738
# stdlib
38-
from sys import _version_info as VersionInfo
39+
from sys import _version_info as VersionInfo # pragma: no cover (typing only)
3940

4041
__author__: str = "Dominic Davis-Foster"
4142
__copyright__: str = "2020 Dominic Davis-Foster"
@@ -65,12 +66,16 @@ class Version(NamedTuple):
6566
serial: str
6667

6768

68-
def make_regexes(version_tuple: Union["Version", "VersionInfo"]) -> List[Pattern]:
69+
def make_regexes(version_tuple: Union["Version", "VersionInfo"], current_platform: str, current_implementation: str) -> List[Pattern]:
6970
"""
7071
Generate a list of regular expressions to match all valid ignores for the given Python version.
7172
7273
:param version_tuple: The Python version.
7374
:type version_tuple: :class:`~typing.NamedTuple` with the attributes ``major`` and ``minor``.
75+
:param current_platform:
76+
:type current_platform: str
77+
:param current_implementation:
78+
:type current_implementation: str
7479
7580
:return: List of regular expressions.
7681
"""
@@ -84,15 +89,19 @@ def make_regexes(version_tuple: Union["Version", "VersionInfo"]) -> List[Pattern
8489
less_equal_versions = [str(version_tuple.minor), *less_than_versions]
8590
exact_versions = [str(version_tuple.minor)]
8691

92+
wrong_platforms_string = fr"(?!.*!{current_platform})" # (?!.*Windows)(?!.*Darwin)
93+
wrong_implementations_string = fr"(?!.*!{current_implementation})" # (?!.*Windows)(?!.*Darwin)
94+
# correct_platforms_string = r"(?=\s*(Linux)?)"
95+
8796
# Add regular expressions for relevant python versions
8897
# We do it with re.compile to get the syntax highlighting in PyCharm
8998
excludes = [
90-
re.compile(fr"{regex_main}\s*\(<(py|PY|Py)3({'|'.join(less_than_versions)})\)"),
91-
re.compile(fr"{regex_main}\s*\(<=(py|PY|Py)3({'|'.join(less_equal_versions)})\)"),
92-
re.compile(fr"{regex_main}\s*\(>(py|PY|Py)3({'|'.join(greater_than_versions)})\)"),
93-
re.compile(fr"{regex_main}\s*\((py|PY|Py)3({'|'.join(greater_than_versions)})\+\)"),
94-
re.compile(fr"{regex_main}\s*\(>=(py|PY|Py)3({'|'.join(greater_equal_versions)})\)"),
95-
re.compile(fr"{regex_main}\s*\((py|PY|Py)3({'|'.join(exact_versions)})\)"),
99+
re.compile(fr"{regex_main}\s*\((?=\s*<(py|PY|Py)3({'|'.join(less_than_versions)})){wrong_platforms_string}{wrong_implementations_string}.*\)"),
100+
re.compile(fr"{regex_main}\s*\((?=\s*<=(py|PY|Py)3({'|'.join(less_equal_versions)})){wrong_platforms_string}{wrong_implementations_string}.*\)"),
101+
re.compile(fr"{regex_main}\s*\((?=\s*>(py|PY|Py)3({'|'.join(greater_than_versions)})){wrong_platforms_string}{wrong_implementations_string}.*\)"),
102+
re.compile(fr"{regex_main}\s*\((?=\s*(py|PY|Py)3({'|'.join(greater_equal_versions)})\+){wrong_platforms_string}{wrong_implementations_string}.*\)"),
103+
re.compile(fr"{regex_main}\s*\((?=\s*>=(py|PY|Py)3({'|'.join(greater_equal_versions)})){wrong_platforms_string}{wrong_implementations_string}.*\)"),
104+
re.compile(fr"{regex_main}\s*\((?=\s*(py|PY|Py)3({'|'.join(exact_versions)})){wrong_platforms_string}{wrong_implementations_string}.*\)"),
96105
]
97106

98107
return excludes
@@ -120,7 +129,7 @@ def configure(self, config: Any) -> None:
120129
# Remove standard "pragma: no cover" regex
121130
config.exclude_list.remove(regex_main)
122131

123-
excludes = make_regexes(sys.version_info)
132+
excludes = make_regexes(sys.version_info, platform.system(), platform.python_implementation())
124133
for exc_pattern in excludes:
125134
config.exclude_list.append(exc_pattern.pattern)
126135

doc-source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ Installation
148148
:maxdepth: 3
149149
:caption: Documentation
150150

151+
usage
151152
API Reference<docs>
152153
Source
153154
Building

doc-source/usage.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
=========
2+
Usage
3+
=========
4+
5+
The ``.coveragerc`` file in the repository root should contain the following options:
6+
7+
.. code-block:: ini
8+
9+
[run]
10+
plugins =
11+
coverage_pyver_pragma
12+
13+
14+
Alternatively you can put the configuration in the ``setup.cfg`` or ``tox.ini`` files like so:
15+
16+
.. code-block:: ini
17+
18+
[coverage:run]
19+
plugins =
20+
coverage_pyver_pragma
21+
22+
23+
Syntax
24+
--------
25+
26+
To ignore when running with versions of Python above, for instance, Python 3.6:
27+
28+
.. code-block:: python
29+
30+
# pragma: no cover (>py36)
31+
32+
You can also ignore for a specific version of Python:
33+
34+
.. code-block:: python
35+
36+
# pragma: no cover (py36)
37+
38+
Other examples:
39+
40+
.. code-block:: python
41+
42+
# pragma: no cover (<py38)
43+
# pragma: no cover (<=py38)
44+
# pragma: no cover (>=py38)
45+
# pragma: no cover (py38+)
46+
47+
48+
You can also exclude lines based on platform. For example, to exclude a line when the platform is not Windows:
49+
50+
.. code-block:: python
51+
52+
# pragma: no cover (!Windows)
53+
54+
You can also exclude lines based on Python implementation. For example, to exclude a line when the implementation is not CPython:
55+
56+
.. code-block:: python
57+
58+
# pragma: no cover (!CPython)
59+
60+
These can also be combined with the Python version:
61+
62+
.. code-block:: python
63+
64+
# pragma: no cover (<=py36 !Windows !CPython)
65+

exclude_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This file serves as a visual test that the conditional excludes are working correctly.
2+
3+
import sys
4+
5+
print("This line should be excluded on Windows") # pragma: no cover (!Linux !Darwin)
6+
7+
print("This line should be excluded on Linux") # pragma: no cover (!Windows !Darwin)
8+
9+
print("This line should be excluded on Darwin") # pragma: no cover (!Windows !Linux)
10+
11+
print("This line should be excluded on Windows and Linux") # pragma: no cover (!Darwin)
12+
13+
print("This line should be excluded on Windows and Darwin") # pragma: no cover (!Linux)
14+
15+
print("This line should be excluded on Linux and Darwin") # pragma: no cover (!Windows)
16+
17+
print("This line should be excluded on Python below 38") # pragma: no cover (<Py38)
18+
19+
print("This line should be excluded on Python above 35") # pragma: no cover (>=Py36)
20+
21+
print("This line should be excluded on Python above 35 on Windows and Darwin") # pragma: no cover (>=Py36 !Linux)
22+
23+
24+
def run():
25+
if sys.version_info < (3, 8): # pragma: no cover (PY38+)
26+
27+
for i in range(100):
28+
print(i)
29+
else: # pragma: no cover (<PY38)
30+
for i in range(100, 200):
31+
print(i)
32+
33+
34+
run()

tests/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
coverage >=5.1
2-
pytest >=5.1.1
2+
pytest >=6.0.0rc1
33
pytest-cov >=2.8.1
44
pytest-randomly >=3.3.1
55
pytest-rerunfailures >=9.0

tests/test_plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# stdlib
22
import sys
3+
import platform
34

45
# this package
56
from coverage_pyver_pragma import PyVerPragmaPlugin, make_regexes, not_version_regex, regex_main
@@ -15,4 +16,4 @@ def test_plugin():
1516
mock_config = MockConfig()
1617
PyVerPragmaPlugin().configure(mock_config)
1718

18-
assert mock_config.exclude_list == [p.pattern for p in make_regexes(sys.version_info)] + [not_version_regex]
19+
assert mock_config.exclude_list == [p.pattern for p in make_regexes(sys.version_info, platform.system(), platform.python_implementation())] + [not_version_regex]

0 commit comments

Comments
 (0)