Skip to content

Commit

Permalink
Make nuget version hashable
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Apr 11, 2023
1 parent 08959e2 commit 19de006
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/univers/nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_nuget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import unittest

from univers.nuget import Version
from univers.versions import NugetVersion


Expand Down Expand Up @@ -77,3 +78,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)
17 changes: 17 additions & 0 deletions tests/test_python_semver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# 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)

0 comments on commit 19de006

Please sign in to comment.