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

fix detection of system environment #8970

Merged
merged 3 commits into from
Feb 16, 2024
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
12 changes: 1 addition & 11 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,17 +728,7 @@ def get_system_env(cls, naive: bool = False) -> Env:
prefix, base_prefix = Path(sys.prefix), Path(cls.get_base_prefix())
env: Env = SystemEnv(prefix)
if not naive:
if prefix.joinpath("poetry_env").exists():
env = GenericEnv(base_prefix, child_env=env)
else:
from poetry.locations import data_dir

try:
prefix.relative_to(data_dir())
except ValueError:
pass
else:
env = GenericEnv(base_prefix, child_env=env)
env = GenericEnv(base_prefix, child_env=env)

return env

Expand Down
38 changes: 21 additions & 17 deletions tests/utils/env/test_env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from poetry.core.constraints.version import Version

from poetry.toml.file import TOMLFile
from poetry.utils.env import GET_BASE_PREFIX
from poetry.utils.env import GET_PYTHON_VERSION_ONELINER
from poetry.utils.env import EnvManager
from poetry.utils.env import IncorrectEnvError
Expand Down Expand Up @@ -68,7 +69,7 @@ def check_output(cmd: list[str], *args: Any, **kwargs: Any) -> str:
return f"/usr/bin/{basename}"

if "print(sys.base_prefix)" in python_cmd:
return "/usr"
return sys.base_prefix

assert "import sys; print(sys.prefix)" in python_cmd
return "/prefix"
Expand Down Expand Up @@ -140,7 +141,7 @@ def test_activate_in_project_venv_no_explicit_config(
env = manager.activate("python3.7")

assert env.path == tmp_path / "poetry-fixture-simple" / ".venv"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)

m.assert_called_with(
tmp_path / "poetry-fixture-simple" / ".venv",
Expand Down Expand Up @@ -196,7 +197,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
assert envs[venv_name]["patch"] == "3.7.1"

assert env.path == tmp_path / f"{venv_name}-py3.7"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)


def test_activate_fails_when_python_cannot_be_found(
Expand Down Expand Up @@ -256,7 +257,7 @@ def test_activate_activates_existing_virtualenv_no_envs_file(
assert envs[venv_name]["patch"] == "3.7.1"

assert env.path == tmp_path / f"{venv_name}-py3.7"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)


def test_activate_activates_same_virtualenv_with_envs_file(
Expand Down Expand Up @@ -296,7 +297,7 @@ def test_activate_activates_same_virtualenv_with_envs_file(
assert envs[venv_name]["patch"] == "3.7.1"

assert env.path == tmp_path / f"{venv_name}-py3.7"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)


def test_activate_activates_different_virtualenv_with_envs_file(
Expand Down Expand Up @@ -342,7 +343,7 @@ def test_activate_activates_different_virtualenv_with_envs_file(
assert envs[venv_name]["patch"] == "3.6.6"

assert env.path == tmp_path / f"{venv_name}-py3.6"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)


def test_activate_activates_recreates_for_different_patch(
Expand Down Expand Up @@ -394,7 +395,7 @@ def test_activate_activates_recreates_for_different_patch(
assert envs[venv_name]["patch"] == "3.7.1"

assert env.path == tmp_path / f"{venv_name}-py3.7"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)
assert (tmp_path / f"{venv_name}-py3.7").exists()


Expand Down Expand Up @@ -442,7 +443,7 @@ def test_activate_does_not_recreate_when_switching_minor(
assert envs[venv_name]["patch"] == "3.6.6"

assert env.path == tmp_path / f"{venv_name}-py3.6"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)
assert (tmp_path / f"{venv_name}-py3.6").exists()


Expand Down Expand Up @@ -594,7 +595,7 @@ def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
env = manager.get()

assert env.path == tmp_path / f"{venv_name}-py3.7"
assert env.base == Path("/usr")
assert env.base == Path(sys.base_prefix)


def test_list(
Expand Down Expand Up @@ -932,7 +933,7 @@ def test_create_venv_finds_no_python_executable(

poetry.package.python_versions = "^3.6"

mocker.patch("sys.version_info", (2, 7, 16))
mocker.patch("sys.version_info", (3, 4, 5))
mocker.patch("shutil.which", return_value=None)

with pytest.raises(NoCompatiblePythonVersionFound) as e:
Expand Down Expand Up @@ -966,11 +967,12 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific
mocker.patch(
"subprocess.check_output",
side_effect=[
sys.base_prefix,
"/usr/bin/python3",
"3.5.3",
"/usr/bin/python3.9",
"3.9.0",
"/usr",
sys.base_prefix,
],
)
m = mocker.patch(
Expand All @@ -995,7 +997,7 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found(

poetry.package.python_versions = "^4.8"

mocker.patch("subprocess.check_output", side_effect=["", "", "", ""])
mocker.patch("subprocess.check_output", side_effect=[sys.base_prefix])
m = mocker.patch(
"poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
)
Expand All @@ -1021,7 +1023,7 @@ def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(

poetry.package.python_versions = "^4.8"

mocker.patch("subprocess.check_output", side_effect=["3.8.0"])
mocker.patch("subprocess.check_output", side_effect=[sys.base_prefix, "3.8.0"])
m = mocker.patch(
"poetry.utils.env.EnvManager.build_venv", side_effect=lambda *args, **kwargs: ""
)
Expand Down Expand Up @@ -1203,10 +1205,12 @@ def mock_check_output(cmd: str, *args: Any, **kwargs: Any) -> str:
executable = cmd[0]
if "python3.5" in str(executable):
return "3.5.12"
else:
return "3.7.1"
else:
return "/usr/bin/python3.5"
return "3.7.1"

if GET_BASE_PREFIX in cmd:
return sys.base_prefix

return "/usr/bin/python3.5"

mocker.patch("shutil.which", side_effect=lambda py: f"/usr/bin/{py}")
check_output = mocker.patch(
Expand Down
Loading