|
17 | 17 |
|
18 | 18 | from collections.abc import Callable, Iterable |
19 | 19 | from typing import cast |
| 20 | +from unittest import mock |
20 | 21 |
|
21 | 22 | import pytest |
22 | 23 |
|
@@ -184,6 +185,41 @@ def test_include_can_measure_stdlib(self) -> None: |
184 | 185 | _, statements, missing, _ = cov1.analysis("random.py") |
185 | 186 | assert statements == missing |
186 | 187 |
|
| 188 | + def test_stdlib_symlink(self) -> None: |
| 189 | + # Find the stdlib and make a symlink to it. |
| 190 | + import colorsys |
| 191 | + |
| 192 | + stdlib_dir = os.path.dirname(colorsys.__file__) |
| 193 | + os.symlink(stdlib_dir, "myliblink") |
| 194 | + |
| 195 | + sys.path.insert(0, os.path.abspath("myliblink")) |
| 196 | + del sys.modules["colorsys"] |
| 197 | + |
| 198 | + class FakeSysconfig: |
| 199 | + """A fake for the sysconfig module.""" |
| 200 | + |
| 201 | + def get_scheme_names(self) -> list[str]: # pylint: disable=missing-function-docstring |
| 202 | + return ["xyzzy"] |
| 203 | + |
| 204 | + def get_paths(self, _: str) -> dict[str, str]: # pylint: disable=missing-function-docstring |
| 205 | + return { |
| 206 | + "stdlib": os.path.abspath("myliblink"), |
| 207 | + } |
| 208 | + |
| 209 | + self.make_file( |
| 210 | + "mymain.py", |
| 211 | + """\ |
| 212 | + import colorsys |
| 213 | + hls = colorsys.rgb_to_hls(1, 0, 0) |
| 214 | + """, |
| 215 | + ) |
| 216 | + |
| 217 | + with mock.patch.object(coverage.inorout, "sysconfig", FakeSysconfig()): |
| 218 | + cov = coverage.Coverage() |
| 219 | + self.start_import_stop(cov, "mymain") |
| 220 | + files = cov.get_data().measured_files() |
| 221 | + assert not any(os.path.basename(f) == "colorsys.py" for f in files) |
| 222 | + |
187 | 223 | def test_exclude_list(self) -> None: |
188 | 224 | cov = coverage.Coverage() |
189 | 225 | cov.clear_exclude() |
|
0 commit comments