Skip to content

Commit 50f83d1

Browse files
authored
Merge pull request #146 from immqu/main
Introduce "all" and "none" specifiers
2 parents 9c15915 + 326d869 commit 50f83d1

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/univers/version_range.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from univers.utils import remove_spaces
2222
from univers.version_constraint import VersionConstraint
2323
from univers.version_constraint import contains_version
24+
from univers.versions import AllVersion
25+
from univers.versions import NoneVersion
2426

2527

2628
class InvalidVersionRange(Exception):
@@ -220,6 +222,13 @@ def __contains__(self, version):
220222
object. A version is contained in a VersionRange if it satisfies its
221223
constraints according to ``vers`` rules.
222224
"""
225+
226+
if self.version_class is AllVersion:
227+
return True
228+
229+
if self.version_class is NoneVersion:
230+
return False
231+
223232
if not isinstance(version, self.version_class):
224233
raise TypeError(
225234
f"{version!r} is not of expected type: {self.version_class!r}",
@@ -712,9 +721,9 @@ class PypiVersionRange(VersionRange):
712721
def from_native(cls, string):
713722
"""
714723
Return a VersionRange built from a PyPI PEP440 version specifiers ``string``.
715-
Raise an a univers.versions.InvalidVersion
724+
Raise a univers.versions.InvalidVersion
716725
"""
717-
# TODO: environment markers are yet supported
726+
# TODO: environment markers are not yet supported
718727
# TODO: handle .* version, ~= and === operators
719728

720729
if ";" in string:
@@ -1177,6 +1186,16 @@ class MattermostVersionRange(VersionRange):
11771186
version_class = versions.SemverVersion
11781187

11791188

1189+
class AllVersionRange(VersionRange):
1190+
scheme = "all"
1191+
version_class = versions.AllVersion
1192+
1193+
1194+
class NoneVersionRange(VersionRange):
1195+
scheme = "none"
1196+
version_class = versions.NoneVersion
1197+
1198+
11801199
def from_gitlab_native(gitlab_scheme, string):
11811200
purl_scheme = gitlab_scheme
11821201
if gitlab_scheme not in PURL_TYPE_BY_GITLAB_SCHEME.values():
@@ -1419,6 +1438,8 @@ def build_range_from_snyk_advisory_string(scheme: str, string: Union[str, List])
14191438
"openssl": OpensslVersionRange,
14201439
"mattermost": MattermostVersionRange,
14211440
"conan": ConanVersionRange,
1441+
"all": AllVersionRange,
1442+
"none": NoneVersionRange,
14221443
}
14231444

14241445
PURL_TYPE_BY_GITLAB_SCHEME = {

src/univers/versions.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def build_value(self, string):
123123

124124
def satisfies(self, constraint):
125125
"""
126-
Return True is this Version satisfies the ``constraint``
126+
Return True if this Version satisfies the ``constraint``
127127
VersionConstraint. Satisfying means that this version is "within" the
128128
``constraint``.
129129
"""
@@ -133,6 +133,18 @@ def __str__(self):
133133
return str(self.value)
134134

135135

136+
class AllVersion(Version):
137+
@classmethod
138+
def is_valid(cls, string):
139+
return string == "vers:all/*"
140+
141+
142+
class NoneVersion(Version):
143+
@classmethod
144+
def is_valid(cls, string):
145+
return string == "vers:none/*"
146+
147+
136148
class GenericVersion(Version):
137149
@classmethod
138150
def is_valid(cls, string):

tests/test_version_range.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from univers.versions import PypiVersion
2929
from univers.versions import RubygemsVersion
3030
from univers.versions import SemverVersion
31+
from univers.versions import Version
3132

3233

3334
class TestVersionRange(TestCase):
@@ -546,3 +547,25 @@ def test_version_range_normalize_case3():
546547
nvr = vr.normalize(known_versions=known_versions)
547548

548549
assert str(nvr) == "vers:pypi/>=1.0.0|<=1.3.0|3.0.0"
550+
551+
552+
def test_version_range_all():
553+
all_vers = VersionRange.from_string("vers:all/*")
554+
assert all_vers.contains(Version("1.2.3"))
555+
assert PypiVersion("2.0.3") in all_vers
556+
# test for invalid all range specification
557+
with pytest.raises(Exception):
558+
VersionRange.from_string("vers:all/>1.2.3")
559+
with pytest.raises(Exception):
560+
VersionRange.from_string("vers:all/*|>1.2.3")
561+
562+
563+
def test_version_range_none():
564+
none_vers = VersionRange.from_string("vers:none/*")
565+
assert not none_vers.contains(Version("1.2.3"))
566+
assert PypiVersion("2.0.3") not in none_vers
567+
# test for invalid all range specification
568+
with pytest.raises(Exception):
569+
VersionRange.from_string("vers:none/!1.2.3")
570+
with pytest.raises(Exception):
571+
VersionRange.from_string("vers:none/*|>1.2.3")

0 commit comments

Comments
 (0)