| 1 |
from textwrap import dedent |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from briefcase.exceptions import BriefcaseCommandError |
| 6 |
|
| 7 |
from .....utils import create_file |
| 8 |
|
| 9 |
|
| 10 |
def test_license_file_txt(create_command, first_app_templated, tmp_path): |
| 11 |
"""A license can be specified as a TXT file.""" |
| 12 |
create_file(tmp_path / "base_path/LICENSE", "This is a license file") |
| 13 |
|
| 14 |
create_command.install_license(first_app_templated) |
| 15 |
|
| 16 |
# A LICENSE.rtf file has been written, in RTF format, containing the license text. |
| 17 |
license = tmp_path / "base_path/build/first-app/windows/app/LICENSE.rtf" |
| 18 |
assert license.is_file() |
| 19 |
|
| 20 |
license_text = license.read_text(encoding="utf-8") |
| 21 |
assert license_text.startswith("{\\rtf1\\ansi") |
| 22 |
assert "This is a license file" in license_text |
| 23 |
assert license_text.endswith("\n}") |
| 24 |
|
| 25 |
|
| 26 |
def test_license_file_rtf(create_command, first_app_templated, tmp_path): |
| 27 |
"""A license can be specified as an RTF file.""" |
| 28 |
first_app_templated.license["file"] = "LICENSE.rtf" |
| 29 |
orig_license = tmp_path / "base_path/LICENSE.rtf" |
| 30 |
create_file( |
| 31 |
orig_license, |
| 32 |
dedent( |
| 33 |
"""\ |
| 34 |
{\\rtf1\\ansi\\deff0 {\\fonttbl {\\f0 Courier;}} |
| 35 |
This is a document that contains bullet points:\x20 |
| 36 |
\\par\\line |
| 37 |
\\bullet first, something short\x20 |
| 38 |
\\bullet then, something longer that needs to\x20 |
| 39 |
run onto multiple lines\x20 |
| 40 |
\\bullet last, something short again\x20 |
| 41 |
\\par\\line |
| 42 |
Then a closing paragraph.\x20 |
| 43 |
\\par\\line |
| 44 |
}""" |
| 45 |
), |
| 46 |
) |
| 47 |
create_command.install_license(first_app_templated) |
| 48 |
|
| 49 |
# The license RTF file is the same as the original |
| 50 |
license = tmp_path / "base_path/build/first-app/windows/app/LICENSE.rtf" |
| 51 |
assert license.is_file() |
| 52 |
assert license.read_text(encoding="utf-8") == orig_license.read_text( |
| 53 |
encoding="utf-8" |
| 54 |
) |
| 55 |
|
| 56 |
|
| 57 |
def test_missing_license_file(create_command, first_app_templated): |
| 58 |
"""If a named license file is missing, an error is raised.""" |
| 59 |
with pytest.raises( |
| 60 |
BriefcaseCommandError, |
| 61 |
match=r"However, this file does not exist.", |
| 62 |
): |
| 63 |
create_command.install_license(first_app_templated) |
| 64 |
|
| 65 |
|
| 66 |
def test_license_text(create_command, first_app_templated, tmp_path): |
| 67 |
"""A license provided as text is converted into a file.""" |
| 68 |
del first_app_templated.license["file"] |
| 69 |
first_app_templated.license["text"] = "This is a license\nDo what you want." |
| 70 |
|
| 71 |
create_command.install_license(first_app_templated) |
| 72 |
|
| 73 |
# A license file was written, and contains the licence text |
| 74 |
license = tmp_path / "base_path/build/first-app/windows/app/LICENSE.rtf" |
| 75 |
license_text = license.read_text(encoding="utf-8") |
| 76 |
assert license_text.startswith("{\\rtf1\\ansi") |
| 77 |
assert "This is a license " in license_text |
| 78 |
assert license_text.endswith("\n}") |
| 79 |
|
| 80 |
|
| 81 |
def test_license_text_suspicious(create_command, first_app_templated, tmp_path, capsys): |
| 82 |
"""A single line license text is flagged as suspicious, but converted.""" |
| 83 |
del first_app_templated.license["file"] |
| 84 |
first_app_templated.license["text"] = "BSD 3 clause" |
| 85 |
|
| 86 |
create_command.install_license(first_app_templated) |
| 87 |
|
| 88 |
license = tmp_path / "base_path/build/first-app/windows/app/LICENSE.rtf" |
| 89 |
assert license.is_file() |
| 90 |
|
| 91 |
license_text = license.read_text(encoding="utf-8") |
| 92 |
assert license_text.startswith("{\\rtf1\\ansi") |
| 93 |
assert "BSD 3 clause" in license_text |
| 94 |
assert license_text.endswith("\n}") |
| 95 |
|
| 96 |
# The user was warned that the text probably isn't a full license |
| 97 |
console = capsys.readouterr().out |
| 98 |
assert "ensure that the contents of this file is adequate." in console |
| 99 |
|
| 100 |
|
| 101 |
def test_no_license(create_command, first_app_templated): |
| 102 |
"""If an app doesn't provide a known license definition, an error is raised.""" |
| 103 |
del first_app_templated.license["file"] |
| 104 |
with pytest.raises( |
| 105 |
BriefcaseCommandError, |
| 106 |
match=r"Your project does not contain a `license` definition.", |
| 107 |
): |
| 108 |
create_command.install_license(first_app_templated) |