From 8fab14c6c42e0ec130d0b515580ad697d0e2367a Mon Sep 17 00:00:00 2001 From: Etty Date: Sat, 17 Oct 2020 18:16:45 +0100 Subject: [PATCH 1/6] Wrap KeyboardInterrupt and restore original_content --- poetry/console/commands/add.py | 50 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/poetry/console/commands/add.py b/poetry/console/commands/add.py index 3ad77ad686b..6b9d53936b5 100644 --- a/poetry/console/commands/add.py +++ b/poetry/console/commands/add.py @@ -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) self.line("Nothing to add.") return 0 @@ -152,31 +152,35 @@ def handle(self): poetry_content[section][_constraint["name"]] = constraint - # Write new content - self.poetry.file.write(content) - - # Cosmetic new line - self.line("") - - # 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.whitelist([r["name"] for r in requirements]) - try: - status = self._installer.run() - except Exception: + # Write new content + self.poetry.file.write(content) + raise KeyboardInterrupt("ops") + + # Cosmetic new line + self.line("") + + # 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.whitelist([r["name"] for r in requirements]) + try: + status = self._installer.run() + raise KeyboardInterrupt("opsssss") + except Exception: + self.poetry.file.write(original_content) + raise + + except KeyboardInterrupt: self.poetry.file.write(original_content) - raise - if status != 0 or self.option("dry-run"): # Revert changes if not self.option("dry-run"): From fe53350f8870b8a502aaeef6337160c335fac9c7 Mon Sep 17 00:00:00 2001 From: Etty Date: Sun, 18 Oct 2020 16:34:29 +0100 Subject: [PATCH 2/6] Use BaseExcaption --- poetry/console/commands/add.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/poetry/console/commands/add.py b/poetry/console/commands/add.py index 6b9d53936b5..080df836cd6 100644 --- a/poetry/console/commands/add.py +++ b/poetry/console/commands/add.py @@ -155,7 +155,6 @@ def handle(self): try: # Write new content self.poetry.file.write(content) - raise KeyboardInterrupt("ops") # Cosmetic new line self.line("") @@ -171,15 +170,11 @@ def handle(self): self._installer.lock() self._installer.whitelist([r["name"] for r in requirements]) - try: - status = self._installer.run() - raise KeyboardInterrupt("opsssss") - except Exception: - self.poetry.file.write(original_content) - raise - - except KeyboardInterrupt: + + status = self._installer.run() + except BaseException: self.poetry.file.write(original_content) + raise if status != 0 or self.option("dry-run"): # Revert changes From c8c16a44cd87589d410fac36f5da4af59d72e9f3 Mon Sep 17 00:00:00 2001 From: Etty Date: Sun, 18 Oct 2020 16:51:23 +0100 Subject: [PATCH 3/6] Add tests --- tests/console/commands/test_add.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index 09fae525aec..19998592ac3 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -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() From 40e84306eac6995be256c2068e8b2f30d89c6862 Mon Sep 17 00:00:00 2001 From: Etty Date: Mon, 19 Oct 2020 22:34:12 +0100 Subject: [PATCH 4/6] Do not rewrite content if there are no package to add --- poetry/console/commands/add.py | 1 - 1 file changed, 1 deletion(-) diff --git a/poetry/console/commands/add.py b/poetry/console/commands/add.py index 080df836cd6..31b7bc2de50 100644 --- a/poetry/console/commands/add.py +++ b/poetry/console/commands/add.py @@ -100,7 +100,6 @@ def handle(self): packages = [name for name in packages if name not in existing_packages] if not packages: - self.poetry.file.write(original_content) self.line("Nothing to add.") return 0 From 2348b23d6b54bffe18fa8b8b8675ab95060d1d16 Mon Sep 17 00:00:00 2001 From: Etty Date: Mon, 19 Oct 2020 22:34:28 +0100 Subject: [PATCH 5/6] Add comment --- poetry/console/commands/add.py | 1 + 1 file changed, 1 insertion(+) diff --git a/poetry/console/commands/add.py b/poetry/console/commands/add.py index 31b7bc2de50..965c80a4474 100644 --- a/poetry/console/commands/add.py +++ b/poetry/console/commands/add.py @@ -172,6 +172,7 @@ def handle(self): status = self._installer.run() except BaseException: + # Catch all exception including KeyboardInterrupt in order to restore the original content self.poetry.file.write(original_content) raise From 07c6eeff79cd5a2eac3cc05909b71216b2abe3f9 Mon Sep 17 00:00:00 2001 From: Etty Date: Mon, 19 Oct 2020 22:39:47 +0100 Subject: [PATCH 6/6] Update poetry/console/commands/add.py Update comment Co-authored-by: Arun Babu Neelicattu --- poetry/console/commands/add.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poetry/console/commands/add.py b/poetry/console/commands/add.py index 965c80a4474..29cf07d2a26 100644 --- a/poetry/console/commands/add.py +++ b/poetry/console/commands/add.py @@ -172,7 +172,7 @@ def handle(self): status = self._installer.run() except BaseException: - # Catch all exception including KeyboardInterrupt in order to restore the original content + # Using BaseException here as some exceptions, eg: KeyboardInterrupt, do not inherit from Exception self.poetry.file.write(original_content) raise