Skip to content

Commit

Permalink
Update env remove and list logic
Browse files Browse the repository at this point in the history
Updated list logic to also include all envs for the same project name
Update handling if other project's venv name was passed, use IncorrectEnvError for better error message
  • Loading branch information
dhvcc committed Aug 18, 2022
1 parent 4d91dad commit 0ec0b91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,9 @@ def list(self, name: str | None = None) -> list[VirtualEnv]:
if name is None:
name = self._poetry.package.name

venv_name = self.generate_env_name(name, str(self._poetry.file.parent))
venv_path = self._poetry.config.virtualenvs_path
env_list = [
VirtualEnv(Path(p)) for p in sorted(venv_path.glob(f"{venv_name}-py*"))
VirtualEnv(Path(p)) for p in sorted(venv_path.glob(f"{name}-*-py*"))
]

venv = self._poetry.file.parent / ".venv"
Expand Down Expand Up @@ -806,6 +805,12 @@ def remove(self, python: str) -> Env:
raise ValueError(
f'<warning>Environment "{python}" does not exist.</warning>'
)
else:
venv_path = self._poetry.config.virtualenvs_path
# Get all the poetry envs, even for other projects
env_names = [Path(p).name for p in sorted(venv_path.glob("*-*-py*"))]
if python in env_names:
raise IncorrectEnvError(python)

try:
python_version = Version.parse(python)
Expand Down
56 changes: 55 additions & 1 deletion tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ def test_remove_by_full_path_to_python(
assert not expected_venv_path.exists()


def test_doesnt_act_on_different_projects_env_when_passing_full_path(
def test_raises_if_acting_on_different_project_by_full_path(
tmp_dir: str,
manager: EnvManager,
poetry: Poetry,
Expand All @@ -754,6 +754,60 @@ def test_doesnt_act_on_different_projects_env_when_passing_full_path(
manager.remove(str(python_path))


def test_raises_if_acting_on_different_project_by_name(
tmp_dir: str,
manager: EnvManager,
poetry: Poetry,
config: Config,
):
config.merge({"virtualenvs": {"path": str(tmp_dir)}})

different_venv_name = (
EnvManager.generate_env_name(
"different-project",
str(poetry.file.parent),
)
+ "-py3.6"
)
different_venv_path = Path(tmp_dir) / different_venv_name
different_venv_bin_path = different_venv_path / "bin"
different_venv_bin_path.mkdir(parents=True)

python_path = different_venv_bin_path / "python"
python_path.touch(exist_ok=True)

with pytest.raises(IncorrectEnvError):
manager.remove(different_venv_name)


def test_removes_when_passing_old_env_after_dir_rename(
tmp_dir: str,
manager: EnvManager,
poetry: Poetry,
config: Config,
venv_name: str,
):
# Make sure that you can still remove old venv after you've renamed root directory
# of the project, which will create another venv with new name
config.merge({"virtualenvs": {"path": str(tmp_dir)}})

previous_venv_name = EnvManager.generate_env_name(
poetry.package.name,
"previous_dir_name",
)
venv_path = Path(tmp_dir) / f"{venv_name}-py3.6"
venv_path.mkdir()

previous_venv_name = f"{previous_venv_name}-py3.6"
previous_venv_path = Path(tmp_dir) / previous_venv_name
previous_venv_path.mkdir()

venv = manager.remove(previous_venv_name)
assert venv.path == previous_venv_path
assert not previous_venv_path.exists()
assert venv_path.exists()


def test_remove_also_deactivates(
tmp_dir: str,
manager: EnvManager,
Expand Down

0 comments on commit 0ec0b91

Please sign in to comment.