Skip to content
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
3 changes: 1 addition & 2 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,6 @@ def create_venv(

create_venv = self._poetry.config.get("virtualenvs.create")
in_project_venv = self.use_in_project_venv()
use_poetry_python = self._poetry.config.get("virtualenvs.use-poetry-python")
venv_prompt = self._poetry.config.get("virtualenvs.prompt")

specific_python_requested = python is not None
Expand All @@ -420,7 +419,7 @@ def create_venv(
# If an executable has been specified, we stop there
# and notify the user of the incompatibility.
# Otherwise, we try to find a compatible Python version.
if specific_python_requested and use_poetry_python:
if specific_python_requested:
raise NoCompatiblePythonVersionFoundError(
self._poetry.package.python_versions,
python.patch_version.to_string(),
Expand Down
4 changes: 3 additions & 1 deletion tests/console/commands/env/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def venv_name(app: PoetryTestApplication) -> str:

@pytest.fixture
def venv_cache(tmp_path: Path) -> Path:
return tmp_path
path = tmp_path / "venv_cache"
path.mkdir()
return path


@pytest.fixture(scope="module")
Expand Down
34 changes: 33 additions & 1 deletion tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

from poetry.core.constraints.version import Version

from poetry.console.commands.env.use import EnvUseCommand
from poetry.toml.file import TOMLFile
from poetry.utils.env import MockEnv
from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError
from tests.console.commands.env.helpers import build_venv
from tests.console.commands.env.helpers import check_output_wrapper

Expand Down Expand Up @@ -66,6 +68,10 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
mock_build_env = mocker.patch(
"poetry.utils.env.EnvManager.build_venv", side_effect=build_venv
)
envs_file = TOMLFile(venv_cache / "envs.toml")

assert not envs_file.exists()
assert not list(venv_cache.iterdir())

tester.execute("3.7")

Expand All @@ -81,7 +87,6 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
prompt="simple-project-py3.7",
)

envs_file = TOMLFile(venv_cache / "envs.toml")
assert envs_file.exists()
envs: dict[str, Any] = envs_file.read()
assert envs[venv_name]["minor"] == "3.7"
Expand All @@ -94,6 +99,33 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
assert tester.io.fetch_output() == f"Using virtualenv: {venv_py37}\n"


@pytest.mark.parametrize("use_poetry_python", [True, False])
def test_activate_does_not_activate_non_existing_virtualenv_with_unsupported_version(
tester: CommandTester,
venv_cache: Path,
venv_name: str,
venvs_in_cache_config: None,
mocked_python_register: MockedPythonRegister,
with_no_active_python: MagicMock,
use_poetry_python: bool,
) -> None:
mocked_python_register("3.7.1")
mocked_python_register("3.8.2")
command = tester.command
assert isinstance(command, EnvUseCommand)
command.poetry.package.python_versions = "~3.8"
command.poetry.config.merge(
{"virtualenvs": {"use-poetry-python": use_poetry_python}}
)

assert not list(venv_cache.iterdir())

with pytest.raises(NoCompatiblePythonVersionFoundError):
tester.execute("3.7")

assert not list(venv_cache.iterdir())


def test_get_prefers_explicitly_activated_virtualenvs_over_env_var(
tester: CommandTester,
current_python: PythonVersion,
Expand Down
4 changes: 3 additions & 1 deletion tests/utils/env/test_env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,14 +991,16 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found(
assert m.call_count == 0


@pytest.mark.parametrize("use_poetry_python", [True, False])
def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(
manager: EnvManager,
poetry: Poetry,
config: Config,
mocker: MockerFixture,
mocked_python_register: MockedPythonRegister,
use_poetry_python: bool,
) -> None:
config.config["virtualenvs"]["use-poetry-python"] = True
config.config["virtualenvs"]["use-poetry-python"] = use_poetry_python
if "VIRTUAL_ENV" in os.environ:
del os.environ["VIRTUAL_ENV"]

Expand Down