Skip to content

Commit

Permalink
fix: update, add and remove with --only shall not uninstall d…
Browse files Browse the repository at this point in the history
…ependencies from all other groups

Reintroduce some logic removed in bd3500d and add tests for it.
Fix logic with `reresolve = False`, which had the same issue.

- The change in installer is the fix for `reresolve = True` (reintroducing old logic).
- The change in transaction is the fix for `reresolve = False`.
  • Loading branch information
radoering authored and abn committed Jan 11, 2025
1 parent d6dbe8c commit 30fe6c7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
19 changes: 18 additions & 1 deletion src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,31 @@ def _do_install(self) -> int:
)

ops = transaction.calculate_operations(
with_uninstalls=self._requires_synchronization or self._update,
with_uninstalls=(
self._requires_synchronization or (self._update and not reresolve)
),
synchronize=self._requires_synchronization,
skip_directory=self._skip_directory,
extras=set(self._extras),
system_site_packages={
p.name for p in self._installed_repository.system_site_packages
},
)
if reresolve and not self._requires_synchronization:
# If no packages synchronisation has been requested we need
# to calculate the uninstall operations
transaction = Transaction(
locked_repository.packages,
lockfile_repo.packages,
installed_packages=self._installed_repository.packages,
root_package=root,
)

ops = [
op
for op in transaction.calculate_operations(with_uninstalls=True)
if op.job_type == "uninstall"
] + ops

# Validate the dependencies
for op in ops:
Expand Down
5 changes: 2 additions & 3 deletions src/poetry/puzzle/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ def calculate_operations(
operations.append(Uninstall(package))

if with_uninstalls:
result_packages = {package.name for package in self._result_packages}
for current_package in self._current_packages:
if current_package.name not in (
relevant_result_packages | uninstalls
) and (
if current_package.name not in (result_packages | uninstalls) and (
installed_package := self._installed_packages.get(
current_package.name
)
Expand Down
9 changes: 8 additions & 1 deletion tests/installation/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ def _configure_run_install_dev(


@pytest.mark.parametrize("lock_version", ("1.1", "2.1"))
@pytest.mark.parametrize("update", [False, True])
@pytest.mark.parametrize("requires_synchronization", [False, True])
@pytest.mark.parametrize(
("groups", "installs", "updates", "removals", "with_packages_installed"),
[
Expand All @@ -399,6 +401,8 @@ def test_run_install_with_dependency_groups(
repo: Repository,
package: ProjectPackage,
installed: CustomInstalledRepository,
update: bool,
requires_synchronization: bool,
lock_version: str,
) -> None:
_configure_run_install_dev(
Expand All @@ -414,10 +418,13 @@ def test_run_install_with_dependency_groups(
if groups is not None:
installer.only_groups(groups)

installer.requires_synchronization(True)
installer.update(update)
installer.requires_synchronization(requires_synchronization)
result = installer.run()
assert result == 0

if not requires_synchronization:
removals = 0
assert installer.executor.installations_count == installs
assert installer.executor.updates_count == updates
assert installer.executor.removals_count == removals
Expand Down
33 changes: 25 additions & 8 deletions tests/puzzle/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,17 @@ def test_it_should_update_installed_packages_if_sources_are_different() -> None:
],
)
@pytest.mark.parametrize("installed", [False, True])
@pytest.mark.parametrize("with_uninstalls", [False, True])
@pytest.mark.parametrize("sync", [False, True])
def test_calculate_operations_with_groups(
installed: bool, sync: bool, groups: set[str], expected: list[str]
installed: bool,
with_uninstalls: bool,
sync: bool,
groups: set[str],
expected: list[str],
) -> None:
transaction = Transaction(
[Package("a", "1"), Package("b", "1"), Package("c", "1")],
[Package("a", "1"), Package("b", "1"), Package("c", "1"), Package("d", "1")],
{
Package("a", "1"): TransitivePackageInfo(
0, {"main"}, {"main": AnyMarker()}
Expand All @@ -273,7 +278,11 @@ def test_calculate_operations_with_groups(
0, {"main", "dev"}, {"main": AnyMarker(), "dev": AnyMarker()}
),
},
[Package("a", "1"), Package("b", "1"), Package("c", "1")] if installed else [],
(
[Package("a", "1"), Package("b", "1"), Package("c", "1"), Package("d", "1")]
if installed
else []
),
None,
{"python_version": "3.8"},
groups,
Expand All @@ -285,12 +294,19 @@ def test_calculate_operations_with_groups(
if installed:
for op in expected_ops:
op["skipped"] = True
if sync:
for name in sorted({"a", "b", "c"}.difference(expected), reverse=True):
expected_ops.insert(0, {"job": "remove", "package": Package(name, "1")})
if with_uninstalls:
expected_ops.insert(0, {"job": "remove", "package": Package("d", "1")})
if sync:
for name in sorted({"a", "b", "c"}.difference(expected), reverse=True):
expected_ops.insert(
0, {"job": "remove", "package": Package(name, "1")}
)

check_operations(
transaction.calculate_operations(with_uninstalls=sync), expected_ops
transaction.calculate_operations(
with_uninstalls=with_uninstalls, synchronize=sync
),
expected_ops,
)


Expand Down Expand Up @@ -329,7 +345,8 @@ def test_calculate_operations_with_markers(
expected_ops.insert(0, {"job": "remove", "package": Package(name, "1")})

check_operations(
transaction.calculate_operations(with_uninstalls=sync), expected_ops
transaction.calculate_operations(with_uninstalls=sync, synchronize=sync),
expected_ops,
)


Expand Down

0 comments on commit 30fe6c7

Please sign in to comment.