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

added --all option to remove #2341

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions docs/docs/managing-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ poetry env remove test-O3eWbxRl-py3.7
```

If you remove the currently activated virtual environment, it will be automatically deactivated.

You can delete all virtual environments at once using `env remove --all`.
28 changes: 24 additions & 4 deletions poetry/console/commands/env/remove.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from cleo import argument
from cleo import option
from clikit.api.args.exceptions import CannotParseArgsException

from ..command import Command

Expand All @@ -9,13 +11,31 @@ class EnvRemoveCommand(Command):
description = "Removes a specific virtualenv associated with the project."

arguments = [
argument("python", "The python executable to remove the virtualenv for.")
argument(
"python",
"The python executable to remove the virtualenv for.",
optional=True,
)
]

options = [option("all", None, "Remove of virtualenvs of this project.")]

def handle(self):
from poetry.utils.env import EnvManager

manager = EnvManager(self.poetry)
venv = manager.remove(self.argument("python"))

self.line("Deleted virtualenv: <comment>{}</comment>".format(venv.path))
if self.option("all"):
for venv in manager.list():
manager.remove(venv.path.name)
self.line("Deleted virtualenv: <comment>{}</comment>".format(venv.path))
else:
# simulate a non-optional argument - it needs to be optional for --all to work
# but required for deleting individual environments
if self.argument("python") is None:
raise CannotParseArgsException(
'Not enough arguments (missing: "python").'
)

venv = manager.remove(self.argument("python"))

self.line("Deleted virtualenv: <comment>{}</comment>".format(venv.path))
42 changes: 42 additions & 0 deletions tests/console/commands/env/test_remove.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from cleo.testers import CommandTester
from clikit.api.args.exceptions import CannotParseArgsException
from pytest import raises

from poetry.utils._compat import Path
from poetry.utils.env import EnvManager
Expand Down Expand Up @@ -55,3 +57,43 @@ def test_remove_by_name(app, tmp_dir):
)

assert expected == tester.io.fetch_output()


def test_remove_no_python(app, tmp_dir):
# additional test for the "required optional argument"
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()

command = app.find("env remove")
tester = CommandTester(command)
with raises(CannotParseArgsException):
tester.execute()


def test_remove_all(app, tmp_dir):
app.poetry.config.merge({"virtualenvs": {"path": str(tmp_dir)}})

venv_name = EnvManager.generate_env_name(
"simple-project", str(app.poetry.file.parent)
)
(Path(tmp_dir) / "{}-py3.7".format(venv_name)).mkdir()
(Path(tmp_dir) / "{}-py3.6".format(venv_name)).mkdir()

command = app.find("env remove")
tester = CommandTester(command)
tester.execute("--all")

assert not (Path(tmp_dir) / "{}-py3.6".format(venv_name)).exists()
assert not (Path(tmp_dir) / "{}-py3.7".format(venv_name)).exists()

expected = "Deleted virtualenv: {}\nDeleted virtualenv: {}\n".format(
(Path(tmp_dir) / "{}-py3.6".format(venv_name)),
(Path(tmp_dir) / "{}-py3.7".format(venv_name)),
)

assert expected == tester.io.fetch_output()