Skip to content

Commit df43a9b

Browse files
committed
test: refactored fixtures for tests which has uncovered #150, #151 and #152
Signed-off-by: Paul Horton <[email protected]>
1 parent b45ff18 commit df43a9b

File tree

62 files changed

+1613
-1005
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1613
-1005
lines changed

cyclonedx/exception/output.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ class ComponentVersionRequiredException(CycloneDxException):
2828
but one is not available/present.
2929
"""
3030
pass
31+
32+
33+
class FormatNotSupportedException(CycloneDxException):
34+
"""
35+
Exception raised when attempting to output a BOM to a format not supported in the requested version.
36+
37+
For example, JSON is not supported prior to 1.2.
38+
"""
39+
pass

cyclonedx/output/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ class SchemaVersion(Enum):
4040
V1_3: str = 'V1Dot3'
4141
V1_4: str = 'V1Dot4'
4242

43+
def to_version(self) -> str:
44+
"""
45+
Return as a version string - e.g. `1.4`
46+
47+
Returns:
48+
`str` version
49+
"""
50+
return f'{self.value[1]}.{self.value[5]}'
51+
4352

4453
DEFAULT_SCHEMA_VERSION = SchemaVersion.V1_3
4554

@@ -51,6 +60,11 @@ def __init__(self, bom: Bom, **kwargs: int) -> None:
5160
self._bom = bom
5261
self._generated: bool = False
5362

63+
@property
64+
@abstractmethod
65+
def schema_version(self) -> SchemaVersion:
66+
pass
67+
5468
@property
5569
def generated(self) -> bool:
5670
return self._generated

cyclonedx/output/json.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
from abc import abstractmethod
2222
from typing import cast, Any, Dict, List, Optional, Union
2323

24-
from . import BaseOutput
24+
from . import BaseOutput, SchemaVersion
2525
from .schema import BaseSchemaVersion, SchemaVersion1Dot0, SchemaVersion1Dot1, SchemaVersion1Dot2, SchemaVersion1Dot3, \
2626
SchemaVersion1Dot4
2727
from .serializer.json import CycloneDxJSONEncoder
28+
from ..exception.output import FormatNotSupportedException
2829
from ..model.bom import Bom
2930
from ..model.component import Component
3031

31-
3232
ComponentDict = Dict[str, Union[
3333
str,
3434
List[Dict[str, str]],
@@ -42,14 +42,19 @@ def __init__(self, bom: Bom) -> None:
4242
super().__init__(bom=bom)
4343
self._json_output: str = ''
4444

45+
@property
46+
def schema_version(self) -> SchemaVersion:
47+
return self.schema_version_enum
48+
4549
def generate(self, force_regeneration: bool = False) -> None:
4650
if self.generated and not force_regeneration:
4751
return
4852

4953
schema_uri: Optional[str] = self._get_schema_uri()
5054
if not schema_uri:
51-
# JSON not supported!
52-
return
55+
raise FormatNotSupportedException(
56+
f'JSON is not supported by CycloneDX in schema version {self.schema_version.to_version()}'
57+
)
5358

5459
vulnerabilities: Dict[str, List[Dict[Any, Any]]] = {"vulnerabilities": []}
5560
if self.get_bom().components:

cyclonedx/output/schema.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919

2020
from abc import ABC, abstractmethod
2121

22+
from . import SchemaVersion
23+
2224

2325
class BaseSchemaVersion(ABC):
2426

27+
@property
28+
@abstractmethod
29+
def schema_version_enum(self) -> SchemaVersion:
30+
pass
31+
2532
def bom_supports_metadata(self) -> bool:
2633
return True
2734

@@ -74,6 +81,10 @@ def get_schema_version(self) -> str:
7481

7582
class SchemaVersion1Dot4(BaseSchemaVersion):
7683

84+
@property
85+
def schema_version_enum(self) -> SchemaVersion:
86+
return SchemaVersion.V1_4
87+
7788
def get_schema_version(self) -> str:
7889
return '1.4'
7990

@@ -83,6 +94,10 @@ def component_version_optional(self) -> bool:
8394

8495
class SchemaVersion1Dot3(BaseSchemaVersion):
8596

97+
@property
98+
def schema_version_enum(self) -> SchemaVersion:
99+
return SchemaVersion.V1_3
100+
86101
def bom_metadata_supports_tools_external_references(self) -> bool:
87102
return False
88103

@@ -107,6 +122,10 @@ def get_schema_version(self) -> str:
107122

108123
class SchemaVersion1Dot2(BaseSchemaVersion):
109124

125+
@property
126+
def schema_version_enum(self) -> SchemaVersion:
127+
return SchemaVersion.V1_2
128+
110129
def bom_metadata_supports_tools_external_references(self) -> bool:
111130
return False
112131

@@ -134,6 +153,10 @@ def get_schema_version(self) -> str:
134153

135154
class SchemaVersion1Dot1(BaseSchemaVersion):
136155

156+
@property
157+
def schema_version_enum(self) -> SchemaVersion:
158+
return SchemaVersion.V1_1
159+
137160
def bom_metadata_supports_tools(self) -> bool:
138161
return False
139162

@@ -173,6 +196,10 @@ def get_schema_version(self) -> str:
173196

174197
class SchemaVersion1Dot0(BaseSchemaVersion):
175198

199+
@property
200+
def schema_version_enum(self) -> SchemaVersion:
201+
return SchemaVersion.V1_0
202+
176203
def bom_metadata_supports_tools(self) -> bool:
177204
return False
178205

cyclonedx/output/xml.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from typing import cast, List, Optional
2222
from xml.etree import ElementTree
2323

24-
from . import BaseOutput
24+
from . import BaseOutput, SchemaVersion
2525
from .schema import BaseSchemaVersion, SchemaVersion1Dot0, SchemaVersion1Dot1, SchemaVersion1Dot2, SchemaVersion1Dot3, \
2626
SchemaVersion1Dot4
2727
from ..exception.output import ComponentVersionRequiredException
@@ -39,6 +39,10 @@ def __init__(self, bom: Bom) -> None:
3939
super().__init__(bom=bom)
4040
self._root_bom_element: ElementTree.Element = self._create_bom_element()
4141

42+
@property
43+
def schema_version(self) -> SchemaVersion:
44+
return self.schema_version_enum
45+
4246
def generate(self, force_regeneration: bool = False) -> None:
4347
if self.generated and force_regeneration:
4448
self._root_bom_element = self._create_bom_element()

0 commit comments

Comments
 (0)