Skip to content

Commit

Permalink
feat: pass artifact cache to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ralbertazzi committed Mar 20, 2023
1 parent 71b3d6e commit 5f98fcf
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 97 deletions.
8 changes: 6 additions & 2 deletions src/poetry/console/commands/debug/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from poetry.console.commands.init import InitCommand
from poetry.console.commands.show import ShowCommand
from poetry.utils.cache import ArtifactCache


if TYPE_CHECKING:
Expand Down Expand Up @@ -82,8 +83,11 @@ def handle(self) -> int:
)

pool = self.poetry.pool
artifact_cache = ArtifactCache(
cache_dir=self.poetry.config.artifacts_cache_directory
)

solver = Solver(package, pool, [], [], self.io)
solver = Solver(package, pool, artifact_cache, [], [], self.io)

ops = solver.solve().calculate_operations()

Expand Down Expand Up @@ -120,7 +124,7 @@ def handle(self) -> int:

pool.add_repository(locked_repository)

solver = Solver(package, pool, [], [], NullIO())
solver = Solver(package, pool, artifact_cache, [], [], NullIO())
with solver.use_environment(env):
ops = solver.solve().calculate_operations()

Expand Down
14 changes: 13 additions & 1 deletion src/poetry/console/commands/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from poetry.console.commands.env_command import EnvCommand
from poetry.console.commands.group_command import GroupCommand
from poetry.utils.cache import ArtifactCache


