Skip to content

Commit 28dcbf7

Browse files
authored
fix: license file detection according to PEP621 (#929)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 78fbbc9 commit 28dcbf7

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

cyclonedx_py/_internal/utils/pep621.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from collections.abc import Generator, Iterable, Iterator
2727
from itertools import chain
2828
from os.path import dirname, join
29+
from pathlib import PurePosixPath
2930
from typing import TYPE_CHECKING, Any
3031

3132
from cyclonedx.exception.model import InvalidUriException
@@ -72,7 +73,7 @@ def project2licenses(project: dict[str, Any], lfac: 'LicenseFactory', *,
7273
# per spec:
7374
# > [...] a string value that is a relative file path [...].
7475
# > Tools MUST assume the file’s encoding is UTF-8.
75-
with open(join(dirname(fpath), plicense['file']), 'rb') as plicense_fileh:
76+
with open(join(dirname(fpath), *PurePosixPath(plicense['file']).parts), 'rb') as plicense_fileh:
7677
yield DisjunctiveLicense(name=f"declared license of '{project['name']}'",
7778
acknowledgement=lack,
7879
text=AttachedText(encoding=Encoding.BASE_64,

tests/integration/test_utils_pep621.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# SPDX-License-Identifier: Apache-2.0
1616
# Copyright (c) OWASP Foundation. All Rights Reserved.
1717

18+
from os import mkdir
1819
from os.path import join
1920
from tempfile import TemporaryDirectory
2021
from unittest import TestCase
@@ -53,7 +54,25 @@ def test_project2licenses_license_dict_file(self) -> None:
5354
}
5455
lfac = LicenseFactory()
5556
with TemporaryDirectory() as tmpdir:
56-
with open(join(tmpdir, project['license']['file']), 'w') as tf:
57+
with open(join(tmpdir, 'license.txt'), 'w') as tf:
58+
tf.write('File license text')
59+
licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml')))
60+
self.assertEqual(len(licenses), 1)
61+
lic = licenses[0]
62+
self.assertIsInstance(lic, DisjunctiveLicense)
63+
self.assertIs(lic.text.encoding, Encoding.BASE_64)
64+
self.assertEqual(lic.text.content, 'RmlsZSBsaWNlbnNlIHRleHQ=')
65+
self.assertEqual(lic.acknowledgement, LicenseAcknowledgement.DECLARED)
66+
67+
def test_project2licenses_license_dict_file_in_subfolder(self) -> None:
68+
project = {
69+
'name': 'testpkg',
70+
'license': {'file': 'foo/license.txt'},
71+
}
72+
lfac = LicenseFactory()
73+
with TemporaryDirectory() as tmpdir:
74+
mkdir(join(tmpdir, 'foo'))
75+
with open(join(tmpdir, 'foo', 'license.txt'), 'w') as tf:
5776
tf.write('File license text')
5877
licenses = list(project2licenses(project, lfac, fpath=join(tmpdir, 'pyproject.toml')))
5978
self.assertEqual(len(licenses), 1)

0 commit comments

Comments
 (0)