From 2c6b6fac4393c42597461d1dc84bc6be900e218d Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Tue, 12 Nov 2024 11:57:17 +0100 Subject: [PATCH 1/6] add all and none versioning schemes, add range tests Signed-off-by: Immanuel Kunz Signed-off-by: Kunz, Immanuel --- src/univers/version_range.py | 25 +++++++++++++++++++++++-- src/univers/versions.py | 10 +++++++++- tests/test_version_range.py | 11 +++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/univers/version_range.py b/src/univers/version_range.py index 69f84f24..373ceaab 100644 --- a/src/univers/version_range.py +++ b/src/univers/version_range.py @@ -19,6 +19,8 @@ from univers import versions from univers.conan.version_range import VersionRange as conan_version_range from univers.utils import remove_spaces +from univers.versions import AllVersion +from univers.versions import NoneVersion from univers.version_constraint import VersionConstraint from univers.version_constraint import contains_version @@ -220,6 +222,13 @@ def __contains__(self, version): object. A version is contained in a VersionRange if it satisfies its constraints according to ``vers`` rules. """ + + if self.version_class is AllVersion: + return True + + if self.version_class is NoneVersion: + return False + if not isinstance(version, self.version_class): raise TypeError( f"{version!r} is not of expected type: {self.version_class!r}", @@ -712,9 +721,9 @@ class PypiVersionRange(VersionRange): def from_native(cls, string): """ Return a VersionRange built from a PyPI PEP440 version specifiers ``string``. - Raise an a univers.versions.InvalidVersion + Raise a univers.versions.InvalidVersion """ - # TODO: environment markers are yet supported + # TODO: environment markers are not yet supported # TODO: handle .* version, ~= and === operators if ";" in string: @@ -1177,6 +1186,16 @@ class MattermostVersionRange(VersionRange): version_class = versions.SemverVersion +class AllVersionRange(VersionRange): + scheme = "all" + version_class = versions.AllVersion + + +class NoneVersionRange(VersionRange): + scheme = "none" + version_class = versions.NoneVersion + + def from_gitlab_native(gitlab_scheme, string): purl_scheme = gitlab_scheme 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]) "openssl": OpensslVersionRange, "mattermost": MattermostVersionRange, "conan": ConanVersionRange, + "all": AllVersionRange, + "none": NoneVersionRange, } PURL_TYPE_BY_GITLAB_SCHEME = { diff --git a/src/univers/versions.py b/src/univers/versions.py index 14b3e007..c26b80f8 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -123,7 +123,7 @@ def build_value(self, string): def satisfies(self, constraint): """ - Return True is this Version satisfies the ``constraint`` + Return True if this Version satisfies the ``constraint`` VersionConstraint. Satisfying means that this version is "within" the ``constraint``. """ @@ -133,6 +133,14 @@ def __str__(self): return str(self.value) +class AllVersion(Version): + pass + + +class NoneVersion(Version): + pass + + class GenericVersion(Version): @classmethod def is_valid(cls, string): diff --git a/tests/test_version_range.py b/tests/test_version_range.py index 2d3c4112..cea18d7b 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -28,6 +28,7 @@ from univers.versions import PypiVersion from univers.versions import RubygemsVersion from univers.versions import SemverVersion +from univers.versions import Version class TestVersionRange(TestCase): @@ -546,3 +547,13 @@ def test_version_range_normalize_case3(): nvr = vr.normalize(known_versions=known_versions) assert str(nvr) == "vers:pypi/>=1.0.0|<=1.3.0|3.0.0" + +def test_version_range_all(): + all_vers = VersionRange.from_string("vers:all/*") + assert all_vers.contains(Version("1.2.3")) + assert PypiVersion("2.0.3") in all_vers + +def test_version_range_none(): + all_vers = VersionRange.from_string("vers:none/*") + assert not all_vers.contains(Version("1.2.3")) + assert PypiVersion("2.0.3") not in all_vers From 00478f37952e671d5e02fdbef03d66b8a6c631a3 Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Wed, 13 Nov 2024 12:52:35 +0100 Subject: [PATCH 2/6] fix naming in none_test Signed-off-by: Immanuel Kunz Signed-off-by: Kunz, Immanuel --- tests/test_version_range.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_version_range.py b/tests/test_version_range.py index cea18d7b..b2c4ec07 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -554,6 +554,6 @@ def test_version_range_all(): assert PypiVersion("2.0.3") in all_vers def test_version_range_none(): - all_vers = VersionRange.from_string("vers:none/*") - assert not all_vers.contains(Version("1.2.3")) - assert PypiVersion("2.0.3") not in all_vers + none_vers = VersionRange.from_string("vers:none/*") + assert not none_vers.contains(Version("1.2.3")) + assert PypiVersion("2.0.3") not in none_vers From 19473bcc3dee74d1fb0e4d6906393f4e7cc67f1b Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Thu, 14 Nov 2024 16:03:17 +0100 Subject: [PATCH 3/6] add string equality check for all and none versions Signed-off-by: Kunz, Immanuel --- src/univers/versions.py | 8 ++++++-- tests/test_version_range.py | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/univers/versions.py b/src/univers/versions.py index c26b80f8..f99594dd 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -134,11 +134,15 @@ def __str__(self): class AllVersion(Version): - pass + @classmethod + def is_valid(cls, string): + return string == "vers:all/*" class NoneVersion(Version): - pass + @classmethod + def is_valid(cls, string): + return string == "vers:none/*" class GenericVersion(Version): diff --git a/tests/test_version_range.py b/tests/test_version_range.py index b2c4ec07..95521693 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -552,6 +552,11 @@ def test_version_range_all(): all_vers = VersionRange.from_string("vers:all/*") assert all_vers.contains(Version("1.2.3")) assert PypiVersion("2.0.3") in all_vers + # test for invalid all range specification + with pytest.raises(Exception): + VersionRange.from_string("vers:all/>1.2.3") + with pytest.raises(Exception): + VersionRange.from_string("vers:all/*|>1.2.3") def test_version_range_none(): none_vers = VersionRange.from_string("vers:none/*") From df2d2f6e416ce99d3c5ca5944a4eb92b85b7d9e7 Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Fri, 15 Nov 2024 06:23:18 +0100 Subject: [PATCH 4/6] add version range test for none scheme Signed-off-by: Kunz, Immanuel --- tests/test_version_range.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_version_range.py b/tests/test_version_range.py index 95521693..93c7403a 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -562,3 +562,8 @@ def test_version_range_none(): none_vers = VersionRange.from_string("vers:none/*") assert not none_vers.contains(Version("1.2.3")) assert PypiVersion("2.0.3") not in none_vers + # test for invalid all range specification + with pytest.raises(Exception): + VersionRange.from_string("vers:none/!1.2.3") + with pytest.raises(Exception): + VersionRange.from_string("vers:none/*|>1.2.3") From 1e9d25348fd8ec4c7ddf8f8c8754b40e5301462c Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Fri, 15 Nov 2024 11:26:35 +0100 Subject: [PATCH 5/6] code style Signed-off-by: Kunz, Immanuel --- src/univers/version_range.py | 4 ++-- src/univers/versions.py | 2 +- tests/test_version_range.py | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/univers/version_range.py b/src/univers/version_range.py index 373ceaab..8f0194e8 100644 --- a/src/univers/version_range.py +++ b/src/univers/version_range.py @@ -225,10 +225,10 @@ def __contains__(self, version): if self.version_class is AllVersion: return True - + if self.version_class is NoneVersion: return False - + if not isinstance(version, self.version_class): raise TypeError( f"{version!r} is not of expected type: {self.version_class!r}", diff --git a/src/univers/versions.py b/src/univers/versions.py index f99594dd..c06f4ff0 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -137,7 +137,7 @@ class AllVersion(Version): @classmethod def is_valid(cls, string): return string == "vers:all/*" - + class NoneVersion(Version): @classmethod diff --git a/tests/test_version_range.py b/tests/test_version_range.py index 93c7403a..104646f8 100644 --- a/tests/test_version_range.py +++ b/tests/test_version_range.py @@ -548,6 +548,7 @@ def test_version_range_normalize_case3(): assert str(nvr) == "vers:pypi/>=1.0.0|<=1.3.0|3.0.0" + def test_version_range_all(): all_vers = VersionRange.from_string("vers:all/*") assert all_vers.contains(Version("1.2.3")) @@ -558,8 +559,9 @@ def test_version_range_all(): with pytest.raises(Exception): VersionRange.from_string("vers:all/*|>1.2.3") + def test_version_range_none(): - none_vers = VersionRange.from_string("vers:none/*") + none_vers = VersionRange.from_string("vers:none/*") assert not none_vers.contains(Version("1.2.3")) assert PypiVersion("2.0.3") not in none_vers # test for invalid all range specification From 326d8698412365a31c82e8f894c106115d3987ce Mon Sep 17 00:00:00 2001 From: "Kunz, Immanuel" Date: Fri, 15 Nov 2024 11:32:35 +0100 Subject: [PATCH 6/6] import sorting code style Signed-off-by: Kunz, Immanuel --- src/univers/version_range.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/univers/version_range.py b/src/univers/version_range.py index 8f0194e8..bee601ef 100644 --- a/src/univers/version_range.py +++ b/src/univers/version_range.py @@ -19,10 +19,10 @@ from univers import versions from univers.conan.version_range import VersionRange as conan_version_range from univers.utils import remove_spaces -from univers.versions import AllVersion -from univers.versions import NoneVersion from univers.version_constraint import VersionConstraint from univers.version_constraint import contains_version +from univers.versions import AllVersion +from univers.versions import NoneVersion class InvalidVersionRange(Exception):