Skip to content

Commit

Permalink
package: add satisfies helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
abn authored and radoering committed May 23, 2022
1 parent 9fa54f5 commit 297532c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/poetry/core/packages/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ def with_features(self: T, features: Iterable[str]) -> T:
def without_features(self: T) -> T:
return self.with_features([])

def satisfies(
self, dependency: Dependency, ignore_source_type: bool = False
) -> bool:
"""
Helper method to check if this package satisfies a given dependency.
This is determined by assessing if this instance provides the package and
features specified by the given dependency. Further, version and source
types are checked.
"""
if not self.provides(dependency) or not dependency.constraint.allows(
self.version
):
return False

return ignore_source_type or self.is_same_source_as(dependency)

def clone(self: T) -> T:
clone = self.__class__(self.pretty_name, self.version)
clone.__dict__ = copy.deepcopy(self.__dict__)
Expand Down
32 changes: 32 additions & 0 deletions tests/packages/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

from poetry.core.factory import Factory
from poetry.core.packages.dependency import Dependency
from poetry.core.packages.dependency_group import DependencyGroup
from poetry.core.packages.directory_dependency import DirectoryDependency
from poetry.core.packages.file_dependency import FileDependency
Expand Down Expand Up @@ -470,3 +471,34 @@ def test_set_readme_property() -> None:
assert package.readmes == (Path("README.md"),)
with pytest.deprecated_call():
assert package.readme == Path("README.md")


@pytest.mark.parametrize(
("package", "dependency", "ignore_source_type", "result"),
[
(Package("foo", "0.1.0"), Dependency("foo", ">=0.1.0"), False, True),
(Package("foo", "0.1.0"), Dependency("foo", "<0.1.0"), False, False),
(
Package("foo", "0.1.0"),
Dependency("foo", ">=0.1.0", source_type="git"),
False,
False,
),
(
Package("foo", "0.1.0"),
Dependency("foo", ">=0.1.0", source_type="git"),
True,
True,
),
(
Package("foo", "0.1.0"),
Dependency("foo", "<0.1.0", source_type="git"),
True,
False,
),
],
)
def test_package_satisfies(
package: Package, dependency: Dependency, ignore_source_type: bool, result: bool
) -> None:
assert package.satisfies(dependency, ignore_source_type) == result

0 comments on commit 297532c

Please sign in to comment.