|
15 | 15 | # SPDX-License-Identifier: Apache-2.0 |
16 | 16 | # Copyright (c) OWASP Foundation. All Rights Reserved. |
17 | 17 |
|
18 | | -import os |
19 | | -import tempfile |
20 | | -import unittest |
| 18 | +from os.path import join |
| 19 | +from tempfile import TemporaryDirectory |
| 20 | +from unittest import TestCase |
21 | 21 |
|
22 | 22 | from cyclonedx.factory.license import LicenseFactory |
23 | 23 | from cyclonedx.model.license import DisjunctiveLicense, LicenseAcknowledgement |
| 24 | +from ddt import ddt, named_data |
24 | 25 |
|
25 | 26 | from cyclonedx_py._internal.utils.pep621 import project2licenses |
26 | 27 |
|
27 | 28 |
|
28 | | -class TestUtilsPEP621(unittest.TestCase): |
| 29 | +@ddt() |
| 30 | +class TestUtilsPEP621(TestCase): |
29 | 31 |
|
30 | 32 | def test_license_dict_text_pep621(self) -> None: |
31 | | - lfac = LicenseFactory() |
32 | | - with tempfile.TemporaryDirectory() as tmpdir: |
33 | | - fpath = tmpdir # Use the temp directory as the base for any temp files |
34 | | - project = { |
35 | | - 'name': 'testpkg', |
36 | | - 'license': {'text': 'This is the license text.'}, |
37 | | - } |
38 | | - licenses = list(project2licenses(project, lfac, fpath=fpath)) |
39 | | - self.assertEqual(len(licenses), 1) |
40 | | - lic = licenses[0] |
41 | | - self.assertIsInstance(lic, DisjunctiveLicense) |
42 | | - self.assertIsNone(lic.id) |
43 | | - self.assertEqual(lic.text.content, 'This is the license text.') |
44 | | - self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
45 | | - |
46 | | - def test_license_dict_file_pep621(self) -> None: |
47 | | - lfac = LicenseFactory() |
48 | | - with tempfile.TemporaryDirectory() as tmpdir: |
49 | | - file_path = os.path.join(tmpdir, 'license.txt') |
50 | | - with open(file_path, 'w') as tf: |
51 | | - tf.write('File license text') |
52 | | - project = { |
53 | | - 'name': 'testpkg', |
54 | | - 'license': {'file': 'license.txt'}, |
55 | | - } |
56 | | - licenses = list(project2licenses(project, lfac, fpath=file_path)) |
57 | | - self.assertEqual(len(licenses), 1) |
58 | | - lic = licenses[0] |
59 | | - self.assertIsInstance(lic, DisjunctiveLicense) |
60 | | - self.assertIsNotNone(lic.text.content) |
61 | | - self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
62 | | - |
63 | | - def test_license_non_dict_pep621(self) -> None: |
64 | | - lfac = LicenseFactory() |
65 | | - fpath = tempfile.mktemp() |
66 | | - |
67 | | - # Test with string license (should be silently skipped) |
68 | 33 | project = { |
69 | 34 | 'name': 'testpkg', |
70 | | - 'license': 'MIT', |
| 35 | + 'license': {'text': 'This is the license text.'}, |
71 | 36 | } |
72 | | - licenses = list(project2licenses(project, lfac, fpath=fpath)) |
73 | | - self.assertEqual(len(licenses), 0) |
| 37 | + lfac = LicenseFactory() |
| 38 | + with TemporaryDirectory() as tmpdir: |
| 39 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
| 40 | + self.assertEqual(len(licenses), 1) |
| 41 | + lic = licenses[0] |
| 42 | + self.assertIsInstance(lic, DisjunctiveLicense) |
| 43 | + self.assertIsNone(lic.id) |
| 44 | + self.assertEqual(lic.text.content, 'This is the license text.') |
| 45 | + self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
74 | 46 |
|
75 | | - # Test with None license (should be silently skipped) |
| 47 | + def test_license_dict_file_pep621(self) -> None: |
76 | 48 | project = { |
77 | 49 | 'name': 'testpkg', |
78 | | - 'license': None, |
| 50 | + 'license': {'file': 'license.txt'}, |
79 | 51 | } |
80 | | - licenses = list(project2licenses(project, lfac, fpath=fpath)) |
81 | | - self.assertEqual(len(licenses), 0) |
| 52 | + lfac = LicenseFactory() |
| 53 | + with TemporaryDirectory() as tmpdir: |
| 54 | + with open(join(tmpdir, project['license']['file']), 'w') as tf: |
| 55 | + tf.write('File license text') |
| 56 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
| 57 | + self.assertEqual(len(licenses), 1) |
| 58 | + lic = licenses[0] |
| 59 | + self.assertIsInstance(lic, DisjunctiveLicense) |
| 60 | + self.assertIsNotNone(lic.text.content) |
| 61 | + self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED) |
82 | 62 |
|
83 | | - # Test with list license (should be silently skipped) |
| 63 | + @named_data( |
| 64 | + ('none', None), |
| 65 | + ('string', 'MIT'), |
| 66 | + ('list', ['MIT', 'Apache-2.0']) |
| 67 | + ) |
| 68 | + def test_license_non_dict_pep621(self, license: any) -> None: |
84 | 69 | project = { |
85 | 70 | 'name': 'testpkg', |
86 | | - 'license': ['MIT', 'Apache-2.0'], |
| 71 | + 'license': license, |
87 | 72 | } |
88 | | - licenses = list(project2licenses(project, lfac, fpath=fpath)) |
| 73 | + lfac = LicenseFactory() |
| 74 | + with TemporaryDirectory() as tmpdir: |
| 75 | + licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml'))) |
89 | 76 | self.assertEqual(len(licenses), 0) |
0 commit comments