diff options
author | Manuel Jacob <me@manueljacob.de> | 2023-02-21 03:03:55 +0100 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2023-03-15 05:48:29 -0400 |
commit | 4574ecf128ae51c2b950f6c9cb2486b86f5354e7 (patch) | |
tree | 80db11484a335c1d8ebc558492c0915a3db4d515 /tests | |
parent | dd7959dbf4eede4d2f2254f607113e45071cc8d3 (diff) | |
download | python-coveragepy-git-4574ecf128ae51c2b950f6c9cb2486b86f5354e7.tar.gz |
fix: don't measure all third-party packages if source is in third-party location
There is logic to not measure third-party packages inside configured sources. However, when a (i.e. another) configured source was inside a third-party location, this logic was previously disabled completely.
This caused a problem if a virtual env is set up inside a configured source directory and a configured source package gets installed inside the virtual env. Previously in this case, coverage was measured for all files in the virtual env for the reason described in the previous paragraph.
This commit changes the code to collect all configured source directories inside third-party locations and disable coverage for code in third-party locations only if its not in one of these collected source directories.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_venv.py | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/tests/test_venv.py b/tests/test_venv.py index de7ebbe1..ae5b303f 100644 --- a/tests/test_venv.py +++ b/tests/test_venv.py @@ -198,8 +198,28 @@ class VirtualenvTest(CoverageTest): with open("debug_out.txt") as f: return f.read() - def test_third_party_venv_isnt_measured(self, coverage_command: str) -> None: - out = run_in_venv(coverage_command + " run --source=. myproduct.py") + @pytest.mark.parametrize('install_source_in_venv', [True, False]) + def test_third_party_venv_isnt_measured( + self, coverage_command: str, install_source_in_venv: bool + ) -> None: + if install_source_in_venv: + make_file("setup.py", """\ + import setuptools + setuptools.setup( + name="myproduct", + py_modules = ["myproduct"], + ) + """) + try: + run_in_venv("python -m pip install .") + finally: + shutil.rmtree("build", ignore_errors=True) + shutil.rmtree("myproduct.egg-info", ignore_errors=True) + # Ensure that coverage doesn't run the non-installed module. + os.remove('myproduct.py') + out = run_in_venv(coverage_command + " run --source=.,myproduct -m myproduct") + else: + out = run_in_venv(coverage_command + " run --source=. myproduct.py") # In particular, this warning doesn't appear: # Already imported a file that will be measured: .../coverage/__main__.py assert out == self.expected_stdout @@ -213,7 +233,7 @@ class VirtualenvTest(CoverageTest): ) assert re_lines(r"^Tracing .*\bmyproduct.py", debug_out) assert re_lines( - r"^Not tracing .*\bcolorsys.py': falls outside the --source spec", + r"^Not tracing .*\bcolorsys.py': (module 'colorsys' |)?falls outside the --source spec", debug_out, ) |