Skip to content

Commit

Permalink
Only write lock file when installation is success (#7498)
Browse files Browse the repository at this point in the history
(affects `poetry add` and `poetry update`)
  • Loading branch information
wagnerluis1982 authored Apr 6, 2023
1 parent 161b19c commit 623bfff
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 131 deletions.
16 changes: 10 additions & 6 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions src/poetry/packages/locker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
28 changes: 14 additions & 14 deletions tests/console/commands/self/test_add_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
4 changes: 2 additions & 2 deletions tests/console/commands/self/test_remove_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/console/commands/self/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 623bfff

Please sign in to comment.