|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -import contextlib |
4 | 3 | import os
|
5 | 4 | import re
|
6 |
| -import site |
7 | 5 | import subprocess
|
8 | 6 | import sys
|
9 | 7 |
|
@@ -297,16 +295,31 @@ def test_env_system_packages_are_relative_to_lib(
|
297 | 295 | path=venv_path, flags={"system-site-packages": with_system_site_packages}
|
298 | 296 | )
|
299 | 297 | env = VirtualEnv(venv_path)
|
300 |
| - site_dir = Path(site.getsitepackages()[-1]) |
| 298 | + |
| 299 | + # These are Poetry's own dependencies. |
| 300 | + # They should not be relative to the virtualenv's lib directory. |
301 | 301 | for dist in metadata.distributions():
|
302 |
| - # Emulate is_relative_to, only available in 3.9+ |
303 |
| - with contextlib.suppress(ValueError): |
304 |
| - dist._path.relative_to(site_dir) # type: ignore[attr-defined] |
305 |
| - break |
306 |
| - assert ( |
307 |
| - env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined] |
308 |
| - is with_system_site_packages |
309 |
| - ) |
| 302 | + assert not env.is_path_relative_to_lib( |
| 303 | + Path(str(dist._path)) # type: ignore[attr-defined] |
| 304 | + ) |
| 305 | + # Checking one package is sufficient |
| 306 | + break |
| 307 | + else: |
| 308 | + pytest.fail("No distributions found in Poetry's own environment") |
| 309 | + |
| 310 | + # These are the virtual environments' base env packages, |
| 311 | + # in this case the system site packages. |
| 312 | + for dist in metadata.distributions(path=[str(env.parent_env.site_packages.path)]): |
| 313 | + assert ( |
| 314 | + env.is_path_relative_to_lib( |
| 315 | + Path(str(dist._path)) # type: ignore[attr-defined] |
| 316 | + ) |
| 317 | + is with_system_site_packages |
| 318 | + ) |
| 319 | + # Checking one package is sufficient |
| 320 | + break |
| 321 | + else: |
| 322 | + pytest.fail("No distributions found in the base environment of the virtualenv") |
310 | 323 |
|
311 | 324 |
|
312 | 325 | @pytest.mark.parametrize(
|
|
0 commit comments