Skip to content

Commit 2b47ff6

Browse files
Fix warnings.
Change tuple -> Tuple Fix Diff initialization Add sorting to AttachedText Signed-off-by: Rodney Richardson <[email protected]>
1 parent ef0fbe2 commit 2b47ff6

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

cyclonedx/model/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import warnings
2222
from datetime import datetime
2323
from enum import Enum
24-
from typing import Any, Iterable, Optional
24+
from typing import Any, Iterable, Optional, Tuple, TypeVar
2525

2626
from sortedcontainers import SortedSet
2727

@@ -59,7 +59,10 @@ def sha1sum(filename: str) -> str:
5959
return h.hexdigest()
6060

6161

62-
class ComparableTuple(tuple):
62+
_T = TypeVar('_T')
63+
64+
65+
class ComparableTuple(Tuple[_T, ...]):
6366
"""
6467
Allows comparison of tuples, allowing for None values.
6568
"""
@@ -244,6 +247,12 @@ def __eq__(self, other: object) -> bool:
244247
return hash(other) == hash(self)
245248
return False
246249

250+
def __lt__(self, other: Any) -> bool:
251+
if isinstance(other, AttachedText):
252+
return ComparableTuple((self.content_type, self.content, self.encoding)) < \
253+
ComparableTuple((other.content_type, other.content, other.encoding))
254+
return NotImplemented
255+
247256
def __hash__(self) -> int:
248257
return hash((self.content, self.content_type, self.encoding))
249258

tests/test_model_component.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from cyclonedx.model import (
3838
AttachedText,
3939
Copyright,
40+
Encoding,
4041
ExternalReference,
4142
ExternalReferenceType,
4243
IdentifiableAction,
@@ -364,21 +365,41 @@ def test_not_same(self) -> None:
364365
self.assertFalse(diff_1 == diff_2)
365366

366367
def test_sort(self) -> None:
368+
text_a = AttachedText(content='a')
369+
text_b = AttachedText(content='b')
370+
367371
# expected sort order: ([url], [text])
368372
expected_order = [1, 0, 5, 2, 3, 4]
369373
diffs = [
370-
Diff(url=XsUri('a'), text='b'),
371-
Diff(url=XsUri('a'), text='a'),
372-
Diff(url=XsUri('b'), text='a'),
373-
Diff(text='a'),
374-
Diff(text='b'),
374+
Diff(url=XsUri('a'), text=text_b),
375+
Diff(url=XsUri('a'), text=text_a),
376+
Diff(url=XsUri('b'), text=text_a),
377+
Diff(text=text_a),
378+
Diff(text=text_b),
375379
Diff(url=XsUri('a')),
376380
]
377381
sorted_diffs = sorted(diffs)
378382
expected_diffs = reorder(diffs, expected_order)
379383
self.assertListEqual(sorted_diffs, expected_diffs)
380384

381385

386+
class TestModelAttachedText(TestCase):
387+
388+
def test_sort(self) -> None:
389+
# expected sort order: (content_type, content, encoding)
390+
expected_order = [0, 4, 2, 1, 3]
391+
text = [
392+
AttachedText(content='a', content_type='a', encoding=Encoding.BASE_64),
393+
AttachedText(content='a', content_type='b', encoding=Encoding.BASE_64),
394+
AttachedText(content='b', content_type='a', encoding=Encoding.BASE_64),
395+
AttachedText(content='b', content_type='b', encoding=Encoding.BASE_64),
396+
AttachedText(content='a', content_type='a'),
397+
]
398+
sorted_text = sorted(text)
399+
expected_text = reorder(text, expected_order)
400+
self.assertListEqual(sorted_text, expected_text)
401+
402+
382403
class TestModelPatch(TestCase):
383404

384405
def test_same_1(self) -> None:
@@ -425,8 +446,8 @@ def test_not_same_1(self) -> None:
425446
self.assertFalse(p1 == p2)
426447

427448
def test_sort(self) -> None:
428-
diff_a = Diff(text='a')
429-
diff_b = Diff(text='b')
449+
diff_a = Diff(text=AttachedText(content='a'))
450+
diff_b = Diff(text=AttachedText(content='b'))
430451

431452
resolves_a = [
432453
IssueType(classification=IssueClassification.DEFECT),

0 commit comments

Comments
 (0)