Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/pytest_cov/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class CovController:
def __init__(self, options: argparse.Namespace, config: Union[None, object], nodeid: Union[None, str]):
"""Get some common config used by multiple derived classes."""
self.cov_source = options.cov_source
# An explicit --cov=... must narrow measurement to exactly what was asked for.
# Passing source_pkgs=[] overrides any source_pkgs in the coverage config, which
# would otherwise be added to the sources and widen the report again.
self.cov_source_pkgs = None if options.cov_source is None else []
self.cov_report = options.cov_report
self.cov_config = options.cov_config
self.cov_append = options.cov_append
Expand Down Expand Up @@ -236,6 +240,7 @@ class Central(CovController):
def start(self):
self.cov = coverage.Coverage(
source=self.cov_source,
source_pkgs=self.cov_source_pkgs,
branch=self.cov_branch,
data_suffix=True,
config_file=self.cov_config,
Expand All @@ -249,6 +254,7 @@ def start(self):

self.combining_cov = coverage.Coverage(
source=self.cov_source,
source_pkgs=self.cov_source_pkgs,
branch=self.cov_branch,
data_suffix=f'{filename_suffix(True)}.combine',
data_file=os.path.abspath(self.cov.config.data_file), # noqa: PTH100
Expand Down Expand Up @@ -286,6 +292,7 @@ class DistMaster(CovController):
def start(self):
self.cov = coverage.Coverage(
source=self.cov_source,
source_pkgs=self.cov_source_pkgs,
branch=self.cov_branch,
data_suffix=True,
config_file=self.cov_config,
Expand All @@ -301,6 +308,7 @@ def start(self):
self.cov._warn_preimported_source = False
self.combining_cov = coverage.Coverage(
source=self.cov_source,
source_pkgs=self.cov_source_pkgs,
branch=self.cov_branch,
data_suffix=f'{filename_suffix(True)}.combine',
data_file=os.path.abspath(self.cov.config.data_file), # noqa: PTH100
Expand Down Expand Up @@ -389,6 +397,7 @@ def start(self):
# Erase any previous data and start coverage.
self.cov = coverage.Coverage(
source=self.cov_source,
source_pkgs=self.cov_source_pkgs,
branch=self.cov_branch,
data_suffix=True,
config_file=self.cov_config,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_pytest_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ def test_central(pytester, testdir, prop):
assert result.ret == 0


def test_central_explicit_cov_overrides_source_pkgs(testdir):
testdir.tmpdir.join('pkga').ensure_dir().join('__init__.py').write('def fa():\n return 1\n')
testdir.tmpdir.join('pkgb').ensure_dir().join('__init__.py').write('def fb():\n return 2\n')
script = testdir.makepyfile("""
from pkga import fa
from pkgb import fb

def test_both():
assert fa() == 1
assert fb() == 2
""")
testdir.tmpdir.join('.coveragerc').write("""
[run]
source_pkgs =
pkga
pkgb
""")

result = testdir.runpytest('-v', '--cov=pkga', '--cov-report=term-missing', script)

result.stdout.fnmatch_lines(['*_ coverage: platform *, python * _*', 'pkga*100%*'])
assert not any(line.startswith('pkgb') for line in result.stdout.lines)
assert result.ret == 0


def test_annotate(testdir):
script = testdir.makepyfile(SCRIPT)

Expand Down