Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cyclonedx/contrib/component/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def make_for_file(self, absolute_file_path: str, *,
`Component` representing the supplied file
"""
if not exists(absolute_file_path):
raise FileExistsError(f'Supplied file path {absolute_file_path!r} does not exist')
raise FileNotFoundError(f'Supplied file path {absolute_file_path!r} does not exist')

return Component(
type=ComponentType.FILE,
Expand Down
45 changes: 45 additions & 0 deletions tests/test_contrib/test_component_builders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This file is part of CycloneDX Python Library
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

import os
from tempfile import TemporaryDirectory
from unittest import TestCase

from cyclonedx.contrib.component.builders import ComponentBuilder
from cyclonedx.model import HashAlgorithm
from cyclonedx.model.component import ComponentType


class TestComponentBuilderMakeForFile(TestCase):

def test_missing_file_raises_file_not_found(self) -> None:
with TemporaryDirectory() as tmp:
missing = os.path.join(tmp, 'missing.whl')
with self.assertRaises(FileNotFoundError):
ComponentBuilder().make_for_file(missing, name='missing')

def test_existing_file(self) -> None:
with TemporaryDirectory() as tmp:
path = os.path.join(tmp, 'file.txt')
with open(path, 'wb') as f:
f.write(b'abc')
component = ComponentBuilder().make_for_file(path, name='file')
self.assertIs(ComponentType.FILE, component.type)
self.assertEqual('file', component.name)
(hash_,) = component.hashes
self.assertIs(HashAlgorithm.SHA_1, hash_.alg)
self.assertEqual('a9993e364706816aba3e25717850c26c9cd0d89d', hash_.content)
Loading