Skip to content

Commit 19de006

Browse files
committed
Make nuget version hashable
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent 08959e2 commit 19de006

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/univers/nuget.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ def __lt__(self, other):
175175
# Revision is the same, so ignore it for comparison purposes.
176176
return self._base_semver < other._base_semver
177177

178+
def __hash__(self):
179+
return hash(
180+
(
181+
self._base_semver.to_tuple(),
182+
self._revision,
183+
)
184+
)
185+
178186
@classmethod
179187
def from_string(cls, str_version):
180188
if not str_version:

tests/test_nuget.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import unittest
2121

22+
from univers.nuget import Version
2223
from univers.versions import NugetVersion
2324

2425

@@ -77,3 +78,13 @@ def test_less(self):
7778
self.check_order(self.assertLess, "1.0.0-pre", "1.0.0.1-alpha")
7879
self.check_order(self.assertLess, "1.0.0", "1.0.0.1-alpha")
7980
self.check_order(self.assertLess, "0.9.9.1", "1.0.0")
81+
82+
def test_NugetVersion_hash(self):
83+
vers1 = NugetVersion("1.0.1+23")
84+
vers2 = NugetVersion("1.0.1+23")
85+
assert hash(vers1) == hash(vers2)
86+
87+
def test_nuget_semver_hash(self):
88+
vers1 = Version.from_string("51.0.0+2")
89+
vers2 = Version.from_string("51.0.0+2")
90+
assert hash(vers1) == hash(vers2)

tests/test_python_semver.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Copyright (c) nexB Inc. and others.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Visit https://aboutcode.org and https://github.com/nexB/univers for support and download.
6+
7+
from unittest import TestCase
8+
9+
import semver
10+
11+
12+
class TestPythonSemver(TestCase):
13+
def test_semver_hash(self):
14+
# python-semver doesn't consider build while hashing
15+
vers1 = semver.VersionInfo.parse("1.2.3")
16+
vers2 = semver.VersionInfo.parse("1.2.3+1")
17+
assert hash(vers1) == hash(vers2)

0 commit comments

Comments
 (0)