Skip to content

Commit d6e72c9

Browse files
SergioChanradoering
authored andcommitted
Fix publish --build prompt behavior in non-interactive mode (#10769)
(cherry picked from commit 0b81284)
1 parent 9fced1a commit d6e72c9

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/poetry/console/commands/publish.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,21 @@ def handle(self) -> int:
7272

7373
# Building package first, if told
7474
if self.option("build"):
75-
if publisher.files and not self.confirm(
76-
f"There are <info>{len(publisher.files)}</info> files ready for"
77-
" publishing. Build anyway?"
78-
):
79-
self.line_error("<error>Aborted!</error>")
80-
81-
return 1
75+
if publisher.files:
76+
if self.io.is_interactive():
77+
if not self.confirm(
78+
f"There are <info>{len(publisher.files)}</info> files ready for"
79+
f" publishing in {dist_dir}. Build anyway?"
80+
):
81+
self.line_error("<error>Aborted!</error>")
82+
83+
return 1
84+
85+
else:
86+
self.line(
87+
f"<warning>Warning: There are <info>{len(publisher.files)}</info> files "
88+
f"ready for publishing in {dist_dir}. Build anyway!</warning>"
89+
)
8290

8391
self.call("build", args=f"--output {dist_dir}")
8492

tests/console/commands/test_publish.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import TYPE_CHECKING
77
from typing import NoReturn
8+
from unittest.mock import PropertyMock
89

910
import pytest
1011
import requests
@@ -215,3 +216,31 @@ def test_publish_dist_dir_and_build_options(
215216
assert "Publishing simple-project (1.2.3) to PyPI" in output
216217
assert "- Uploading simple_project-1.2.3.tar.gz" in error
217218
assert "- Uploading simple_project-1.2.3-py2.py3-none-any.whl" in error
219+
220+
221+
def test_publish_build_no_interaction_skips_confirmation(
222+
app_tester: ApplicationTester, mocker: MockerFixture
223+
) -> None:
224+
mocker.patch(
225+
"poetry.publishing.publisher.Publisher.files",
226+
new_callable=PropertyMock,
227+
return_value=[Path("dist/simple_project-1.2.3-py2.py3-none-any.whl")],
228+
)
229+
confirm = mocker.patch("poetry.console.commands.publish.PublishCommand.confirm")
230+
command_call = mocker.patch("poetry.console.commands.publish.PublishCommand.call")
231+
publisher_publish = mocker.patch("poetry.publishing.Publisher.publish")
232+
233+
exit_code = app_tester.execute("publish --build --no-interaction --dry-run")
234+
235+
assert exit_code == 0
236+
output = app_tester.io.fetch_output()
237+
error = app_tester.io.fetch_error()
238+
239+
confirm.assert_not_called()
240+
assert "Build anyway?" not in output
241+
assert "Build anyway?" not in error
242+
assert (
243+
"Warning: There are 1 files ready for publishing in dist. Build anyway!"
244+
) in output
245+
command_call.assert_called_once_with("build", args="--output dist")
246+
assert publisher_publish.call_count == 1

0 commit comments

Comments
 (0)