|
| 1 | +# This file is part of CycloneDX Python |
| 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 os.path import join |
| 19 | +from tempfile import TemporaryDirectory |
| 20 | +from unittest import TestCase |
| 21 | + |
| 22 | +from cyclonedx.factory.license import LicenseFactory |
| 23 | +from cyclonedx.model import Encoding |
| 24 | +from cyclonedx.model.license import DisjunctiveLicense, LicenseAcknowledgement |
| 25 | +from ddt import ddt, named_data |
| 26 | + |
| 27 | +from cyclonedx_py._internal.utils.pep621 import project2licenses |
| 28 | + |
| 29 | + |
| 30 | +@ddt() |
| 31 | +class TestUtilsPEP621(TestCase): |
| 32 | + |
| 33 | + def test_project2licenses_license_dict_text(self) -> None: |
| 34 | + project = { |
| 35 | + 'name': 'testpkg', |
| 36 | + 'license': {'text': 'This is the license text.'}, |
| 37 | + } |
| 38 | + lfac = LicenseFactory() |
| 39 | + with TemporaryDirectory() as tmpdir: |
| 40 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
| 41 | + self.assertEqual(len(licenses), 1) |
| 42 | + lic = licenses[0] |
| 43 | + self.assertIsInstance(lic, DisjunctiveLicense) |
| 44 | + self.assertIsNone(lic.id) |
| 45 | + self.assertIsNone(lic.text.encoding) |
| 46 | + self.assertEqual(lic.text.content, 'This is the license text.') |
| 47 | + self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
| 48 | + |
| 49 | + def test_project2licenses_license_dict_file(self) -> None: |
| 50 | + project = { |
| 51 | + 'name': 'testpkg', |
| 52 | + 'license': {'file': 'license.txt'}, |
| 53 | + } |
| 54 | + lfac = LicenseFactory() |
| 55 | + with TemporaryDirectory() as tmpdir: |
| 56 | + with open(join(tmpdir, project['license']['file']), 'w') as tf: |
| 57 | + tf.write('File license text') |
| 58 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
| 59 | + self.assertEqual(len(licenses), 1) |
| 60 | + lic = licenses[0] |
| 61 | + self.assertIsInstance(lic, DisjunctiveLicense) |
| 62 | + self.assertIs(lic.text.encoding, Encoding.BASE_64) |
| 63 | + self.assertEqual(lic.text.content, 'RmlsZSBsaWNlbnNlIHRleHQ=') |
| 64 | + self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
| 65 | + |
| 66 | + @named_data( |
| 67 | + ('none', None), |
| 68 | + ('string', 'MIT'), |
| 69 | + ('list', ['MIT', 'Apache-2.0']) |
| 70 | + ) |
| 71 | + def test_project2licenses_license_non_dict(self, license: any) -> None: |
| 72 | + project = { |
| 73 | + 'name': 'testpkg', |
| 74 | + 'license': license, |
| 75 | + } |
| 76 | + lfac = LicenseFactory() |
| 77 | + with TemporaryDirectory() as tmpdir: |
| 78 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
| 79 | + self.assertEqual(len(licenses), 0) |
0 commit comments