FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(project): escape project name in rendered pythonnative.toml by ernesthenry · Pull Request #35 · pythonnative/pythonnative · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
22 changes: 14 additions & 8 deletions src/pythonnative/project/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

from __future__ import annotations

import json
import re
from dataclasses import dataclass, field
from pathlib import Path
Expand Down Expand Up @@ -599,20 +600,25 @@ def render_default_toml(*, name: str, app_id: str, python_version: str = "3.11")
for the optional tables.
"""
display = name.replace("_", " ").replace("-", " ").strip().title() or name
escaped_app_id = json.dumps(app_id)
escaped_name = json.dumps(name)
escaped_display = json.dumps(display)
escaped_python_version = json.dumps(python_version)
comment_name = name.replace("\n", " ").replace("\r", " ")
return f"""# PythonNative project configuration.
# Docs: https://pythonnative.com/guides/configuration/

[app]
id = "{app_id}"
name = "{name}"
display_name = "{display}"
id = {escaped_app_id}
name = {escaped_name}
display_name = {escaped_display}
version = "1.0.0"
build = 1
python_version = "{python_version}"
python_version = {escaped_python_version}
orientation = "portrait" # portrait | landscape | all
entry_point = "app/main.py"
# Custom URL schemes for deep links (e.g. "{name}" opens {name}://...).
# url_schemes = ["{name}"]
# Custom URL schemes for deep links (e.g. {escaped_name} opens {comment_name}://...).
# url_schemes = [{escaped_name}]

# Declare the device capabilities your app needs. A string becomes the
# iOS permission prompt text; `true` uses a sensible default.
Expand All @@ -637,7 +643,7 @@ def render_default_toml(*, name: str, app_id: str, python_version: str = "3.11")
[ios]
deployment_target = "13.0"
# development_team = "ABCDE12345"
# bundle_id = "{app_id}"
# bundle_id = {escaped_app_id}

[ios.signing]
export_method = "development" # development | ad-hoc | app-store | enterprise
Expand All @@ -652,5 +658,5 @@ def render_default_toml(*, name: str, app_id: str, python_version: str = "3.11")

[android.signing]
# keystore = "release.keystore"
# key_alias = "{name}"
# key_alias = {escaped_name}
"""
9 changes: 9 additions & 0 deletions tests/project/test_config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,12 @@ def test_rendered_default_toml_parses_and_loads() -> None:
assert cfg.app_id == "com.example.my_app"
assert cfg.display_name == "My App"
assert cfg.requirements == []


def test_rendered_default_toml_escapes_special_characters() -> None:
text = render_default_toml(name='bad"name\nwith\\backslash', app_id="com.example.myapp")
data = tomllib.loads(text)
cfg = AppConfig.from_dict(data)
assert cfg.name == 'bad"name\nwith\\backslash'
assert cfg.app_id == "com.example.myapp"
assert cfg.display_name == 'Bad"Name\nWith\\Backslash'
15 changes: 15 additions & 0 deletions tests/test_cli.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ def test_cli_init_creates_named_directory(tmp_path: Path) -> None:
assert os.listdir(str(tmp_path)) == ["my_app"]


def test_cli_init_escapes_special_characters_in_toml(tmp_path: Path) -> None:
result = run_pn(["init", 'bad"name'], str(tmp_path))
assert result.returncode == 0, result.stderr

project_dir = tmp_path / 'bad"name'
config_path = project_dir / "pythonnative.toml"
assert config_path.is_file()

from pythonnative.project.config import AppConfig

cfg = AppConfig.load(project_dir)
assert cfg.name == 'bad"name'
assert cfg.app_id == "com.example.badname"


def test_cli_init_without_name_uses_cwd(tmp_path: Path) -> None:
project_dir = tmp_path / "widgets"
project_dir.mkdir()
Expand Down
Loading

Back | FazBrowse Home | New Git URL