Skip to content

Commit

Permalink
fix: LicenseChoiceFactory.make_from_string() prioritize SPDX id ove…
Browse files Browse the repository at this point in the history
…r expression

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck committed Sep 6, 2023
1 parent 4092717 commit 701e09e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
12 changes: 10 additions & 2 deletions cyclonedx/factory/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ def __init__(self, *, license_factory: LicenseFactory) -> None:
self.license_factory = license_factory

def make_from_string(self, expression_or_name_or_spdx: str) -> LicenseChoice:
"""Make a :class:`cyclonedx.model.LicenseChoice` from a string."""
"""Make a :class:`cyclonedx.model.LicenseChoice` from a string.
Priority: SPDX license ID, SPDX license expression, named license
"""
try:
return LicenseChoice(license=self.license_factory.make_with_id(expression_or_name_or_spdx))
except InvalidSpdxLicenseException:
pass
try:
return self.make_with_compound_expression(expression_or_name_or_spdx)
except InvalidLicenseExpressionException:
return self.make_with_license(expression_or_name_or_spdx)
pass
return LicenseChoice(license=self.license_factory.make_with_name(expression_or_name_or_spdx))

def make_with_compound_expression(self, compound_expression: str) -> LicenseChoice:
"""Make a :class:`cyclonedx.model.LicenseChoice` with a compound expression.
Expand Down
29 changes: 23 additions & 6 deletions tests/test_factory_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,46 @@ def test_make_with_name(self) -> None:

class TestFactoryLicenseChoice(unittest.TestCase):

def test_make_from_string_with_license_id(self) -> None:
license_ = unittest.mock.NonCallableMock(spec=License)
expected = LicenseChoice(license=license_)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
license_factory.make_with_id.return_value = license_
factory = LicenseChoiceFactory(license_factory=license_factory)

actual = factory.make_from_string('foo')

self.assertEqual(expected, actual)
self.assertIs(license_, actual.license)
license_factory.make_with_id.assert_called_once_with('foo')

def test_make_from_string_with_compound_expression(self) -> None:
expected = LicenseChoice(expression='foo')
factory = LicenseChoiceFactory(license_factory=unittest.mock.MagicMock(spec=LicenseFactory))
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
license_factory.make_with_id.side_effect = InvalidSpdxLicenseException('foo')
factory = LicenseChoiceFactory(license_factory=license_factory)

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

self.assertEqual(expected, actual)
license_factory.make_with_id.assert_called_once_with('foo')

def test_make_from_string_with_license(self) -> None:
def test_make_from_string_with_license_name(self) -> None:
license_ = unittest.mock.NonCallableMock(spec=License)
expected = LicenseChoice(license=license_)
license_factory = unittest.mock.MagicMock(spec=LicenseFactory)
license_factory.make_from_string.return_value = license_
license_factory.make_with_id.side_effect = InvalidSpdxLicenseException('foo')
license_factory.make_with_name.return_value = license_
factory = LicenseChoiceFactory(license_factory=license_factory)

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

self.assertEqual(expected, actual)
self.assertIs(license_, actual.license)
license_factory.make_from_string.assert_called_once_with('foo', license_text=None, license_url=None)
license_factory.make_with_id.assert_called_once_with('foo')
license_factory.make_with_name.assert_called_once_with('foo')

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

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

self.assertEqual(expected, actual)
self.assertIs(license_, actual.license)
Expand Down

0 comments on commit 701e09e

Please sign in to comment.