Skip to content

Commit

Permalink
Create GradleEscape extension for beeware#1876
Browse files Browse the repository at this point in the history
  • Loading branch information
qilyn committed Nov 26, 2024
1 parent b875bfc commit e6c218b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/briefcase/integrations/cookiecutter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
return obj.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."""

Expand Down
34 changes: 34 additions & 0 deletions tests/integrations/cookiecutter/test_GradleEscape.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e6c218b

Please sign in to comment.