Commit a66033bd by Etty Committed by GitHub

add: fix revert for dry-run and interrupts

This change correctly reverts pyproject.toml changes for dry runs as 
well as keyboard interrupts.

Resolves: #2933 #318
parent 55968783
...@@ -100,7 +100,6 @@ class AddCommand(InstallerCommand, InitCommand): ...@@ -100,7 +100,6 @@ class AddCommand(InstallerCommand, InitCommand):
packages = [name for name in packages if name not in existing_packages] packages = [name for name in packages if name not in existing_packages]
if not packages: if not packages:
self.poetry.file.write(content)
self.line("Nothing to add.") self.line("Nothing to add.")
return 0 return 0
...@@ -152,6 +151,7 @@ class AddCommand(InstallerCommand, InitCommand): ...@@ -152,6 +151,7 @@ class AddCommand(InstallerCommand, InitCommand):
poetry_content[section][_constraint["name"]] = constraint poetry_content[section][_constraint["name"]] = constraint
try:
# Write new content # Write new content
self.poetry.file.write(content) self.poetry.file.write(content)
...@@ -170,11 +170,10 @@ class AddCommand(InstallerCommand, InitCommand): ...@@ -170,11 +170,10 @@ class AddCommand(InstallerCommand, InitCommand):
self._installer.whitelist([r["name"] for r in requirements]) self._installer.whitelist([r["name"] for r in requirements])
try:
status = self._installer.run() status = self._installer.run()
except Exception: except BaseException:
# Using BaseException here as some exceptions, eg: KeyboardInterrupt, do not inherit from Exception
self.poetry.file.write(original_content) self.poetry.file.write(original_content)
raise raise
if status != 0 or self.option("dry-run"): if status != 0 or self.option("dry-run"):
......
...@@ -1630,3 +1630,26 @@ Writing lock file ...@@ -1630,3 +1630,26 @@ Writing lock file
""" """
assert expected == old_tester.io.fetch_output() 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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment