Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env: fix check whether path is relative to system site packages #9861

Merged
Merged
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
4 changes: 1 addition & 3 deletions src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import os
import re
import sys

from contextlib import contextmanager
from copy import deepcopy
Expand All @@ -17,7 +16,6 @@
from poetry.utils.env.script_strings import GET_ENVIRONMENT_INFO
from poetry.utils.env.script_strings import GET_PATHS
from poetry.utils.env.script_strings import GET_SYS_PATH
from poetry.utils.env.system_env import SystemEnv


if TYPE_CHECKING:
Expand Down Expand Up @@ -145,5 +143,5 @@ def includes_system_site_packages(self) -> bool:
def is_path_relative_to_lib(self, path: Path) -> bool:
return super().is_path_relative_to_lib(path) or (
self.includes_system_site_packages
and SystemEnv(Path(sys.prefix)).is_path_relative_to_lib(path)
and self.parent_env.is_path_relative_to_lib(path)
)
35 changes: 24 additions & 11 deletions tests/utils/env/test_env.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

import contextlib
import os
import re
import site
import subprocess
import sys

Expand Down Expand Up @@ -297,16 +295,31 @@ def test_env_system_packages_are_relative_to_lib(
path=venv_path, flags={"system-site-packages": with_system_site_packages}
)
env = VirtualEnv(venv_path)
site_dir = Path(site.getsitepackages()[-1])

# These are Poetry's own dependencies.
# They should not be relative to the virtualenv's lib directory.
for dist in metadata.distributions():
# Emulate is_relative_to, only available in 3.9+
with contextlib.suppress(ValueError):
dist._path.relative_to(site_dir) # type: ignore[attr-defined]
break
assert (
env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]
is with_system_site_packages
)
assert not env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
)
# Checking one package is sufficient
break
else:
pytest.fail("No distributions found in Poetry's own environment")

# These are the virtual environments' base env packages,
# in this case the system site packages.
for dist in metadata.distributions(path=[str(env.parent_env.site_packages.path)]):
assert (
env.is_path_relative_to_lib(
Path(str(dist._path)) # type: ignore[attr-defined]
)
is with_system_site_packages
)
# Checking one package is sufficient
break
else:
pytest.fail("No distributions found in the base environment of the virtualenv")


@pytest.mark.parametrize(
Expand Down
Loading