Skip to content

Commit

Permalink
fix clean option and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Feb 12, 2025
1 parent 13424cc commit b907d1f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ poetry python install <PYTHON_VERSION>

#### Options

* `--clean`: Cleanup installation if check fails.
* `--clean`: Clean up installation if check fails.
* `--free-threaded`: Use free-threaded version if available.
* `--implementation`: Python implementation to use. (cpython, pypy)
* `--reinstall`: Reinstall if installation already exists.
Expand Down
6 changes: 4 additions & 2 deletions src/poetry/console/commands/python/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PythonInstallCommand(Command):
]

options: ClassVar[list[Option]] = [
option("clean", "c", "Cleanup installation if check fails.", flag=True),
option("clean", "c", "Clean up installation if check fails.", flag=True),
option(
"free-threaded", "t", "Use free-threaded version if available.", flag=True
),
Expand Down Expand Up @@ -117,7 +117,9 @@ def handle(self) -> int:
self.io.write("<fg=red>Failed</>\n")

if installer.installation_directory.exists() and self.option("clean"):
PythonRemoveCommand.remove_python_installation(request, impl, self.io)
PythonRemoveCommand.remove_python_installation(
str(installer.version), impl, self.io
)

raise e

Expand Down
22 changes: 19 additions & 3 deletions tests/console/commands/python/test_python_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

from poetry.core.constraints.version.version import Version

from poetry.console.exceptions import PoetryRuntimeError
from poetry.utils.env.python.installer import PythonDownloadNotFoundError
from poetry.utils.env.python.installer import PythonInstallationError
Expand All @@ -15,6 +17,7 @@
from cleo.testers.command_tester import CommandTester
from pytest_mock import MockerFixture

from poetry.config.config import Config
from tests.types import CommandTesterFactory


Expand Down Expand Up @@ -89,19 +92,32 @@ def test_install_failure(tester: CommandTester, mock_installer: MagicMock) -> No
assert "foo\n" in tester.io.fetch_error()


def test_install_corrupt(tester: CommandTester, mock_installer: MagicMock) -> None:
@pytest.mark.parametrize("clean", [False, True])
def test_install_corrupt(
tester: CommandTester, mock_installer: MagicMock, config: Config, clean: bool
) -> None:
def create_install_dir() -> None:
(config.python_installation_dir / "cpython@3.11.9").mkdir(parents=True)

mock_installer.return_value.exists.side_effect = [False, PoetryRuntimeError("foo")]
mock_installer.return_value.install.side_effect = create_install_dir
mock_installer.return_value.version = Version.parse("3.11.9")

with pytest.raises(PoetryRuntimeError):
tester.execute("3.11")
clean_opt = "-c " if clean else ""
tester.execute(f"{clean_opt}3.11")

mock_installer.assert_called_once_with("3.11", "cpython", False)
mock_installer.return_value.install.assert_called_once()

assert tester.io.fetch_output() == (
expected = (
"Downloading and installing 3.11 (cpython) ... Done\n"
"Testing 3.11 (cpython) ... Failed\n"
)
if clean:
expected += "Removing installation 3.11.9 (cpython) ... Done\n"

assert tester.io.fetch_output() == expected


def test_install_success(tester: CommandTester, mock_installer: MagicMock) -> None:
Expand Down

0 comments on commit b907d1f

Please sign in to comment.