File: test_input_license.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (237 lines) | stat: -rw-r--r-- 8,091 bytes parent folder | download | duplicates (2)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from unittest.mock import MagicMock

import pytest

from ...utils import PartialMatchString


@pytest.mark.parametrize("license_file_name", ["LICENSE", "LICENCE"])
@pytest.mark.parametrize(
    "license_text, license",
    [
        ("MIT", "MIT license"),
        ("MIT license", "MIT license"),
        ("Permission is hereby granted, free of charge", "MIT license"),
        ("Apache license", "Apache Software License"),
        ("BSD", "BSD license"),
        ("BSD license", "BSD license"),
        # Includes some extra text to ensure it doesn't get caught as MIT because of
        # perMITted
        (
            "Redistribution and use in source and binary forms, with or without modification, are permitted",
            "BSD license",
        ),
        ("GPLv2", "GNU General Public License v2 (GPLv2)"),
        (
            "version 2 of the GNU General Public License",
            "GNU General Public License v2 (GPLv2)",
        ),
        ("GPLv2+", "GNU General Public License v2 or later (GPLv2+)"),
        (
            "Free Software Foundation, either version 2 of the License",
            "GNU General Public License v2 or later (GPLv2+)",
        ),
        ("GPLv3", "GNU General Public License v3 (GPLv3)"),
        (
            "version 3 of the GNU General Public License",
            "GNU General Public License v3 (GPLv3)",
        ),
        ("GPLv3+", "GNU General Public License v3 or later (GPLv3+)"),
        (
            "either version 3 of the License",
            "GNU General Public License v3 or later (GPLv3+)",
        ),
        ("Some text", "Other"),
    ],
)
def test_get_license_from_file(
    convert_command, license_text, license, license_file_name, monkeypatch
):
    mock_selection_question = MagicMock()
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )

    dummy_license_text = (
        "some leading text\neven_more_text" + license_text + "some_ending_text\n"
    )
    (convert_command.base_path / license_file_name).write_text(
        dummy_license_text, encoding="utf-8"
    )

    convert_command.input_license(None)
    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString("the license file"),
        description="Project License",
        options=[
            "BSD license",
            "MIT license",
            "Apache Software License",
            "GNU General Public License v2 (GPLv2)",
            "GNU General Public License v2 or later (GPLv2+)",
            "GNU General Public License v3 (GPLv3)",
            "GNU General Public License v3 or later (GPLv3+)",
            "Proprietary",
            "Other",
        ],
        default=license,
        override_value=None,
    )


@pytest.mark.parametrize(
    "license_text, license",
    [
        ("MIT", "MIT license"),
        ("MIT license", "MIT license"),
        ("Permission is hereby granted, free of charge", "MIT license"),
        ("Apache license", "Apache Software License"),
        ("BSD", "BSD license"),
        ("BSD license", "BSD license"),
        # Includes some extra text to ensure it doesn't get caught as MIT because of
        # perMITted
        (
            "Redistribution and use in source and binary forms, with or without modification, are permitted",
            "BSD license",
        ),
        ("GPLv2", "GNU General Public License v2 (GPLv2)"),
        (
            "version 2 of the GNU General Public License",
            "GNU General Public License v2 (GPLv2)",
        ),
        ("GPLv2+", "GNU General Public License v2 or later (GPLv2+)"),
        (
            "Free Software Foundation, either version 2 of the License",
            "GNU General Public License v2 or later (GPLv2+)",
        ),
        ("GPLv3", "GNU General Public License v3 (GPLv3)"),
        (
            "version 3 of the GNU General Public License",
            "GNU General Public License v3 (GPLv3)",
        ),
        ("GPLv3+", "GNU General Public License v3 or later (GPLv3+)"),
        (
            "either version 3 of the License",
            "GNU General Public License v3 or later (GPLv3+)",
        ),
        ("Some text", "Other"),
    ],
)
def test_get_license_from_pep621_license_file(
    convert_command,
    license_text,
    license,
    monkeypatch,
):
    mock_selection_question = MagicMock()
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )

    dummy_license_text = (
        "some leading text\neven_more_text" + license_text + "some_ending_text\n"
    )
    (convert_command.base_path / "LICENSE.txt").write_text(
        dummy_license_text, encoding="utf-8"
    )
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n" 'license = {file = "LICENSE.txt"}', encoding="utf-8"
    )

    convert_command.input_license(None)

    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString("the license file"),
        description="Project License",
        options=[
            "BSD license",
            "MIT license",
            "Apache Software License",
            "GNU General Public License v2 (GPLv2)",
            "GNU General Public License v2 or later (GPLv2+)",
            "GNU General Public License v3 (GPLv3)",
            "GNU General Public License v3 or later (GPLv3+)",
            "Proprietary",
            "Other",
        ],
        default=license,
        override_value=None,
    )


@pytest.mark.parametrize(
    "license_text, license",
    [
        ("MIT", "MIT license"),
        ("MIT license", "MIT license"),
        ("BSD", "BSD license"),
        ("BSD license", "BSD license"),
        ("GPLv2", "GNU General Public License v2 (GPLv2)"),
        ("GPLv2+", "GNU General Public License v2 or later (GPLv2+)"),
        ("GPLv3", "GNU General Public License v3 (GPLv3)"),
        ("GPLv3+", "GNU General Public License v3 or later (GPLv3+)"),
        ("Some text", "Other"),
    ],
)
def test_get_license_from_pyproject(
    convert_command,
    license_text,
    license,
    monkeypatch,
):
    mock_selection_question = MagicMock()
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )
    (convert_command.base_path / "pyproject.toml").write_text(
        "[project]\n" f'license = {{text = "{license_text}"}}', encoding="utf-8"
    )

    convert_command.input_license(None)
    mock_selection_question.assert_called_once_with(
        intro=PartialMatchString("the PEP621 formatted pyproject.toml"),
        description="Project License",
        options=[
            "BSD license",
            "MIT license",
            "Apache Software License",
            "GNU General Public License v2 (GPLv2)",
            "GNU General Public License v2 or later (GPLv2+)",
            "GNU General Public License v3 (GPLv3)",
            "GNU General Public License v3 or later (GPLv3+)",
            "Proprietary",
            "Other",
        ],
        default=license,
        override_value=None,
    )


def test_no_license_hint(convert_command, monkeypatch):
    mock_selection_question = MagicMock()
    monkeypatch.setattr(
        convert_command.console, "selection_question", mock_selection_question
    )

    convert_command.input_license(None)
    mock_selection_question.assert_called_once_with(
        intro="What license do you want to use for this project's code? ",
        description="Project License",
        options=[
            "BSD license",
            "MIT license",
            "Apache Software License",
            "GNU General Public License v2 (GPLv2)",
            "GNU General Public License v2 or later (GPLv2+)",
            "GNU General Public License v3 (GPLv3)",
            "GNU General Public License v3 or later (GPLv3+)",
            "Proprietary",
            "Other",
        ],
        default=None,
        override_value=None,
    )


def test_override_is_used(convert_command):
    assert convert_command.input_license("Proprietary") == "Proprietary"