Skip to content

Commit

Permalink
fix for virtualenvs.create=false: GenericVenv, which is a subclass of…
Browse files Browse the repository at this point in the history
… VirtualEnv, does not necessarily have a pyvenv.cfg file
  • Loading branch information
radoering committed Nov 16, 2023
1 parent 8dbe41e commit 3cfdb05
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/poetry/utils/env/virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _updated_path(self) -> str:
@cached_property
def includes_system_site_packages(self) -> bool:
pyvenv_cfg = self._path / "pyvenv.cfg"
return (
return pyvenv_cfg.exists() and (
re.search(
r"^\s*include-system-site-packages\s*=\s*true\s*$",
pyvenv_cfg.read_text(),
Expand Down
34 changes: 27 additions & 7 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,30 +1373,50 @@ def test_venv_has_correct_paths(tmp_venv: VirtualEnv) -> None:
)


def test_env_system_packages(tmp_path: Path, poetry: Poetry) -> None:
@pytest.mark.parametrize("with_system_site_packages", [True, False])
def test_env_system_packages(
tmp_path: Path, poetry: Poetry, with_system_site_packages: bool
) -> None:
venv_path = tmp_path / "venv"
pyvenv_cfg = venv_path / "pyvenv.cfg"

EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
EnvManager(poetry).build_venv(
path=venv_path, flags={"system-site-packages": with_system_site_packages}
)
env = VirtualEnv(venv_path)

assert "include-system-site-packages = true" in pyvenv_cfg.read_text()
assert env.includes_system_site_packages
assert (
f"include-system-site-packages = {str(with_system_site_packages).lower()}"
in pyvenv_cfg.read_text()
)
assert env.includes_system_site_packages is with_system_site_packages


def test_generic_env_system_packages(poetry: Poetry) -> None:
"""https://github.com/python-poetry/poetry/issues/8646"""
env = GenericEnv(Path(sys.base_prefix))
assert not env.includes_system_site_packages


@pytest.mark.parametrize("with_system_site_packages", [True, False])
def test_env_system_packages_are_relative_to_lib(
tmp_path: Path, poetry: Poetry
tmp_path: Path, poetry: Poetry, with_system_site_packages: bool
) -> None:
venv_path = tmp_path / "venv"
EnvManager(poetry).build_venv(path=venv_path, flags={"system-site-packages": True})
EnvManager(poetry).build_venv(
path=venv_path, flags={"system-site-packages": with_system_site_packages}
)
env = VirtualEnv(venv_path)
site_dir = Path(site.getsitepackages()[-1])
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]
assert (
env.is_path_relative_to_lib(dist._path) # type: ignore[attr-defined]
is with_system_site_packages
)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 3cfdb05

Please sign in to comment.