Skip to content

Commit ed7ceb5

Browse files
authored
conftest.py: Make coverage optional (#2008)
This allows to execute the tests without coverage installed. We plan to do that in Fedora, where we run the tests, but not measure coverage.
1 parent 271dd73 commit ed7ceb5

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

tests/conftest.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from contextlib import contextmanager
99
from functools import partial
1010

11-
import coverage
1211
import pytest
1312
import six
1413

@@ -236,28 +235,31 @@ def no_coverage():
236235
pass
237236

238237

239-
class EnableCoverage(object):
240-
_COV_FILE = Path(coverage.__file__)
241-
_ROOT_COV_FILES_AND_FOLDERS = [i for i in _COV_FILE.parents[1].iterdir() if i.name.startswith("coverage")]
242-
243-
def __init__(self, link):
244-
self.link = link
245-
self.targets = []
246-
247-
def __enter__(self, creator):
248-
site_packages = creator.purelib
249-
for entry in self._ROOT_COV_FILES_AND_FOLDERS:
250-
target = site_packages / entry.name
251-
if not target.exists():
252-
clean = self.link(entry, target)
253-
self.targets.append((target, clean))
254-
return self
255-
256-
def __exit__(self, exc_type, exc_val, exc_tb):
257-
for target, clean in self.targets:
258-
if target.exists():
259-
clean()
260-
assert self._COV_FILE.exists()
238+
if COVERAGE_RUN:
239+
import coverage
240+
241+
class EnableCoverage(object):
242+
_COV_FILE = Path(coverage.__file__)
243+
_ROOT_COV_FILES_AND_FOLDERS = [i for i in _COV_FILE.parents[1].iterdir() if i.name.startswith("coverage")]
244+
245+
def __init__(self, link):
246+
self.link = link
247+
self.targets = []
248+
249+
def __enter__(self, creator):
250+
site_packages = creator.purelib
251+
for entry in self._ROOT_COV_FILES_AND_FOLDERS:
252+
target = site_packages / entry.name
253+
if not target.exists():
254+
clean = self.link(entry, target)
255+
self.targets.append((target, clean))
256+
return self
257+
258+
def __exit__(self, exc_type, exc_val, exc_tb):
259+
for target, clean in self.targets:
260+
if target.exists():
261+
clean()
262+
assert self._COV_FILE.exists()
261263

262264

263265
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)