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
|
from unittest.mock import MagicMock
import pytest
from briefcase.commands.new import LICENSE_OPTIONS
from ...utils import PartialMatchString
@pytest.mark.parametrize("license_file_name", ["LICENSE", "LICENCE"])
@pytest.mark.parametrize(
"license_text, license_id",
[
("MIT", "MIT"),
("MIT license", "MIT"),
("Permission is hereby granted, free of charge", "MIT"),
("Apache license", "Apache-2.0"),
("BSD", "BSD-3-Clause"),
("BSD license", "BSD-3-Clause"),
# 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-3-Clause",
),
("GPLv2", "GPL-2.0"),
("version 2 of the GNU General Public License", "GPL-2.0"),
("GPLv2+", "GPL-2.0+"),
("Free Software Foundation, either version 2 of the License", "GPL-2.0+"),
("GPLv3", "GPL-3.0"),
("version 3 of the GNU General Public License", "GPL-3.0"),
("GPLv3+", "GPL-3.0+"),
("either version 3 of the License", "GPL-3.0+"),
("Some text", "Other"),
],
)
def test_get_license_from_file(
convert_command,
license_text,
license_id,
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("Based on the license file"),
description="Project License",
options=LICENSE_OPTIONS,
default=license_id,
override_value=None,
)
@pytest.mark.parametrize(
"license_text, license_id",
[
("MIT", "MIT"),
("MIT license", "MIT"),
("Permission is hereby granted, free of charge", "MIT"),
("Apache license", "Apache-2.0"),
("BSD", "BSD-3-Clause"),
("BSD license", "BSD-3-Clause"),
# 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-3-Clause",
),
("GPLv2", "GPL-2.0"),
("version 2 of the GNU General Public License", "GPL-2.0"),
("GPLv2+", "GPL-2.0+"),
("Free Software Foundation, either version 2 of the License", "GPL-2.0+"),
("GPLv3", "GPL-3.0"),
("version 3 of the GNU General Public License", "GPL-3.0"),
("GPLv3+", "GPL-3.0+"),
("either version 3 of the License", "GPL-3.0+"),
("Some text", "Other"),
],
)
def test_get_license_from_pep621_license_file(
convert_command,
license_text,
license_id,
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]\nlicense = {file = "LICENSE.txt"}', encoding="utf-8"
)
convert_command.input_license(None)
mock_selection_question.assert_called_once_with(
intro=PartialMatchString("Based on the license file"),
description="Project License",
options=LICENSE_OPTIONS,
default=license_id,
override_value=None,
)
@pytest.mark.parametrize(
"license_text, license_id",
[
("MIT", "MIT"),
("MIT license", "MIT"),
("BSD", "BSD-3-Clause"),
("BSD license", "BSD-3-Clause"),
("GPLv2", "GPL-2.0"),
("GPLv2+", "GPL-2.0+"),
("GPLv3", "GPL-3.0"),
("GPLv3+", "GPL-3.0+"),
("Some text", "Other"),
],
)
def test_get_license_from_pyproject(
convert_command,
license_text,
license_id,
monkeypatch,
):
mock_selection_question = MagicMock()
monkeypatch.setattr(
convert_command.console, "selection_question", mock_selection_question
)
(convert_command.base_path / "pyproject.toml").write_text(
f'[project]\nlicense = {{text = "{license_text}"}}', encoding="utf-8"
)
convert_command.input_license(None)
mock_selection_question.assert_called_once_with(
intro=PartialMatchString("Based on the PEP621 formatted pyproject.toml"),
description="Project License",
options=LICENSE_OPTIONS,
default=license_id,
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)
assert mock_selection_question.call_count == 1
assert mock_selection_question.call_args.kwargs["default"] is None
assert "Based on" not in mock_selection_question.call_args.kwargs["intro"]
def test_override_is_used(convert_command):
assert convert_command.input_license("Proprietary") == "Proprietary"
|