From 9c19089e203a0eb1a0acf0940af215a1b8d7baa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 5 Feb 2022 15:51:30 +0100 Subject: [PATCH 1/3] Refactoring in preparation to increase coverage by adding additional tests --- tests/packages/constraints/test_constraint.py | 116 ++++++++++++------ 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/tests/packages/constraints/test_constraint.py b/tests/packages/constraints/test_constraint.py index 335ae465e..4bf155c75 100644 --- a/tests/packages/constraints/test_constraint.py +++ b/tests/packages/constraints/test_constraint.py @@ -1,9 +1,17 @@ +from typing import TYPE_CHECKING + +import pytest + from poetry.core.packages.constraints.constraint import Constraint from poetry.core.packages.constraints.empty_constraint import EmptyConstraint from poetry.core.packages.constraints.multi_constraint import MultiConstraint from poetry.core.packages.constraints.union_constraint import UnionConstraint +if TYPE_CHECKING: + from poetry.core.packages.constraints import BaseConstraint + + def test_allows(): c = Constraint("win32") @@ -41,46 +49,74 @@ def test_allows_all(): assert not c.allows_all(UnionConstraint(Constraint("win32"), Constraint("linux"))) -def test_intersect(): - c = Constraint("win32") - - intersection = c.intersect(Constraint("linux")) - assert intersection == EmptyConstraint() - - intersection = c.intersect( - UnionConstraint(Constraint("win32"), Constraint("linux")) - ) - assert intersection == Constraint("win32") - - intersection = c.intersect( - UnionConstraint(Constraint("linux"), Constraint("linux2")) - ) - assert intersection == EmptyConstraint() - - intersection = c.intersect(Constraint("linux", "!=")) - assert intersection == c - - c = Constraint("win32", "!=") - - intersection = c.intersect(Constraint("linux", "!=")) - assert intersection == MultiConstraint( - Constraint("win32", "!="), Constraint("linux", "!=") - ) - - -def test_union(): - c = Constraint("win32") - - union = c.union(Constraint("linux")) - assert union == UnionConstraint(Constraint("win32"), Constraint("linux")) - - union = c.union(UnionConstraint(Constraint("win32"), Constraint("linux"))) - assert union == UnionConstraint(Constraint("win32"), Constraint("linux")) - - union = c.union(UnionConstraint(Constraint("linux"), Constraint("linux2"))) - assert union == UnionConstraint( - Constraint("win32"), Constraint("linux"), Constraint("linux2") - ) +@pytest.mark.parametrize( + ("constraint1", "constraint2", "expected"), + [ + ( + Constraint("win32"), + Constraint("linux"), + EmptyConstraint(), + ), + ( + Constraint("win32"), + UnionConstraint(Constraint("win32"), Constraint("linux")), + Constraint("win32"), + ), + ( + Constraint("win32"), + UnionConstraint(Constraint("linux"), Constraint("linux2")), + EmptyConstraint(), + ), + ( + Constraint("win32"), + Constraint("linux", "!="), + Constraint("win32"), + ), + ( + Constraint("win32", "!="), + Constraint("linux", "!="), + MultiConstraint(Constraint("win32", "!="), Constraint("linux", "!=")), + ), + ], +) +def test_intersect( + constraint1: "BaseConstraint", + constraint2: "BaseConstraint", + expected: "BaseConstraint", +): + intersection = constraint1.intersect(constraint2) + assert intersection == expected + + +@pytest.mark.parametrize( + ("constraint1", "constraint2", "expected"), + [ + ( + Constraint("win32"), + Constraint("linux"), + UnionConstraint(Constraint("win32"), Constraint("linux")), + ), + ( + Constraint("win32"), + UnionConstraint(Constraint("win32"), Constraint("linux")), + UnionConstraint(Constraint("win32"), Constraint("linux")), + ), + ( + Constraint("win32"), + UnionConstraint(Constraint("linux"), Constraint("linux2")), + UnionConstraint( + Constraint("win32"), Constraint("linux"), Constraint("linux2") + ), + ), + ], +) +def test_union( + constraint1: "BaseConstraint", + constraint2: "BaseConstraint", + expected: "BaseConstraint", +): + union = constraint1.union(constraint2) + assert union == expected def test_difference(): From 91e10dc41be83b2b88a0fa3725b453da1e8de89f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 5 Feb 2022 16:40:34 +0100 Subject: [PATCH 2/3] Increase test coverage for constraint intersection --- tests/packages/constraints/test_constraint.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/packages/constraints/test_constraint.py b/tests/packages/constraints/test_constraint.py index 4bf155c75..ab065f5ff 100644 --- a/tests/packages/constraints/test_constraint.py +++ b/tests/packages/constraints/test_constraint.py @@ -52,6 +52,11 @@ def test_allows_all(): @pytest.mark.parametrize( ("constraint1", "constraint2", "expected"), [ + ( + Constraint("win32"), + Constraint("win32"), + Constraint("win32"), + ), ( Constraint("win32"), Constraint("linux"), @@ -72,6 +77,11 @@ def test_allows_all(): Constraint("linux", "!="), Constraint("win32"), ), + ( + Constraint("win32", "!="), + Constraint("linux"), + Constraint("linux"), + ), ( Constraint("win32", "!="), Constraint("linux", "!="), From 1727cf73bea2a318f546e119d6ebc4d9f38b84ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sat, 5 Feb 2022 16:42:52 +0100 Subject: [PATCH 3/3] Improve constraint union --- .../core/packages/constraints/constraint.py | 15 ++++++++++++- tests/packages/constraints/test_constraint.py | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/poetry/core/packages/constraints/constraint.py b/src/poetry/core/packages/constraints/constraint.py index 52316c00a..4602ca426 100644 --- a/src/poetry/core/packages/constraints/constraint.py +++ b/src/poetry/core/packages/constraints/constraint.py @@ -3,6 +3,7 @@ from typing import Any from typing import Union +from poetry.core.packages.constraints import AnyConstraint from poetry.core.packages.constraints.base_constraint import BaseConstraint from poetry.core.packages.constraints.empty_constraint import EmptyConstraint @@ -106,7 +107,19 @@ def union(self, other: "BaseConstraint") -> "BaseConstraint": UnionConstraint, ) - return UnionConstraint(self, other) + if other == self: + return self + + if self.operator == "!=" and other.operator == "==" and self.allows(other): + return self + + if other.operator == "!=" and self.operator == "==" and other.allows(self): + return other + + if other.operator == "==" and self.operator == "==": + return UnionConstraint(self, other) + + return AnyConstraint() return other.union(self) diff --git a/tests/packages/constraints/test_constraint.py b/tests/packages/constraints/test_constraint.py index ab065f5ff..80c50c304 100644 --- a/tests/packages/constraints/test_constraint.py +++ b/tests/packages/constraints/test_constraint.py @@ -2,6 +2,7 @@ import pytest +from poetry.core.packages.constraints import AnyConstraint from poetry.core.packages.constraints.constraint import Constraint from poetry.core.packages.constraints.empty_constraint import EmptyConstraint from poetry.core.packages.constraints.multi_constraint import MultiConstraint @@ -101,6 +102,11 @@ def test_intersect( @pytest.mark.parametrize( ("constraint1", "constraint2", "expected"), [ + ( + Constraint("win32"), + Constraint("win32"), + Constraint("win32"), + ), ( Constraint("win32"), Constraint("linux"), @@ -118,6 +124,21 @@ def test_intersect( Constraint("win32"), Constraint("linux"), Constraint("linux2") ), ), + ( + Constraint("win32"), + Constraint("linux", "!="), + Constraint("linux", "!="), + ), + ( + Constraint("win32", "!="), + Constraint("linux"), + Constraint("win32", "!="), + ), + ( + Constraint("win32", "!="), + Constraint("linux", "!="), + AnyConstraint(), + ), ], ) def test_union(