diff --git a/changes/1876.bugfix.txt b/changes/1876.bugfix.txt new file mode 100644 index 000000000..ab1386abd --- /dev/null +++ b/changes/1876.bugfix.txt @@ -0,0 +1 @@ +Added escape template for Gradle which handles single quotes. diff --git a/src/briefcase/integrations/cookiecutter.py b/src/briefcase/integrations/cookiecutter.py index 8367b94c7..d691b2892 100644 --- a/src/briefcase/integrations/cookiecutter.py +++ b/src/briefcase/integrations/cookiecutter.py @@ -77,6 +77,28 @@ def escape_non_ascii(obj): environment.filters["escape_non_ascii"] = escape_non_ascii +class GradleEscape(Extension): + """Jinja2 extension to escape strings for Gradle as well.""" + + def __init__(self, environment): + """Initialize the extension with the given environment.""" + super().__init__(environment) + + def escape_gradle(obj): + """Escapes single quotes and backslashes.""" + return obj.replace("\\", "\\\\").replace("'", "\\'") + + def escape_non_ascii(obj): + """Quotes obj if non ascii characters are present.""" + if obj.isascii(): + return obj + else: + return '"' + obj + '"' + + environment.filters["escape_gradle"] = escape_gradle + environment.filters["escape_non_ascii"] = escape_non_ascii + + class PListExtension(Extension): """Jinja2 extension for generating plist values.""" diff --git a/tests/integrations/cookiecutter/test_GradleEscape.py b/tests/integrations/cookiecutter/test_GradleEscape.py new file mode 100644 index 000000000..105ea28c5 --- /dev/null +++ b/tests/integrations/cookiecutter/test_GradleEscape.py @@ -0,0 +1,34 @@ +from unittest.mock import MagicMock + +import pytest + +from briefcase.integrations.cookiecutter import GradleEscape + + +@pytest.mark.parametrize( + "value, expected", + [ + ("Hello World", "Hello World"), + ("Hello ' World", "Hello \\' World"), + ("Hello \\ World", "Hello \\\\ World"), + ], +) +def test_escape_gradle(value, expected): + env = MagicMock() + env.filters = {} + GradleEscape(env) + assert env.filters["escape_gradle"](value) == expected + + +@pytest.mark.parametrize( + "value, expected", + [ + ("helloworld", "helloworld"), + ("helloworldı", '"helloworldı"'), + ], +) +def test_escape_non_ascii(value, expected): + env = MagicMock() + env.filters = {} + GradleEscape(env) + assert env.filters["escape_non_ascii"](value) == expected