Skip to content

Commit 107864e

Browse files
committed
tests
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent acbffb4 commit 107864e

File tree

1 file changed

+38
-51
lines changed

1 file changed

+38
-51
lines changed

tests/integration/test_utils_pep621.py

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,62 @@
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

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
2121

2222
from cyclonedx.factory.license import LicenseFactory
2323
from cyclonedx.model.license import DisjunctiveLicense, LicenseAcknowledgement
24+
from ddt import ddt, named_data
2425

2526
from cyclonedx_py._internal.utils.pep621 import project2licenses
2627

2728

28-
class TestUtilsPEP621(unittest.TestCase):
29+
@ddt()
30+
class TestUtilsPEP621(TestCase):
2931

3032
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)
6833
project = {
6934
'name': 'testpkg',
70-
'license': 'MIT',
35+
'license': {'text': 'This is the license text.'},
7136
}
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)
7446

75-
# Test with None license (should be silently skipped)
47+
def test_license_dict_file_pep621(self) -> None:
7648
project = {
7749
'name': 'testpkg',
78-
'license': None,
50+
'license': {'file': 'license.txt'},
7951
}
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)
8262

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:
8469
project = {
8570
'name': 'testpkg',
86-
'license': ['MIT', 'Apache-2.0'],
71+
'license': license,
8772
}
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')))
8976
self.assertEqual(len(licenses), 0)

0 commit comments

Comments
 (0)