|
32 | 32 | from collections.abc import Iterator |
33 | 33 | from unittest.mock import MagicMock |
34 | 34 |
|
| 35 | + from cleo.io.buffered_io import BufferedIO |
35 | 36 | from pytest import LogCaptureFixture |
36 | 37 | from pytest_mock import MockerFixture |
37 | 38 |
|
@@ -470,6 +471,56 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir( |
470 | 471 | assert not envs_file.exists() |
471 | 472 |
|
472 | 473 |
|
| 474 | +def test_activate_with_in_project_setting_if_venv_is_file( |
| 475 | + manager: EnvManager, |
| 476 | + poetry: Poetry, |
| 477 | + io: BufferedIO, |
| 478 | + config: Config, |
| 479 | + tmp_path: Path, |
| 480 | + mocker: MockerFixture, |
| 481 | + venv_flags_default: dict[str, bool], |
| 482 | + mocked_python_register: MockedPythonRegister, |
| 483 | +) -> None: |
| 484 | + if "VIRTUAL_ENV" in os.environ: |
| 485 | + del os.environ["VIRTUAL_ENV"] |
| 486 | + |
| 487 | + config.merge( |
| 488 | + { |
| 489 | + "virtualenvs": { |
| 490 | + "path": str(tmp_path / "virtualenvs"), |
| 491 | + "in-project": True, |
| 492 | + } |
| 493 | + } |
| 494 | + ) |
| 495 | + |
| 496 | + mocked_python_register("3.7.1") |
| 497 | + m = mocker.patch("poetry.utils.env.EnvManager.build_venv") |
| 498 | + |
| 499 | + venv_path = poetry.file.path.parent / ".venv" |
| 500 | + assert not venv_path.exists() |
| 501 | + venv_path.touch() |
| 502 | + assert venv_path.is_file() |
| 503 | + |
| 504 | + manager.activate("python3.7") |
| 505 | + |
| 506 | + m.assert_called_with( |
| 507 | + poetry.file.path.parent / ".venv", |
| 508 | + executable=Path("/usr/bin/python3.7"), |
| 509 | + flags=venv_flags_default, |
| 510 | + prompt="simple-project-py3.7", |
| 511 | + ) |
| 512 | + |
| 513 | + envs_file = TOMLFile(tmp_path / "virtualenvs" / "envs.toml") |
| 514 | + assert not envs_file.exists() |
| 515 | + |
| 516 | + # The .venv file is removed, but no .venv is created because we mocked build_venv. |
| 517 | + assert not venv_path.exists() |
| 518 | + assert ( |
| 519 | + f"{venv_path} is not a virtual environment but a file. Removing it." |
| 520 | + in io.fetch_error() |
| 521 | + ) |
| 522 | + |
| 523 | + |
473 | 524 | def test_deactivate_non_activated_but_existing( |
474 | 525 | tmp_path: Path, |
475 | 526 | manager: EnvManager, |
|
0 commit comments