Skip to content

Commit c3e59a8

Browse files
committed
tests: test .builder.this.this_tool
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 9940cf9 commit c3e59a8

File tree

4 files changed

+109
-16
lines changed

4 files changed

+109
-16
lines changed

cyclonedx/builder/this.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@ def this_component() -> Component:
3737
licenses=(DisjunctiveLicense(id='Apache-2.0',
3838
acknowledgement=LicenseAcknowledgement.DECLARED),),
3939
external_references=(
40+
# let's assume this is not a fork
4041
ExternalReference(
41-
type=ExternalReferenceType.BUILD_SYSTEM,
42-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
43-
),
44-
ExternalReference(
45-
type=ExternalReferenceType.DISTRIBUTION,
46-
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
42+
type=ExternalReferenceType.WEBSITE,
43+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
4744
),
4845
ExternalReference(
4946
type=ExternalReferenceType.DOCUMENTATION,
5047
url=XsUri('https://cyclonedx-python-library.readthedocs.io/')
5148
),
49+
ExternalReference(
50+
type=ExternalReferenceType.VCS,
51+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
52+
),
53+
ExternalReference(
54+
type=ExternalReferenceType.BUILD_SYSTEM,
55+
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/actions')
56+
),
5257
ExternalReference(
5358
type=ExternalReferenceType.ISSUE_TRACKER,
5459
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/issues')
@@ -61,13 +66,10 @@ def this_component() -> Component:
6166
type=ExternalReferenceType.RELEASE_NOTES,
6267
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md')
6368
),
69+
# we cannot assert where the lib was fetched from, but we can give a hint
6470
ExternalReference(
65-
type=ExternalReferenceType.VCS,
66-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib')
67-
),
68-
ExternalReference(
69-
type=ExternalReferenceType.WEBSITE,
70-
url=XsUri('https://github.com/CycloneDX/cyclonedx-python-lib/#readme')
71+
type=ExternalReferenceType.DISTRIBUTION,
72+
url=XsUri('https://pypi.org/project/cyclonedx-python-lib/')
7173
),
7274
),
7375
# to be extended...

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pep8-naming = "0.14.1"
9595
isort = "5.13.2"
9696
autopep8 = "2.3.1"
9797
mypy = "1.11.2"
98+
toml = { version="0.10.2", python="<3.11"}
9899
tox = "4.18.0"
99100
xmldiff = "2.7.0"
100101
bandit = "1.7.9"

tests/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
1718
import re
19+
import sys
1820
from os import getenv, path
19-
from os.path import basename, join, splitext
20-
from typing import TYPE_CHECKING, Any, Generator, Iterable, List, Optional, Tuple, TypeVar, Union
21+
from typing import TYPE_CHECKING, Any, Dict, Generator, Iterable, List, Optional, Tuple, TypeVar, Union
2122
from unittest import TestCase
2223
from uuid import UUID
2324

25+
if sys.version_info >= (3, 11):
26+
from tomllib import load as toml_load
27+
else:
28+
from toml import load as toml_load
29+
2430
from sortedcontainers import SortedSet
2531

2632
from cyclonedx.output import BomRefDiscriminator as _BomRefDiscriminator
@@ -47,7 +53,7 @@ class SnapshotMixin:
4753

4854
@staticmethod
4955
def getSnapshotFile(snapshot_name: str) -> str: # noqa: N802
50-
return join(SNAPSHOTS_DIRECTORY, f'{snapshot_name}.bin')
56+
return path.join(SNAPSHOTS_DIRECTORY, f'{snapshot_name}.bin')
5157

5258
@classmethod
5359
def writeSnapshot(cls, snapshot_name: str, data: str) -> None: # noqa: N802
@@ -189,4 +195,9 @@ class DpTuple(Tuple[SchemaVersion, str]):
189195
@property
190196
def __name__(self) -> str:
191197
schema_version, test_data_file = self
192-
return f'{schema_version.to_version()}-{splitext(basename(test_data_file))[0]}'
198+
return f'{schema_version.to_version()}-{path.splitext(path.basename(test_data_file))[0]}'
199+
200+
201+
def load_pyproject() -> Dict[str, Any]:
202+
with open(path.join(path.dirname(__file__), '..', 'pyproject.toml'), 'rb') as f:
203+
return toml_load(f)

tests/test_builder_this.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# This file is part of CycloneDX Python Lib
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
# Copyright (c) OWASP Foundation. All Rights Reserved.
17+
18+
from typing import Any, Dict, Iterable, Tuple
19+
from unittest import TestCase
20+
21+
from cyclonedx.builder.this import this_component, this_tool
22+
from cyclonedx.model import ExternalReference, ExternalReferenceType
23+
from cyclonedx.model.component import ComponentType
24+
from cyclonedx.model.license import License, LicenseAcknowledgement
25+
from tests import load_pyproject
26+
27+
28+
class TestThisBase(TestCase):
29+
30+
@staticmethod
31+
def __first_ers_uri(t: ExternalReferenceType, ers: Iterable[ExternalReference]) -> str:
32+
return next(filter(lambda r: r.type is t, ers)).url.uri
33+
34+
def assertExtRefs(self, p: Dict[str, Any], ers: Iterable[ExternalReference]) -> None: # noqa:N802
35+
self.assertEqual(p['tool']['poetry']['homepage'], self.__first_ers_uri(
36+
ExternalReferenceType.WEBSITE, ers))
37+
self.assertEqual(p['tool']['poetry']['repository'], self.__first_ers_uri(
38+
ExternalReferenceType.VCS, ers))
39+
self.assertEqual(p['tool']['poetry']['documentation'], self.__first_ers_uri(
40+
ExternalReferenceType.DOCUMENTATION, ers))
41+
42+
43+
class TestThisComponent(TestThisBase):
44+
def test_basics(self) -> None:
45+
p = load_pyproject()
46+
c = this_component()
47+
self.assertIs(ComponentType.LIBRARY, c.type)
48+
self.assertEqual('CycloneDX', c.group)
49+
self.assertEqual(p['tool']['poetry']['name'], c.name)
50+
self.assertEqual(p['tool']['poetry']['version'], c.version)
51+
self.assertEqual(p['tool']['poetry']['description'], c.description)
52+
53+
def test_license(self) -> None:
54+
p = load_pyproject()
55+
ls: Tuple[License, ...] = tuple(this_component().licenses)
56+
self.assertEqual(1, len(ls))
57+
l = ls[0] # noqa:E741
58+
self.assertIs(LicenseAcknowledgement.DECLARED, l.acknowledgement)
59+
# this uses the fact that poetry expect license declarations as valid SPDX-license-id
60+
self.assertEqual(p['tool']['poetry']['license'], l.id)
61+
62+
def test_extrefs(self) -> None:
63+
p = load_pyproject()
64+
ers: Tuple[ExternalReference, ...] = tuple(this_component().external_references)
65+
self.assertExtRefs(p, ers)
66+
67+
68+
class TestThisTool(TestThisBase):
69+
def test_basics(self) -> None:
70+
p = load_pyproject()
71+
t = this_tool()
72+
self.assertEqual('CycloneDX', t.vendor)
73+
self.assertEqual(p['tool']['poetry']['name'], t.name)
74+
self.assertEqual(p['tool']['poetry']['version'], t.version)
75+
76+
def test_extrefs(self) -> None:
77+
p = load_pyproject()
78+
ers: Tuple[ExternalReference, ...] = tuple(this_tool().external_references)
79+
self.assertExtRefs(p, ers)

0 commit comments

Comments
 (0)