Skip to content

Commit e1bdfdd

Browse files
authored
fix: LicenseChoiceFactory.make_from_string() prioritize SPDX id over expression (#427)
Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
1 parent 4092717 commit e1bdfdd

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

cyclonedx/factory/license.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,19 @@ def __init__(self, *, license_factory: LicenseFactory) -> None:
5757
self.license_factory = license_factory
5858

5959
def make_from_string(self, expression_or_name_or_spdx: str) -> LicenseChoice:
60-
"""Make a :class:`cyclonedx.model.LicenseChoice` from a string."""
60+
"""Make a :class:`cyclonedx.model.LicenseChoice` from a string.
61+
62+
Priority: SPDX license ID, SPDX license expression, named license
63+
"""
64+
try:
65+
return LicenseChoice(license=self.license_factory.make_with_id(expression_or_name_or_spdx))
66+
except InvalidSpdxLicenseException:
67+
pass
6168
try:
6269
return self.make_with_compound_expression(expression_or_name_or_spdx)
6370
except InvalidLicenseExpressionException:
64-
return self.make_with_license(expression_or_name_or_spdx)
71+
pass
72+
return LicenseChoice(license=self.license_factory.make_with_name(expression_or_name_or_spdx))
6573

6674
def make_with_compound_expression(self, compound_expression: str) -> LicenseChoice:
6775
"""Make a :class:`cyclonedx.model.LicenseChoice` with a compound expression.

tests/test_factory_license.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,46 @@ def test_make_with_name(self) -> None:
7272

7373
class TestFactoryLicenseChoice(unittest.TestCase):
7474

75+
def test_make_from_string_with_license_id(self) -> None:
76+
license_ = unittest.mock.NonCallableMock(spec=License)
77+
expected = LicenseChoice(license=license_)
78+
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
79+
license_factory.make_with_id.return_value = license_
80+
factory = LicenseChoiceFactory(license_factory=license_factory)
81+
82+
actual = factory.make_from_string('foo')
83+
84+
self.assertEqual(expected, actual)
85+
self.assertIs(license_, actual.license)
86+
license_factory.make_with_id.assert_called_once_with('foo')
87+
7588
def test_make_from_string_with_compound_expression(self) -> None:
7689
expected = LicenseChoice(expression='foo')
77-
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
90+
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
91+
license_factory.make_with_id.side_effect = InvalidSpdxLicenseException('foo')
92+
factory = LicenseChoiceFactory(license_factory=license_factory)
7893

7994
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=True):
8095
actual = factory.make_from_string('foo')
8196

8297
self.assertEqual(expected, actual)
98+
license_factory.make_with_id.assert_called_once_with('foo')
8399

84-
def test_make_from_string_with_license(self) -> None:
100+
def test_make_from_string_with_license_name(self) -> None:
85101
license_ = unittest.mock.NonCallableMock(spec=License)
86102
expected = LicenseChoice(license=license_)
87103
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
88-
license_factory.make_from_string.return_value = license_
104+
license_factory.make_with_id.side_effect = InvalidSpdxLicenseException('foo')
105+
license_factory.make_with_name.return_value = license_
89106
factory = LicenseChoiceFactory(license_factory=license_factory)
90107

91108
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
92109
actual = factory.make_from_string('foo')
93110

94111
self.assertEqual(expected, actual)
95112
self.assertIs(license_, actual.license)
96-
license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None)
113+
license_factory.make_with_id.assert_called_once_with('foo')
114+
license_factory.make_with_name.assert_called_once_with('foo')
97115

98116
def test_make_with_compound_expression(self) -> None:
99117
expected = LicenseChoice(expression='foo')
@@ -119,8 +137,7 @@ def test_make_with_license(self) -> None:
119137
license_factory.make_from_string.return_value = license_
120138
factory = LicenseChoiceFactory(license_factory=license_factory)
121139

122-
with unittest.mock.patch('cyclonedx.factory.license.is_spdx_compound_expression', return_value=False):
123-
actual = factory.make_with_license('foo', license_text=text, license_url=url)
140+
actual = factory.make_with_license('foo', license_text=text, license_url=url)
124141

125142
self.assertEqual(expected, actual)
126143
self.assertIs(license_, actual.license)

0 commit comments

Comments
 (0)