From 4d52c91ecc08cf67fb4718c03da8498aaa2cdef9 Mon Sep 17 00:00:00 2001 From: Wagner Macedo Date: Sun, 12 Feb 2023 00:20:56 +0100 Subject: [PATCH] rollback poetry.lock when a failure to add detected --- src/poetry/console/commands/add.py | 16 ++++++++++++++-- src/poetry/packages/locker.py | 3 +++ tests/helpers.py | 14 ++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/poetry/console/commands/add.py b/src/poetry/console/commands/add.py index 05bdc21bf77..e99f5a471d3 100644 --- a/src/poetry/console/commands/add.py +++ b/src/poetry/console/commands/add.py @@ -243,7 +243,7 @@ def handle(self) -> int: # Refresh the locker self.poetry.set_locker( - self.poetry.locker.__class__(self.poetry.locker.lock, poetry_content) + self.poetry.locker.copy_with(self.poetry.locker.lock, poetry_content) ) self.installer.set_locker(self.poetry.locker) @@ -261,9 +261,21 @@ def handle(self) -> int: status = self.installer.run() - if status == 0 and not self.option("dry-run"): + if self.option("dry-run"): + return status + + if status == 0: assert isinstance(content, TOMLDocument) self.poetry.file.write(content) + else: + for r in requirements: + with contextlib.suppress(ValueError): + self.poetry.package.dependency_group(group).remove_dependency( + r["name"] + ) + self.installer.lock(update=True) + self.installer.whitelist([]) + self.installer.run() return status diff --git a/src/poetry/packages/locker.py b/src/poetry/packages/locker.py index 2f4a2dcfda4..8a7782bbba4 100644 --- a/src/poetry/packages/locker.py +++ b/src/poetry/packages/locker.py @@ -267,6 +267,9 @@ def set_lock_data(self, root: Package, packages: list[Package]) -> bool: self._write_lock_data(lock) return do_write + def copy_with(self, lock: str | Path, local_config: dict[str, Any]) -> Locker: + return self.__class__(lock, local_config) + def _write_lock_data(self, data: TOMLDocument) -> None: lockfile = TOMLFile(self.lock) lockfile.write(data) diff --git a/tests/helpers.py b/tests/helpers.py index feef37728c0..84dfd7cd900 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -196,6 +196,12 @@ def __init__(self, lock: str | Path, local_config: dict) -> None: self._lock_data = None self._write = False + @property + def lock(self) -> Path: + if isinstance(self._lock, TOMLFile): + return self._lock.path + return self._lock + def write(self, write: bool = True) -> None: self._write = write @@ -215,6 +221,14 @@ def mock_lock_data(self, data: dict) -> None: def is_fresh(self) -> bool: return True + def copy_with(self, lock: str | Path, local_config: dict[str, Any]) -> TestLocker: + new_locker = TestLocker(lock, local_config) + new_locker._lock_data = self._lock_data + new_locker._locked = self._locked + new_locker._lock_data = self._lock_data + new_locker._write = self._write + return new_locker + def _write_lock_data(self, data: TOMLDocument) -> None: if self._write: super()._write_lock_data(data)