Skip to content

Commit b907d1f

Browse files
committed
fix clean option and add test
1 parent 13424cc commit b907d1f

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ poetry python install <PYTHON_VERSION>
746746

747747
#### Options
748748

749-
* `--clean`: Cleanup installation if check fails.
749+
* `--clean`: Clean up installation if check fails.
750750
* `--free-threaded`: Use free-threaded version if available.
751751
* `--implementation`: Python implementation to use. (cpython, pypy)
752752
* `--reinstall`: Reinstall if installation already exists.

src/poetry/console/commands/python/install.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class PythonInstallCommand(Command):
3030
]
3131

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

119119
if installer.installation_directory.exists() and self.option("clean"):
120-
PythonRemoveCommand.remove_python_installation(request, impl, self.io)
120+
PythonRemoveCommand.remove_python_installation(
121+
str(installer.version), impl, self.io
122+
)
121123

122124
raise e
123125

tests/console/commands/python/test_python_install.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66

7+
from poetry.core.constraints.version.version import Version
8+
79
from poetry.console.exceptions import PoetryRuntimeError
810
from poetry.utils.env.python.installer import PythonDownloadNotFoundError
911
from poetry.utils.env.python.installer import PythonInstallationError
@@ -15,6 +17,7 @@
1517
from cleo.testers.command_tester import CommandTester
1618
from pytest_mock import MockerFixture
1719

20+
from poetry.config.config import Config
1821
from tests.types import CommandTesterFactory
1922

2023

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

9194

92-
def test_install_corrupt(tester: CommandTester, mock_installer: MagicMock) -> None:
95+
@pytest.mark.parametrize("clean", [False, True])
96+
def test_install_corrupt(
97+
tester: CommandTester, mock_installer: MagicMock, config: Config, clean: bool
98+
) -> None:
99+
def create_install_dir() -> None:
100+
(config.python_installation_dir / "cpython@3.11.9").mkdir(parents=True)
101+
93102
mock_installer.return_value.exists.side_effect = [False, PoetryRuntimeError("foo")]
103+
mock_installer.return_value.install.side_effect = create_install_dir
104+
mock_installer.return_value.version = Version.parse("3.11.9")
94105

95106
with pytest.raises(PoetryRuntimeError):
96-
tester.execute("3.11")
107+
clean_opt = "-c " if clean else ""
108+
tester.execute(f"{clean_opt}3.11")
97109

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

101-
assert tester.io.fetch_output() == (
113+
expected = (
102114
"Downloading and installing 3.11 (cpython) ... Done\n"
103115
"Testing 3.11 (cpython) ... Failed\n"
104116
)
117+
if clean:
118+
expected += "Removing installation 3.11.9 (cpython) ... Done\n"
119+
120+
assert tester.io.fetch_output() == expected
105121

106122

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

0 commit comments

Comments
 (0)