if TYPE_CHECKING:
Expand Down Expand Up @@ -203,9 +204,13 @@ def _display_packages_information(
locked_packages = locked_repository.packages
pool = RepositoryPool(ignore_repository_names=True)
pool.add_repository(locked_repository)
artifact_cache = ArtifactCache(
cache_dir=self.poetry.config.artifacts_cache_directory
)
solver = Solver(
root,
pool=pool,
artifact_cache=artifact_cache,
installed=[],
locked=locked_packages,
io=NullIO(),
Expand Down Expand Up @@ -526,7 +531,14 @@ def find_latest_package(
if package.is_direct_origin():
for dep in requires:
if dep.name == package.name and dep.source_type == package.source_type:
provider = Provider(root, self.poetry.pool, NullIO())
provider = Provider(
root,
self.poetry.pool,
ArtifactCache(
cache_dir=self.poetry.config.artifacts_cache_directory
),
NullIO(),
)
return provider.search_for_direct_origin_dependency(dep)

allow_prereleases = False
Expand Down
5 changes: 5 additions & 0 deletions src/poetry/installation/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from poetry.repositories import RepositoryPool
from poetry.repositories.installed_repository import InstalledRepository
from poetry.repositories.lockfile_repository import LockfileRepository
from poetry.utils.cache import ArtifactCache
from poetry.utils.extras import get_extra_package_names
from poetry.utils.helpers import pluralize

Expand Down Expand Up @@ -80,6 +81,7 @@ def __init__(
installed = self._get_installed()

self._installed_repository = installed
self._artifact_cache = ArtifactCache(cache_dir=config.artifacts_cache_directory)

@property
def executor(self) -> Executor:
Expand Down Expand Up @@ -206,6 +208,7 @@ def _do_refresh(self) -> int:
solver = Solver(
self._package,
self._pool,
self._artifact_cache,
locked_repository.packages,
locked_repository.packages,
self._io,
Expand Down Expand Up @@ -252,6 +255,7 @@ def _do_install(self) -> int:
solver = Solver(
self._package,
self._pool,
self._artifact_cache,
self._installed_repository.packages,
locked_repository.packages,
self._io,
Expand Down Expand Up @@ -324,6 +328,7 @@ def _do_install(self) -> int:
solver = Solver(
root,
pool,
self._artifact_cache,
self._installed_repository.packages,
locked_repository.packages,
NullIO(),
Expand Down
3 changes: 3 additions & 0 deletions src/poetry/puzzle/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from poetry.core.version.markers import BaseMarker

from poetry.repositories import RepositoryPool
from poetry.utils.cache import ArtifactCache
from poetry.utils.env import Env


Expand Down Expand Up @@ -140,13 +141,15 @@ def __init__(
self,
package: Package,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
io: IO,
*,
installed: list[Package] | None = None,
locked: list[Package] | None = None,
) -> None:
self._package = package
self._pool = pool
self._artifact_cache = artifact_cache
self._io = io
self._env: Env | None = None
self._python_constraint = package.python_constraint
Expand Down
9 changes: 8 additions & 1 deletion src/poetry/puzzle/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from poetry.packages import DependencyPackage
from poetry.puzzle.transaction import Transaction
from poetry.repositories import RepositoryPool
from poetry.utils.cache import ArtifactCache
from poetry.utils.env import Env


Expand All @@ -40,6 +41,7 @@ def __init__(
self,
package: ProjectPackage,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
installed: list[Package],
locked: list[Package],
io: IO,
Expand All @@ -51,7 +53,12 @@ def __init__(
self._io = io

self._provider = Provider(
self._package, self._pool, self._io, installed=installed, locked=locked
self._package,
self._pool,
artifact_cache,
self._io,
installed=installed,
locked=locked,
)
self._overrides: list[dict[DependencyPackage, dict[str, Dependency]]] = []

Expand Down
14 changes: 12 additions & 2 deletions tests/mixology/version_solver/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
from poetry.puzzle.provider import Provider as BaseProvider
from poetry.repositories import Repository
from poetry.repositories import RepositoryPool
from poetry.utils.cache import ArtifactCache


if TYPE_CHECKING:
from pathlib import Path

from tests.helpers import TestRepository


Expand All @@ -35,11 +38,18 @@ def pool(repo: TestRepository) -> RepositoryPool:
return pool


@pytest.fixture
def artifact_cache(tmp_path: Path) -> ArtifactCache:
return ArtifactCache(cache_dir=tmp_path)


@pytest.fixture
def root() -> ProjectPackage:
return ProjectPackage("myapp", "0.0.0")


@pytest.fixture
def provider(pool: RepositoryPool, root: ProjectPackage) -> Provider:
return Provider(root, pool, NullIO())
def provider(
pool: RepositoryPool, root: ProjectPackage, artifact_cache: ArtifactCache
) -> Provider:
return Provider(root, pool, artifact_cache, NullIO())
59 changes: 42 additions & 17 deletions tests/mixology/version_solver/test_with_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

from poetry.repositories import Repository
from poetry.repositories import RepositoryPool
from poetry.utils.cache import ArtifactCache


def test_with_compatible_locked_dependencies(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "*"))

Expand All @@ -33,7 +37,7 @@ def test_with_compatible_locked_dependencies(
add_to_repo(repo, "bar", "1.0.2")

locked = [get_package("foo", "1.0.1"), get_package("bar", "1.0.1")]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -43,7 +47,10 @@ def test_with_compatible_locked_dependencies(


def test_with_incompatible_locked_dependencies(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", ">1.0.1"))

Expand All @@ -55,7 +62,7 @@ def test_with_incompatible_locked_dependencies(
add_to_repo(repo, "bar", "1.0.2")

locked = [get_package("foo", "1.0.1"), get_package("bar", "1.0.1")]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -65,7 +72,10 @@ def test_with_incompatible_locked_dependencies(


def test_with_unrelated_locked_dependencies(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "*"))

Expand All @@ -78,7 +88,7 @@ def test_with_unrelated_locked_dependencies(
add_to_repo(repo, "baz", "1.0.0")

locked = [get_package("baz", "1.0.1")]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -88,7 +98,10 @@ def test_with_unrelated_locked_dependencies(


def test_unlocks_dependencies_if_necessary_to_ensure_that_a_new_dependency_is_satisfied(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "*"))
root.add_dependency(Factory.create_dependency("newdep", "2.0.0"))
Expand All @@ -109,7 +122,7 @@ def test_unlocks_dependencies_if_necessary_to_ensure_that_a_new_dependency_is_sa
get_package("baz", "1.0.0"),
get_package("qux", "1.0.0"),
]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -125,7 +138,10 @@ def test_unlocks_dependencies_if_necessary_to_ensure_that_a_new_dependency_is_sa


def test_with_compatible_locked_dependencies_use_latest(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "*"))
root.add_dependency(Factory.create_dependency("baz", "*"))
Expand All @@ -144,7 +160,7 @@ def test_with_compatible_locked_dependencies_use_latest(
get_package("bar", "1.0.1"),
get_package("baz", "1.0.0"),
]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -155,7 +171,10 @@ def test_with_compatible_locked_dependencies_use_latest(


def test_with_compatible_locked_dependencies_with_extras(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "^1.0"))

Expand All @@ -179,7 +198,7 @@ def test_with_compatible_locked_dependencies_with_extras(
get_package("bar", "1.0.0"),
get_package("baz", "1.0.0"),
]
provider = Provider(root, pool, NullIO(), locked=locked)
provider = Provider(root, pool, artifact_cache, NullIO(), locked=locked)

check_solver_result(
root,
Expand All @@ -189,7 +208,10 @@ def test_with_compatible_locked_dependencies_with_extras(


def test_with_yanked_package_in_lock(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "*"))

Expand All @@ -199,7 +221,7 @@ def test_with_yanked_package_in_lock(
# yanked version is kept in lock file
locked_foo = get_package("foo", "2")
assert not locked_foo.yanked
provider = Provider(root, pool, NullIO(), locked=[locked_foo])
provider = Provider(root, pool, artifact_cache, NullIO(), locked=[locked_foo])
result = check_solver_result(
root,
provider,
Expand All @@ -209,7 +231,7 @@ def test_with_yanked_package_in_lock(
assert foo.yanked

# without considering the lock file, the other version is chosen
provider = Provider(root, pool, NullIO())
provider = Provider(root, pool, artifact_cache, NullIO())
check_solver_result(
root,
provider,
Expand All @@ -218,7 +240,10 @@ def test_with_yanked_package_in_lock(


def test_no_update_is_respected_for_legacy_repository(
root: ProjectPackage, repo: Repository, pool: RepositoryPool
root: ProjectPackage,
repo: Repository,
pool: RepositoryPool,
artifact_cache: ArtifactCache,
):
root.add_dependency(Factory.create_dependency("foo", "^1.0"))

Expand All @@ -231,7 +256,7 @@ def test_no_update_is_respected_for_legacy_repository(
repo.add_package(foo_100)
repo.add_package(foo_101)

provider = Provider(root, pool, NullIO(), locked=[foo_100])
provider = Provider(root, pool, artifact_cache, NullIO(), locked=[foo_100])
check_solver_result(
root,
provider,
Expand Down
Loading

0 comments on commit 5f98fcf

Please sign in to comment.