def test_allows_all() -> None: c = UnionConstraint(Constraint("win32"), Constraint("linux")) assert c.allows_all(c) assert not c.allows_all(UnionConstraint(Constraint("win32"), Constraint("darwin"))) assert not c.allows_all(UnionConstraint(Constraint("linux2"), Constraint("darwin"))) assert c.allows_all(Constraint("win32")) assert not c.allows_all(Constraint("darwin"))
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"))
def test_allows_any() -> None: c = Constraint("win32") assert c.allows_any(Constraint("win32")) assert not c.allows_any(Constraint("linux")) assert c.allows_any(UnionConstraint(Constraint("win32"), Constraint("linux"))) assert c.allows_any(Constraint("linux", "!=")) c = Constraint("win32", "!=") assert not c.allows_any(Constraint("win32")) assert c.allows_any(Constraint("linux")) assert c.allows_any(UnionConstraint(Constraint("win32"), Constraint("linux"))) assert c.allows_any(Constraint("linux", "!="))
def parse_constraint(constraints: str) -> BaseConstraint: if constraints == "*": return AnyConstraint() or_constraints = re.split(r"\s*\|\|?\s*", constraints.strip()) or_groups = [] for constraints in or_constraints: and_constraints = re.split( r"(?<!^)(?<![=>< ,]) *(?<!-)[, ](?!-) *(?!,|$)", constraints) constraint_objects = [] if len(and_constraints) > 1: for constraint in and_constraints: constraint_objects.append(parse_single_constraint(constraint)) else: constraint_objects.append( parse_single_constraint(and_constraints[0])) if len(constraint_objects) == 1: constraint = constraint_objects[0] else: constraint = constraint_objects[0] for next_constraint in constraint_objects[1:]: constraint = constraint.intersect(next_constraint) or_groups.append(constraint) if len(or_groups) == 1: return or_groups[0] else: return UnionConstraint(*or_groups)
def test_allows_all(): c = Constraint("win32") assert c.allows_all(Constraint("win32")) assert not c.allows_all(Constraint("linux")) assert not c.allows_all(Constraint("linux", "!=")) 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 union(self, other: BaseConstraint) -> BaseConstraint: if isinstance(other, Constraint): from poetry.core.packages.constraints.union_constraint import ( UnionConstraint, ) 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)
MultiConstraint(Constraint("win32", "!="), Constraint( "linux", "!=")), ), ( "!=win32,!=linux,!=linux2", MultiConstraint( Constraint("win32", "!="), Constraint("linux", "!="), Constraint("linux2", "!="), ), ), ], ) def test_parse_constraint_multi(input, constraint): assert parse_constraint(input) == constraint @pytest.mark.parametrize( "input,constraint", [ ("win32 || linux", UnionConstraint(Constraint("win32"), Constraint("linux"))), ( "win32 || !=linux2", UnionConstraint(Constraint("win32"), Constraint("linux2", "!=")), ), ], ) def test_parse_constraint_union(input, constraint): assert parse_constraint(input) == constraint
@pytest.mark.parametrize( ("constraint1", "constraint2", "expected"), [ ( Constraint("win32"), Constraint("win32"), Constraint("win32"), ), ( 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"),