From 253812c1a486a05624a919c91b16a2b617908f42 Mon Sep 17 00:00:00 2001 From: Keshav Priyadarshi Date: Tue, 17 May 2022 15:24:45 +0530 Subject: [PATCH] make nuget version hashable Signed-off-by: Keshav Priyadarshi --- src/univers/nuget.py | 8 ++++++++ tests/test_nuget.py | 11 +++++++++++ tests/test_python_semver.py | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/test_python_semver.py diff --git a/src/univers/nuget.py b/src/univers/nuget.py index eb34c877..ff9ab0ce 100644 --- a/src/univers/nuget.py +++ b/src/univers/nuget.py @@ -175,6 +175,14 @@ def __lt__(self, other): # Revision is the same, so ignore it for comparison purposes. return self._base_semver < other._base_semver + def __hash__(self): + return hash( + ( + self._base_semver.to_tuple(), + self._revision, + ) + ) + @classmethod def from_string(cls, str_version): if not str_version: diff --git a/tests/test_nuget.py b/tests/test_nuget.py index f4b9d2a9..bbf116e1 100644 --- a/tests/test_nuget.py +++ b/tests/test_nuget.py @@ -19,6 +19,7 @@ import unittest from univers.versions import NugetVersion +from univers.nuget import Version class NuGetTest(unittest.TestCase): @@ -76,3 +77,13 @@ def test_less(self): self.check_order(self.assertLess, "1.0.0-pre", "1.0.0.1-alpha") self.check_order(self.assertLess, "1.0.0", "1.0.0.1-alpha") self.check_order(self.assertLess, "0.9.9.1", "1.0.0") + + def test_NugetVersion_hash(self): + vers1 = NugetVersion("1.0.1+23") + vers2 = NugetVersion("1.0.1+23") + assert hash(vers1) == hash(vers2) + + def test_nuget_semver_hash(self): + vers1 = Version.from_string("51.0.0+2") + vers2 = Version.from_string("51.0.0+2") + assert hash(vers1) == hash(vers2) diff --git a/tests/test_python_semver.py b/tests/test_python_semver.py new file mode 100644 index 00000000..67da1462 --- /dev/null +++ b/tests/test_python_semver.py @@ -0,0 +1,16 @@ +# +# Copyright (c) nexB Inc. and others. +# SPDX-License-Identifier: Apache-2.0 +# +# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download. + +from unittest import TestCase +import semver + + +class TestPythonSemver(TestCase): + def test_semver_hash(self): + # python-semver doesn't consider build while hashing + vers1 = semver.VersionInfo.parse("1.2.3") + vers2 = semver.VersionInfo.parse("1.2.3+1") + assert hash(vers1) == hash(vers2)