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

Add command catch KeyboardInterrupt and restore original_content (2933, 318) #3233

Merged
merged 6 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
33 changes: 16 additions & 17 deletions poetry/console/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def handle(self):
packages = [name for name in packages if name not in existing_packages]

if not packages:
self.poetry.file.write(content)
self.poetry.file.write(original_content)
estyxx marked this conversation as resolved.
Show resolved Hide resolved
self.line("Nothing to add.")
return 0

Expand Down Expand Up @@ -152,29 +152,28 @@ def handle(self):

poetry_content[section][_constraint["name"]] = constraint

# Write new content
self.poetry.file.write(content)
try:
# Write new content
self.poetry.file.write(content)

# Cosmetic new line
self.line("")
# Cosmetic new line
self.line("")

# Update packages
self.reset_poetry()
# Update packages
self.reset_poetry()

self._installer.set_package(self.poetry.package)
self._installer.dry_run(self.option("dry-run"))
self._installer.verbose(self._io.is_verbose())
self._installer.update(True)
if self.option("lock"):
self._installer.lock()
self._installer.set_package(self.poetry.package)
self._installer.dry_run(self.option("dry-run"))
self._installer.verbose(self._io.is_verbose())
self._installer.update(True)
if self.option("lock"):
self._installer.lock()

self._installer.whitelist([r["name"] for r in requirements])
self._installer.whitelist([r["name"] for r in requirements])

try:
status = self._installer.run()
except Exception:
except BaseException:
estyxx marked this conversation as resolved.
Show resolved Hide resolved
self.poetry.file.write(original_content)

raise

if status != 0 or self.option("dry-run"):
Expand Down
23 changes: 23 additions & 0 deletions tests/console/commands/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,3 +1630,26 @@ def test_add_with_lock_old_installer(app, repo, installer, old_tester):
"""

assert expected == old_tester.io.fetch_output()


def test_add_keyboard_interrupt_restore_content(app, repo, installer, tester, mocker):
mocker.patch(
"poetry.installation.installer.Installer.run", side_effect=KeyboardInterrupt()
)
original_content = app.poetry.file.read()

repo.add_package(get_package("cachy", "0.2.0"))

tester.execute("cachy --dry-run")

assert original_content == app.poetry.file.read()


def test_dry_run_restore_original_content(app, repo, installer, tester):
original_content = app.poetry.file.read()

repo.add_package(get_package("cachy", "0.2.0"))

tester.execute("cachy --dry-run")

assert original_content == app.poetry.file.read()