From f55db69151b815538bc2d6458a3b23c299a11caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Mon, 11 Apr 2022 10:52:24 +0200 Subject: [PATCH] specification: add allow_similar flag to is_same_package_as --- src/poetry/core/packages/specification.py | 12 +++++++--- tests/packages/test_specification.py | 28 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 tests/packages/test_specification.py diff --git a/src/poetry/core/packages/specification.py b/src/poetry/core/packages/specification.py index 2b0092136..30a041958 100644 --- a/src/poetry/core/packages/specification.py +++ b/src/poetry/core/packages/specification.py @@ -71,9 +71,15 @@ def source_subdirectory(self) -> str | None: def features(self) -> frozenset[str]: return self._features - def is_same_package_as(self, other: PackageSpecification) -> bool: - if other.complete_name != self.complete_name: - return False + def is_same_package_as( + self, other: PackageSpecification, *, allow_similar: bool = False + ) -> bool: + if allow_similar: + if other.name != self.name: + return False + else: + if other.complete_name != self.complete_name: + return False if self._source_type: if self._source_type != other.source_type: diff --git a/tests/packages/test_specification.py b/tests/packages/test_specification.py new file mode 100644 index 000000000..84268ce88 --- /dev/null +++ b/tests/packages/test_specification.py @@ -0,0 +1,28 @@ +from __future__ import annotations + +import pytest + +from poetry.core.packages.specification import PackageSpecification + + +@pytest.mark.parametrize( + "spec1, spec2, expected_exact, expected_similar", + [ + (PackageSpecification("a"), PackageSpecification("a"), True, True), + (PackageSpecification("a"), PackageSpecification("ab"), False, False), + ( + PackageSpecification("a"), + PackageSpecification("a", features=["c"]), + False, + True, + ), + ], +) +def test_is_same_package_allow_similar( + spec1: PackageSpecification, + spec2: PackageSpecification, + expected_exact: bool, + expected_similar: bool, +) -> None: + assert spec1.is_same_package_as(spec2) == expected_exact + assert spec1.is_same_package_as(spec2, allow_similar=True) == expected_similar