Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/univers/version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class VersionRange:
constraints = attr.ib(type=tuple, default=attr.Factory(tuple))

def __attrs_post_init__(self, *args, **kwargs):
if not self.constraints:
raise ValueError(
f"{self.__class__.__name__} requires at least one VersionConstraint."
)
constraints = tuple(sorted(self.constraints))
# Notes: setattr is used because this is an immutable frozen instance.
# See https://www.attrs.org/en/stable/init.html?#post-init
Expand Down
16 changes: 15 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Visit https://aboutcode.org and https://github.com/aboutcode-org/univers for support and download.

from typing import NamedTuple
from typing import Optional
from typing import Union


Expand All @@ -14,13 +15,26 @@ class SchemaDrivenVersTest(NamedTuple):
input: dict
expected_output: Union[list, bool]
description: str = ""
# name of the exception class expected to be raised while computing the
# result, for test cases whose input is invalid
expected_error: Optional[str] = None

@classmethod
def from_data(cls, data: dict):
return cls(**data)

def assert_result(self):
assert self.result == self.expected_output
if self.expected_error:
try:
self.result
except Exception as e:
assert type(e).__name__ == self.expected_error
else:
raise AssertionError(
f"{self.expected_error} not raised for input: {self.input!r}"
)
else:
assert self.result == self.expected_output

@property
def result(self):
Expand Down
6 changes: 4 additions & 2 deletions tests/data/schema/range/conan_range_from_native.json
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@
"native_range": "",
"scheme": "conan"
},
"expected_output": "vers:conan/"
"expected_output": null,
"expected_error": "ValueError"
},
{
"description": "Construct VERS range from native conan range.",
Expand Down Expand Up @@ -1437,7 +1438,8 @@
"native_range": "",
"scheme": "conan"
},
"expected_output": "vers:conan/"
"expected_output": null,
"expected_error": "ValueError"
},
{
"description": "Construct VERS range from native conan range.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@
"native_range": "",
"scheme": "conan"
},
"expected_output": "vers:conan/"
"expected_output": null,
"expected_error": "ValueError"
},
{
"description": "Construct VERS range from GitLab native conan range.",
Expand Down Expand Up @@ -1437,7 +1438,8 @@
"native_range": "",
"scheme": "conan"
},
"expected_output": "vers:conan/"
"expected_output": null,
"expected_error": "ValueError"
},
{
"description": "Construct VERS range from GitLab native conan range.",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_version_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,13 @@ def test_version_range_lexicographic():
assert LexicographicVersion(-123) in VersionRange.from_string("vers:lexicographic/<~")
assert LexicographicVersion(None) in VersionRange.from_string("vers:lexicographic/*")
assert LexicographicVersion("ABC") in VersionRange.from_string("vers:lexicographic/>abc|<=None")


def test_version_range_with_empty_constraints_is_invalid():
# https://github.com/aboutcode-org/univers/issues/203
with pytest.raises(ValueError):
VersionRange(constraints=[])
with pytest.raises(ValueError):
RANGE_CLASS_BY_SCHEMES["apache"](constraints=[])
with pytest.raises(ValueError):
VersionRange.from_string("vers:apache/")