Skip to content

Commit

Permalink
Merge pull request #2068 from qilyn/1876-bugfix
Browse files Browse the repository at this point in the history
Create GradleEscape extension
  • Loading branch information
freakboy3742 authored Nov 26, 2024
2 parents b875bfc + e44e2b1 commit f468ca9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/1876.bugfix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added escape template for Gradle which handles single quotes.
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 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."""

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 f468ca9

Please sign in to comment.