File: test_license_expression_bump.py

package info (click to toggle)
python-spdx-tools 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,024 kB
  • sloc: python: 18,708; xml: 12,553; sh: 46; makefile: 6
file content (108 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import pytest
from license_expression import LicenseExpression

from spdx_tools.common.spdx_licensing import spdx_licensing
from spdx_tools.spdx3.bump_from_spdx2.license_expression import (
    bump_license_expression,
    bump_license_expression_or_none_or_no_assertion,
)
from spdx_tools.spdx3.model.licensing import (
    ConjunctiveLicenseSet,
    CustomLicense,
    CustomLicenseAddition,
    DisjunctiveLicenseSet,
    ListedLicense,
    ListedLicenseException,
    NoAssertionLicense,
    NoneLicense,
    WithAdditionOperator,
)
from spdx_tools.spdx.model import SpdxNoAssertion, SpdxNone
from tests.spdx.fixtures import extracted_licensing_info_fixture


@pytest.mark.parametrize(
    "element, expected_class",
    [
        (SpdxNoAssertion(), NoAssertionLicense),
        (SpdxNone(), NoneLicense),
        (spdx_licensing.parse("MIT"), ListedLicense),
    ],
)
def test_license_expression_or_none_or_no_assertion(element, expected_class):
    license_info = bump_license_expression_or_none_or_no_assertion(element, [])

    assert isinstance(license_info, expected_class)


@pytest.mark.parametrize(
    "license_expression, extracted_licensing_info, expected_element",
    [
        (spdx_licensing.parse("MIT"), [], ListedLicense("MIT", "MIT", "blank")),
        (spdx_licensing.parse("LGPL-2.0"), [], ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")),
        (
            spdx_licensing.parse("LicenseRef-1"),
            [extracted_licensing_info_fixture()],
            CustomLicense("LicenseRef-1", "licenseName", "extractedText"),
        ),
        (
            spdx_licensing.parse("MIT AND LGPL-2.0"),
            [],
            ConjunctiveLicenseSet(
                [ListedLicense("MIT", "MIT", "blank"), ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank")]
            ),
        ),
        (
            spdx_licensing.parse("LicenseRef-1 OR LGPL-2.0"),
            [extracted_licensing_info_fixture()],
            DisjunctiveLicenseSet(
                [
                    CustomLicense("LicenseRef-1", "licenseName", "extractedText"),
                    ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank"),
                ]
            ),
        ),
        (
            spdx_licensing.parse("LGPL-2.0 WITH 389-exception"),
            [],
            WithAdditionOperator(
                ListedLicense("LGPL-2.0-only", "LGPL-2.0-only", "blank"),
                ListedLicenseException("389-exception", "", ""),
            ),
        ),
        (
            spdx_licensing.parse("LicenseRef-1 WITH custom-exception"),
            [
                extracted_licensing_info_fixture(),
                extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"),
            ],
            WithAdditionOperator(
                CustomLicense("LicenseRef-1", "licenseName", "extractedText"),
                CustomLicenseAddition("custom-exception", "exceptionName", "This is a custom exception"),
            ),
        ),
        (
            spdx_licensing.parse("MIT AND LicenseRef-1 WITH custom-exception"),
            [
                extracted_licensing_info_fixture(),
                extracted_licensing_info_fixture("custom-exception", "This is a custom exception", "exceptionName"),
            ],
            ConjunctiveLicenseSet(
                [
                    ListedLicense("MIT", "MIT", "blank"),
                    WithAdditionOperator(
                        CustomLicense("LicenseRef-1", "licenseName", "extractedText"),
                        CustomLicenseAddition("custom-exception", "exceptionName", "This is a custom exception"),
                    ),
                ]
            ),
        ),
    ],
)
def test_license_expression_bump(license_expression: LicenseExpression, extracted_licensing_info, expected_element):
    license_info = bump_license_expression(license_expression, extracted_licensing_info)

    assert license_info == expected_element