Skip to content

Commit

Permalink
get rid of tool-repo mutial exclusive tool vs component/service
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Aug 16, 2024
1 parent e9fe527 commit 59b0987
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 51 deletions.
29 changes: 1 addition & 28 deletions cyclonedx/model/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,9 @@ def __init__(
self, *,
components: Optional[Iterable[Component]] = None,
services: Optional[Iterable[Service]] = None,
# Deprecated in v1.5
# Deprecated since v1.5
tools: Optional[Iterable[Tool]] = None
) -> None:
if tools and (components or services):
# Must use components/services or tools. Cannot use both
raise MutuallyExclusivePropertiesException(
'Cannot define both old (CycloneDX <= 1.4) and new '
'(CycloneDX >= 1.5) format for tools.'
)

if tools:
warn('Using Tool is deprecated as of CycloneDX v1.5. Components and Services should be used now. '
'See https://cyclonedx.org/docs/1.5/', DeprecationWarning)
Expand All @@ -202,12 +195,6 @@ def components(self) -> 'SortedSet[Component]':

@components.setter
def components(self, components: Iterable[Component]) -> None:
if self._tools:
raise MutuallyExclusivePropertiesException(
'Cannot define both old (CycloneDX <= 1.4) and new '
'(CycloneDX >= 1.5) format for tools.'
)

self._components = SortedSet(components)

@property
Expand All @@ -220,28 +207,14 @@ def services(self) -> 'SortedSet[Service]':

@services.setter
def services(self, services: Iterable[Service]) -> None:
if self._tools:
raise MutuallyExclusivePropertiesException(
'Cannot define both old (CycloneDX <= 1.4) and new '
'(CycloneDX >= 1.5) format for tools.'
)
self._services = SortedSet(services)

@property
def tools(self) -> 'SortedSet[Tool]':
"""
Returns:
A SortedSet of Tools
"""
return self._tools

@tools.setter
def tools(self, tools: Iterable[Tool]) -> None:
if self._components or self._services:
raise MutuallyExclusivePropertiesException(
'Cannot define both old (CycloneDX <= 1.4) and new '
'(CycloneDX >= 1.5) format for tools.'
)
self._tools = SortedSet(tools)

def __len__(self) -> int:
Expand Down
32 changes: 9 additions & 23 deletions tests/test_model_tool_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_tool_with_component_and_service_load_json(self) -> None:
'bom_with_tool_with_component_and_service.json')
with open(test_file, encoding='UTF-8') as f:
bom_json = json_loads(f.read())
bom = Bom.from_json(bom_json) # type: ignore[attr-defined]
bom = Bom.from_json(bom_json) # type: ignore[attr-defined]
self.assertTupleEqual(
tuple(bom.metadata.tools.components),
tuple(expected.metadata.tools.components), 'components')
Expand All @@ -68,23 +68,14 @@ def test_tool_with_component_and_service_load_xml(self) -> None:
tuple(bom.metadata.tools.tools),
tuple(expected.metadata.tools.tools), 'tools')

def test_invalid_tool_repo_properties(self) -> None:
with self.assertRaises(MutuallyExclusivePropertiesException):
ToolsRepository(
components=[Component(name='test-component')],
services=[Service(name='test-service')],
tools=[Tool(name='test-tool')]
)

def test_assign_component_with_existing_tool(self) -> None:
tr = ToolsRepository(tools=[Tool()])
with self.assertRaises(MutuallyExclusivePropertiesException):
tr.components = SortedSet([Component(name='test-component')])

def test_assign_service_with_existing_tool(self) -> None:
tr = ToolsRepository(tools=[Tool()])
with self.assertRaises(MutuallyExclusivePropertiesException):
tr.services = SortedSet([Service(name='test-service')])
def test_init(self) -> None:
cs = (Component(name='test-component'),)
ss = (Service(name='test-service'),)
ts = (Tool(name='test-tool'),)
tr = ToolsRepository(components=cs, services=ss, tools=ts)
self.assertTupleEqual(cs, tuple(tr.components))
self.assertTupleEqual(ss, tuple(tr.services))
self.assertTupleEqual(ts, tuple(tr.tools))

def test_unequal_different_type(self) -> None:
tr = ToolsRepository()
Expand Down Expand Up @@ -117,11 +108,6 @@ def test_equal(self) -> None:
tr2.tools.add(t)
self.assertTrue(tr1 == tr2)

def test_assign_tool_with_existing_component(self) -> None:
tr = ToolsRepository(components=SortedSet([Component(name='test-component')]))
with self.assertRaises(MutuallyExclusivePropertiesException):
tr.tools = SortedSet([Tool()])

def test_proper_service_provider_conversion(self) -> None:
o = OrganizationalEntity(name='test-org')
s = Service(name='test-service', provider=o)
Expand Down

0 comments on commit 59b0987

Please sign in to comment.