Skip to content

Commit

Permalink
Make sure to never remove essential packages like pip, setuptools and…
Browse files Browse the repository at this point in the history
… the root.
  • Loading branch information
PetterS committed Mar 26, 2020
1 parent 78e8f16 commit 201044c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
16 changes: 9 additions & 7 deletions poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from poetry.puzzle.operations import Uninstall
from poetry.puzzle.operations import Update
from poetry.puzzle.operations.operation import Operation
from poetry.puzzle.provider import Provider
from poetry.repositories import Pool
from poetry.repositories import Repository
from poetry.repositories.installed_repository import InstalledRepository
Expand Down Expand Up @@ -234,14 +235,15 @@ def _do_install(self, local_repo):
ops = solver.solve(use_latest=whitelist)

if self._remove_untracked:
for installed in self._installed_repository.packages:
is_in_lock_file = False
for locked in locked_repository.packages:
if locked.name == installed.name:
is_in_lock_file = True
break
locked_names = {locked.name for locked in locked_repository.packages}

if not is_in_lock_file:
for installed in self._installed_repository.packages:
if installed.name == self._package.name:
continue
if installed.name in Provider.UNSAFE_PACKAGES:
# Never remove pip, setuptools etc.
continue
if installed.name not in locked_names:
ops.append(Uninstall(installed))

# We need to filter operations so that packages
Expand Down
16 changes: 10 additions & 6 deletions tests/installation/test_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_run_install_remove_untracked(installer, locker, repo, package, installe
{
"package": [
{
"name": "A",
"name": "a",
"version": "1.0",
"category": "main",
"optional": False,
Expand All @@ -314,20 +314,24 @@ def test_run_install_remove_untracked(installer, locker, repo, package, installe
"python-versions": "*",
"platform": "*",
"content-hash": "123456789",
"hashes": {"A": []},
"hashes": {"a": []},
},
}
)
package_a = get_package("A", "1.0")
package_b = get_package("B", "1.1")
package_c = get_package("C", "1.2")
package_a = get_package("a", "1.0")
package_b = get_package("b", "1.1")
package_c = get_package("c", "1.2")
package_pip = get_package("pip", "20.0.0")
repo.add_package(package_a)
repo.add_package(package_b)
repo.add_package(package_c)
repo.add_package(package_pip)

installed.add_package(package_a)
installed.add_package(package_b)
installed.add_package(package_c)
installed.add_package(package_pip) # Always required and never removed.
installed.add_package(package) # Root package never removed.

package.add_dependency("A", "~1.0")

Expand All @@ -341,7 +345,7 @@ def test_run_install_remove_untracked(installer, locker, repo, package, installe
assert len(updates) == 0

removals = installer.installer.removals
assert len(removals) == 2
assert set(r.name for r in removals) == {"b", "c"}


def test_run_whitelist_add(installer, locker, repo, package):
Expand Down

0 comments on commit 201044c

Please sign in to comment.