diff --git a/src/univers/versions.py b/src/univers/versions.py index 5d6101ac..9d4e4919 100644 --- a/src/univers/versions.py +++ b/src/univers/versions.py @@ -248,6 +248,8 @@ def build_value(cls, string): @classmethod def is_valid(cls, string): + if not super().is_valid(string): + return False try: cls.build_value(string) return True @@ -325,7 +327,7 @@ def build_value(cls, string): @classmethod def is_valid(cls, string): - return gem.GemVersion.is_correct(string) + return super().is_valid(string) and gem.GemVersion.is_correct(string) class ArchLinuxVersion(Version): @@ -375,6 +377,8 @@ def build_value(cls, string): @classmethod def is_valid(cls, string): + if not super().is_valid(string): + return False try: cls.build_value(string) return True @@ -392,6 +396,8 @@ def build_value(cls, string): @classmethod def is_valid(cls, string): + if not super().is_valid(string): + return False try: cls.build_value(string) return True @@ -664,6 +670,8 @@ def build_value(cls, string): @classmethod def is_valid(cls, string): + if not super().is_valid(string): + return False try: cls.build_value(string) return True diff --git a/tests/test_versions.py b/tests/test_versions.py index ab3007d3..b8f9b1d1 100644 --- a/tests/test_versions.py +++ b/tests/test_versions.py @@ -253,3 +253,26 @@ def test_lexicographic_version(): assert LexicographicVersion("Abc") < LexicographicVersion(None) assert LexicographicVersion("123") < LexicographicVersion("bbc") assert LexicographicVersion("2.3.4") > LexicographicVersion("1.2.3") + + +def test_version_with_empty_string_is_invalid(): + # https://github.com/aboutcode-org/univers/issues/204 + # These schemes used to accept the empty string and then sort it as the + # minimum version (Maven, Rubygems), raise on comparison (Nuget), or + # both (Conan), silently corrupting downstream range logic. + import pytest + + from univers.versions import ConanVersion + from univers.versions import InvalidVersion + + for version_class in ( + Version, + MavenVersion, + NugetVersion, + RubygemsVersion, + ConanVersion, + SemverVersion, + PypiVersion, + ): + with pytest.raises(InvalidVersion): + version_class("")