1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-09 22:36:14 +01:00

core.Package: add ordering methods, repr and hash

This commit is contained in:
Daniel Mach 2022-09-12 17:16:52 +02:00
parent de10c926ea
commit 8dbf859ce2
2 changed files with 76 additions and 1 deletions

View File

@ -28,7 +28,7 @@ import sys
import tempfile
import textwrap
import time
from functools import cmp_to_key
from functools import cmp_to_key, total_ordering
from http.client import IncompleteRead
from io import StringIO
from urllib.parse import urlsplit, urlunsplit, urlparse, quote_plus, urlencode, unquote
@ -1155,6 +1155,7 @@ class Project:
return Project(dir, getPackageList, progress_obj, wc_check)
@total_ordering
class Package:
"""represent a package (its directory) and read/keep/write its metadata"""
@ -1195,6 +1196,18 @@ class Package:
self.todo = []
def __repr__(self):
return super().__repr__() + f"({self.prjname}/{self.name})"
def __hash__(self):
return hash((self.name, self.prjname, self.apiurl))
def __eq__(self, other):
return (self.name, self.prjname, self.apiurl) == (other.name, other.prjname, other.apiurl)
def __lt__(self, other):
return (self.name, self.prjname, self.apiurl) < (other.name, other.prjname, other.apiurl)
def wc_check(self):
dirty_files = []
if self.scm_url:

View File

@ -0,0 +1,62 @@
import unittest
import osc.core
class PackageMock(osc.core.Package):
def __init__(self, apiurl, project_name, name):
"""
Let's override __init__ to avoid loading from a working copy.
"""
self.apiurl = apiurl
self.prjname = project_name
self.name = name
class TestPackage(unittest.TestCase):
def test_eq(self):
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projA", "pkgA")
self.assertEqual(p1, p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projA", "pkgB")
self.assertNotEqual(p1, p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projB", "pkgA")
self.assertNotEqual(p1, p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlB", "projA", "pkgA")
self.assertNotEqual(p1, p2)
def test_lt(self):
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projA", "pkgA")
self.assertFalse(p1 < p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projA", "pkgB")
self.assertTrue(p1 < p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projB", "pkgA")
self.assertTrue(p1 < p2)
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlB", "projA", "pkgA")
self.assertTrue(p1 < p2)
def test_hash(self):
p1 = PackageMock("http://urlA", "projA", "pkgA")
p2 = PackageMock("http://urlA", "projA", "pkgA")
packages = set()
packages.add(p1)
# the second instance appears to be there because it has the same hash
# it is ok, because we consider such packages equal
self.assertIn(p2, packages)
if __name__ == "__main__":
unittest.main()