diff --git a/src/poetry/installation/installer.py b/src/poetry/installation/installer.py index b5aa54f7072..ec5911ce8f7 100644 --- a/src/poetry/installation/installer.py +++ b/src/poetry/installation/installer.py @@ -291,12 +291,10 @@ def _do_install(self) -> int: lockfile_repo = LockfileRepository() self._populate_lockfile_repo(lockfile_repo, ops) - if self._update: + if self._lock and self._update: + # If we are only in lock mode, no need to go any further self._write_lock_file(lockfile_repo) - - if self._lock: - # If we are only in lock mode, no need to go any further - return 0 + return 0 if self._groups is not None: root = self._package.with_dependency_groups(list(self._groups), only=True) @@ -362,7 +360,13 @@ def _do_install(self) -> int: self._filter_operations(ops, lockfile_repo) # Execute operations - return self._execute(ops) + status = self._execute(ops) + + if status == 0 and self._update: + # Only write lock file when installation is success + self._write_lock_file(lockfile_repo) + + return status def _write_lock_file(self, repo: LockfileRepository, force: bool = False) -> None: if self._write_lock and (force or self._update): diff --git a/src/poetry/packages/locker.py b/src/poetry/packages/locker.py index 8784a391ebd..987a1acd609 100644 --- a/src/poetry/packages/locker.py +++ b/src/poetry/packages/locker.py @@ -224,6 +224,18 @@ def locked_repository(self) -> LockfileRepository: return repository def set_lock_data(self, root: Package, packages: list[Package]) -> bool: + """Store lock data and eventually persist to the lock file""" + lock = self._compute_lock_data(root, packages) + + if self._should_write(lock): + self._write_lock_data(lock) + return True + + return False + + def _compute_lock_data( + self, root: Package, packages: list[Package] + ) -> TOMLDocument: package_specs = self._lock_packages(packages) # Retrieving hashes for package in package_specs: @@ -254,6 +266,10 @@ def set_lock_data(self, root: Package, packages: list[Package]) -> bool: "content-hash": self._content_hash, } + return lock + + def _should_write(self, lock: TOMLDocument) -> bool: + # if lock file exists: compare with existing lock data do_write = True if self.is_locked(): try: @@ -263,8 +279,6 @@ def set_lock_data(self, root: Package, packages: list[Package]) -> bool: pass else: do_write = lock != lock_data - if do_write: - self._write_lock_data(lock) return do_write def _write_lock_data(self, data: TOMLDocument) -> None: diff --git a/tests/console/commands/self/test_add_plugins.py b/tests/console/commands/self/test_add_plugins.py index 37de59731f0..e7bd3c1707f 100644 --- a/tests/console/commands/self/test_add_plugins.py +++ b/tests/console/commands/self/test_add_plugins.py @@ -49,11 +49,11 @@ def test_add_no_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing poetry-plugin (0.1.0) + +Writing lock file """ assert_plugin_add_result(tester, expected, "^0.1.0") @@ -71,11 +71,11 @@ def test_add_with_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing poetry-plugin (0.2.0) + +Writing lock file """ assert_plugin_add_result(tester, expected, "^0.2.0") @@ -93,12 +93,12 @@ def test_add_with_git_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (2.0.5) • Installing poetry-plugin (0.1.2 9cf87a2) + +Writing lock file """ assert_plugin_add_result( @@ -119,13 +119,13 @@ def test_add_with_git_constraint_with_extras( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 3 installs, 0 updates, 0 removals • Installing pendulum (2.0.5) • Installing tomlkit (0.7.0) • Installing poetry-plugin (0.1.2 9cf87a2) + +Writing lock file """ assert_plugin_add_result( @@ -162,12 +162,12 @@ def test_add_with_git_constraint_with_subdirectory( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (2.0.5) • Installing poetry-plugin (0.1.2 9cf87a2) + +Writing lock file """ constraint = { @@ -262,11 +262,11 @@ def test_add_existing_plugin_updates_if_requested( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 0 installs, 1 update, 0 removals • Updating poetry-plugin (1.2.3 -> 2.3.4) + +Writing lock file """ assert_plugin_add_result(tester, expected, "^2.3.4") @@ -298,12 +298,12 @@ def test_adding_a_plugin_can_update_poetry_dependencies_if_needed( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 1 update, 0 removals • Updating tomlkit (0.7.1 -> 0.7.2) • Installing poetry-plugin (1.2.3) + +Writing lock file """ assert_plugin_add_result(tester, expected, "^1.2.3") diff --git a/tests/console/commands/self/test_remove_plugins.py b/tests/console/commands/self/test_remove_plugins.py index 660b3584012..17f24df5708 100644 --- a/tests/console/commands/self/test_remove_plugins.py +++ b/tests/console/commands/self/test_remove_plugins.py @@ -73,11 +73,11 @@ def test_remove_installed_package(tester: CommandTester): Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 0 installs, 0 updates, 1 removal • Removing poetry-plugin (1.2.3) + +Writing lock file """ assert tester.io.fetch_output() == expected diff --git a/tests/console/commands/self/test_update.py b/tests/console/commands/self/test_update.py index 09c3a21b501..6720406de8b 100644 --- a/tests/console/commands/self/test_update.py +++ b/tests/console/commands/self/test_update.py @@ -71,12 +71,12 @@ def test_self_update_can_update_from_recommended_installation( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 0 installs, 2 updates, 0 removals • Updating cleo (0.8.2 -> 1.0.0) • Updating poetry ({__version__} -> {new_version}) + +Writing lock file """ assert tester.io.fetch_output() == expected_output diff --git a/tests/console/commands/test_add.py b/tests/console/commands/test_add.py index b7ffde8ae1b..5c2a1bafe1f 100644 --- a/tests/console/commands/test_add.py +++ b/tests/console/commands/test_add.py @@ -16,6 +16,8 @@ if TYPE_CHECKING: + from typing import Any + from cleo.testers.command_tester import CommandTester from pytest_mock import MockerFixture @@ -70,11 +72,11 @@ def test_add_no_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -100,11 +102,11 @@ def test_add_replace_by_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected assert tester.command.installer.executor.installations_count == 1 @@ -119,11 +121,11 @@ def test_add_replace_by_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.1.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -167,11 +169,11 @@ def test_add_equal_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.1.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -191,11 +193,11 @@ def test_add_greater_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -225,12 +227,12 @@ def test_add_constraint_with_extras( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing msgpack-python (0.5.3) • Installing cachy (0.1.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -255,12 +257,12 @@ def test_add_constraint_dependencies( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing msgpack-python (0.5.3) • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -285,12 +287,12 @@ def test_add_git_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -321,12 +323,12 @@ def test_add_git_constraint_with_poetry( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -354,14 +356,14 @@ def test_add_git_constraint_with_extras( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 4 installs, 0 updates, 0 removals • Installing cleo (0.6.5) • Installing pendulum (1.4.4) • Installing tomlkit (0.5.5) • Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert tester.io.fetch_output().strip() == expected.strip() @@ -400,11 +402,11 @@ def test_add_git_constraint_with_subdirectory( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing two (2.0.0 9cf87a2) + +Writing lock file """ assert tester.io.fetch_output().strip() == expected.strip() assert tester.command.installer.executor.installations_count == 1 @@ -444,12 +446,12 @@ def test_add_git_ssh_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -491,12 +493,12 @@ def test_add_directory_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -532,12 +534,12 @@ def test_add_directory_with_poetry( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -564,12 +566,12 @@ def test_add_file_constraint_wheel( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -600,12 +602,12 @@ def test_add_file_constraint_sdist( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -640,12 +642,12 @@ def test_add_constraint_with_extras_option( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing msgpack-python (0.5.3) • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -680,13 +682,13 @@ def test_add_url_constraint_wheel( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals • Installing pendulum (1.4.4) • Installing demo\ (0.1.0 https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -722,8 +724,6 @@ def test_add_url_constraint_wheel_with_extras( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 4 installs, 0 updates, 0 removals • Installing cleo (0.6.5) @@ -731,6 +731,8 @@ def test_add_url_constraint_wheel_with_extras( • Installing tomlkit (0.5.5) • Installing demo\ (0.1.0 https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl) + +Writing lock file """ # Order might be different, split into lines and compare the overall output. expected = set(expected.splitlines()) @@ -764,11 +766,11 @@ def test_add_constraint_with_python( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -802,11 +804,11 @@ def test_add_constraint_with_platform( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -853,11 +855,11 @@ def test_add_constraint_with_source( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -915,11 +917,11 @@ def test_add_to_section_that_does_not_exist_yet( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -962,11 +964,11 @@ def test_add_to_dev_section_deprecated( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing cachy (0.2.0) + +Writing lock file """ assert tester.io.fetch_error() == warning @@ -993,11 +995,11 @@ def test_add_should_not_select_prereleases( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing pyyaml (3.13) + +Writing lock file """ assert tester.io.fetch_output() == expected @@ -1112,11 +1114,11 @@ def test_add_should_work_when_adding_existing_package_with_latest_constraint( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing foo (1.1.2) + +Writing lock file """ assert expected in tester.io.fetch_output() @@ -1141,11 +1143,11 @@ def test_add_chooses_prerelease_if_only_prereleases_are_available( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing foo (1.2.3b1) + +Writing lock file """ assert expected in tester.io.fetch_output() @@ -1164,11 +1166,11 @@ def test_add_prefers_stable_releases( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals • Installing foo (1.2.3) + +Writing lock file """ assert expected in tester.io.fetch_output() @@ -1212,11 +1214,11 @@ def test_add_no_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1245,11 +1247,11 @@ def test_add_equal_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.1.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1273,11 +1275,11 @@ def test_add_greater_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1309,12 +1311,12 @@ def test_add_constraint_with_extras_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing msgpack-python (0.5.3) - Installing cachy (0.1.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1343,12 +1345,12 @@ def test_add_constraint_dependencies_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing msgpack-python (0.5.3) - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1372,12 +1374,12 @@ def test_add_git_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1407,12 +1409,12 @@ def test_add_git_constraint_with_poetry_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1439,14 +1441,14 @@ def test_add_git_constraint_with_extras_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 4 installs, 0 updates, 0 removals - Installing cleo (0.6.5) - Installing pendulum (1.4.4) - Installing tomlkit (0.5.5) - Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1478,12 +1480,12 @@ def test_add_git_ssh_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.2 9cf87a2) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1520,12 +1522,12 @@ def test_add_directory_constraint_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1558,12 +1560,12 @@ def test_add_directory_with_poetry_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.2 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1591,12 +1593,12 @@ def test_add_file_constraint_wheel_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1629,12 +1631,12 @@ def test_add_file_constraint_sdist_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo (0.1.0 {app.poetry.file.parent.joinpath(path).resolve().as_posix()}) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1671,12 +1673,12 @@ def test_add_constraint_with_extras_option_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing msgpack-python (0.5.3) - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1713,13 +1715,13 @@ def test_add_url_constraint_wheel_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 2 installs, 0 updates, 0 removals - Installing pendulum (1.4.4) - Installing demo\ (0.1.0 https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1756,8 +1758,6 @@ def test_add_url_constraint_wheel_with_extras_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 4 installs, 0 updates, 0 removals - Installing cleo (0.6.5) @@ -1765,6 +1765,8 @@ def test_add_url_constraint_wheel_with_extras_old_installer( - Installing tomlkit (0.5.5) - Installing demo\ (0.1.0 https://python-poetry.org/distributions/demo-0.1.0-py2.py3-none-any.whl) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1800,11 +1802,11 @@ def test_add_constraint_with_python_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1840,11 +1842,11 @@ def test_add_constraint_with_platform_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1893,11 +1895,11 @@ def test_add_constraint_with_source_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1959,11 +1961,11 @@ def test_add_to_section_that_does_no_exist_yet_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing cachy (0.2.0) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -1993,11 +1995,11 @@ def test_add_should_not_select_prereleases_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing pyyaml (3.13) + +Writing lock file """ assert old_tester.io.fetch_output() == expected @@ -2058,11 +2060,11 @@ def test_add_should_work_when_adding_existing_package_with_latest_constraint_old Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing foo (1.1.2) + +Writing lock file """ assert expected in old_tester.io.fetch_output() @@ -2090,11 +2092,11 @@ def test_add_chooses_prerelease_if_only_prereleases_are_available_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing foo (1.2.3b1) + +Writing lock file """ assert expected in old_tester.io.fetch_output() @@ -2116,11 +2118,11 @@ def test_add_preferes_stable_releases_old_installer( Updating dependencies Resolving dependencies... -Writing lock file - Package operations: 1 install, 0 updates, 0 removals - Installing foo (1.2.3) + +Writing lock file """ assert expected in old_tester.io.fetch_output() @@ -2157,7 +2159,8 @@ def test_add_keyboard_interrupt_restore_content( tester = command_tester_factory("add", poetry=poetry_with_up_to_date_lockfile) mocker.patch( - "poetry.installation.installer.Installer.run", side_effect=KeyboardInterrupt() + "poetry.installation.installer.Installer._execute", + side_effect=KeyboardInterrupt(), ) original_pyproject_content = poetry_with_up_to_date_lockfile.file.read() original_lockfile_content = poetry_with_up_to_date_lockfile._locker.lock_data @@ -2200,3 +2203,39 @@ def test_add_with_dry_run_keep_files_intact( assert ( poetry_with_up_to_date_lockfile._locker.lock_data == original_lockfile_content ) + + +def test_add_should_not_change_lock_file_when_dependency_installation_fail( + poetry_with_up_to_date_lockfile: Poetry, + repo: TestRepository, + command_tester_factory: CommandTesterFactory, + mocker: MockerFixture, +): + tester = command_tester_factory("add", poetry=poetry_with_up_to_date_lockfile) + + repo.add_package(get_package("docker", "4.3.1")) + repo.add_package(get_package("cachy", "0.2.0")) + + original_pyproject_content = poetry_with_up_to_date_lockfile.file.read() + original_lockfile_content = poetry_with_up_to_date_lockfile.locker.lock_data + + def error(_: Any) -> int: + tester.io.write("\n BuildError\n\n") + return 1 + + mocker.patch("poetry.installation.installer.Installer._execute", side_effect=error) + tester.execute("cachy") + + expected = """\ +Using version ^0.2.0 for cachy + +Updating dependencies +Resolving dependencies... + + BuildError + +""" + + assert poetry_with_up_to_date_lockfile.file.read() == original_pyproject_content + assert poetry_with_up_to_date_lockfile.locker.lock_data == original_lockfile_content + assert tester.io.fetch_output() == expected diff --git a/tests/installation/test_installer.py b/tests/installation/test_installer.py index 841a2ccde64..400278e7798 100644 --- a/tests/installation/test_installer.py +++ b/tests/installation/test_installer.py @@ -105,6 +105,7 @@ def __init__(self, lock_path: Path) -> None: self._lock = lock_path / "poetry.lock" self._written_data = None self._locked = False + self._lock_data = None self._content_hash = self._get_content_hash() @property @@ -2415,6 +2416,29 @@ def test_installer_can_handle_old_lock_files( assert installer.executor.installations_count == 8 +def test_installer_does_not_write_lock_file_when_installation_fails( + installer: Installer, + locker: Locker, + repo: Repository, + package: ProjectPackage, + mocker: MockerFixture, +): + repo.add_package(get_package("A", "1.0")) + package.add_dependency(Factory.create_dependency("A", "~1.0")) + + locker.locked(False) + + mocker.patch("poetry.installation.installer.Installer._execute", return_value=1) + result = installer.run() + assert result == 1 # error + + assert locker._lock_data is None + + assert installer.executor.installations_count == 0 + assert installer.executor.updates_count == 0 + assert installer.executor.removals_count == 0 + + @pytest.mark.parametrize("quiet", [True, False]) def test_run_with_dependencies_quiet( installer: Installer, diff --git a/tests/installation/test_installer_old.py b/tests/installation/test_installer_old.py index 45d3a5c767d..ced7a6fc934 100644 --- a/tests/installation/test_installer_old.py +++ b/tests/installation/test_installer_old.py @@ -62,6 +62,7 @@ def __init__(self, lock_path: Path) -> None: self._lock = lock_path / "poetry.lock" self._written_data = None self._locked = False + self._lock_data = None self._content_hash = self._get_content_hash() @property