Skip to content

Commit 5290b0f

Browse files
committed
Ensure source dirs are absolute
This `COV_CORE_SOURCE` environment variable is key for making sure that child processes continue computing code coverage. However, there's no guarantee that child processes start in the same directory as their parent process, which screws up coverage reporting if you're using relative paths for coverage sources. The fix is to make sure we're dealing with absolute paths. This is a little tricky to get right, because sources can include both dirs and packages. This fixes #465
1 parent 204af14 commit 5290b0f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: src/pytest_cov/engine.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ def set_env(self):
108108
if self.cov_source is None:
109109
os.environ['COV_CORE_SOURCE'] = os.pathsep
110110
else:
111-
os.environ['COV_CORE_SOURCE'] = os.pathsep.join(self.cov_source)
111+
os.environ['COV_CORE_SOURCE'] = os.pathsep.join(
112+
# We must make the paths absolute to ensure that subprocesses started in different
113+
# directories still find the same sources: https://github.com/nedbat/coveragepy/issues/1942.
114+
# Unfortunately, coverage.py doesn't provide a mechanism
115+
# for unambiguously specifying source_dirs, so we have to detect if a source is
116+
# a directory or a package by looking in the filesystem: https://github.com/nedbat/coveragepy/issues/1942.
117+
os.path.abspath(source) # noqa: PTH100 # see note below for why we're using `os.path.abspath`
118+
if Path(source).is_dir()
119+
else source
120+
for source in self.cov_source
121+
)
112122
config_file = Path(self.cov_config)
113123
if config_file.exists():
114124
os.environ['COV_CORE_CONFIG'] = os.fspath(config_file.resolve())

0 commit comments

Comments
 (0)