Skip to content

Commit 5b1d965

Browse files
test(cli): cover app id derivation from project names (#79)
Refs: #62
1 parent e4af1c8 commit 5b1d965

2 files changed

Lines changed: 124 additions & 5 deletions

File tree

‎src/pythonnative/cli/pn.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,37 @@ def _sanitize_name(name: str) -> str:
147147

148148

149149
def _app_id_from_name(name: str) -> str:
150+
"""Derive the default reverse-DNS app id from a project name.
151+
152+
The result is an identifier rather than a name, and it fills
153+
``app.id``, the default for both the Android application id and the
154+
iOS bundle id. It therefore has to satisfy the stricter of the two
155+
grammars, ``config._APP_ID_SEGMENT``, which admits no hyphen. An
156+
explicit ``[ios].bundle_id`` may contain one, because
157+
``config._validate_app_id`` is called with ``allow_hyphen=True`` for
158+
that field alone.
159+
160+
Deleting hyphens is lossy, so the derivation is not injective:
161+
``my-app`` and ``myapp`` both give ``com.example.myapp``. Mapping
162+
``-`` to ``_`` instead would not fix that, only move it, since
163+
``_NAME_RE`` permits both characters and ``my-app`` would then
164+
collide with ``my_app``. Injectivity would need an escaping scheme;
165+
deletion is the deliberate choice here.
166+
167+
The ``app`` prefix covers the name taken from the current directory
168+
when ``pn init`` is given none. A typed name cannot reach it, because
169+
``_NAME_RE`` already guarantees a leading letter.
170+
171+
The result always satisfies ``_APP_ID_SEGMENT`` but is not guaranteed
172+
to pass ``config._validate_app_id``, which also rejects reserved
173+
words: ``class`` is a legal project name and passes through here.
174+
175+
Args:
176+
name: A project name, either typed or taken from the directory.
177+
178+
Returns:
179+
A reverse-DNS id under ``com.example``.
180+
"""
150181
slug = re.sub(r"[^a-z0-9_]", "", name.lower())
151182
if not slug or not slug[0].isalpha():
152183
slug = "app" + slug

‎tests/test_cli.py‎

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import json
33
import os
4+
import re
45
import shutil
56
import subprocess
67
import sys
@@ -309,16 +310,103 @@ def test_cli_init_suggestion_is_stable_for_legal_names() -> None:
309310
assert pn_cli._sanitize_name(name) == name
310311

311312

312-
def test_cli_init_without_name_accepts_a_cwd_that_fails_the_pattern(tmp_path: Path) -> None:
313-
# Validation covers the typed name only. A directory named MyProject is
314-
# extremely ordinary, and `pn init` there must keep working.
315-
project_dir = tmp_path / "MyProject"
313+
# _app_id_from_name maps a project name onto a reverse-DNS segment, whose
314+
# grammar (config._APP_ID_SEGMENT) differs from _NAME_RE's. These two groups
315+
# split by which entry path can actually produce the input.
316+
317+
# Reachable from a typed `pn init <name>`: _NAME_RE admits only [a-z][a-z0-9_-]*,
318+
# so the sole character the substitution can remove is the hyphen.
319+
_APP_ID_TYPED_CASES = [
320+
pytest.param("my_app", "com.example.my_app", id="underscore"),
321+
pytest.param("myapp", "com.example.myapp", id="plain"),
322+
pytest.param("my-app", "com.example.myapp", id="hyphen"),
323+
pytest.param("my--app", "com.example.myapp", id="doubled-hyphen"),
324+
pytest.param("my-app-", "com.example.myapp", id="trailing-hyphen"),
325+
pytest.param("e2e-suite", "com.example.e2esuite", id="digits-and-hyphen"),
326+
]
327+
328+
# Reachable only from the derived path, `pn init` with no name taking cwd.name,
329+
# which is never validated. A typed name cannot produce any of these: _NAME_RE
330+
# rejects uppercase, a leading digit, a leading underscore, the empty string,
331+
# and non-ASCII.
332+
_APP_ID_DERIVED_ONLY_CASES = [
333+
pytest.param("MyApp", "com.example.myapp", id="uppercase"),
334+
pytest.param("MyProject", "com.example.myproject", id="mixed-case"),
335+
pytest.param("3d_viewer", "com.example.app3d_viewer", id="digit-leading"),
336+
pytest.param("2048", "com.example.app2048", id="all-digits"),
337+
pytest.param("_leading", "com.example.app_leading", id="underscore-leading"),
338+
pytest.param("", "com.example.app", id="empty"),
339+
pytest.param("\u5de5\u7a0b", "com.example.app", id="all-non-ascii"),
340+
pytest.param("caf\u00e9", "com.example.caf", id="partly-stripped-non-ascii"),
341+
pytest.param("app.v2", "com.example.appv2", id="dotted"),
342+
]
343+
344+
_APP_ID_ALL_CASES = _APP_ID_TYPED_CASES + _APP_ID_DERIVED_ONLY_CASES
345+
346+
# The pattern the issue suggests asserting. It cannot fail against the current
347+
# implementation for any input, because the substitution leaves only [a-z0-9_]
348+
# and the "app" prefix supplies a leading letter otherwise. It still earns its
349+
# place: it catches a change that admits a character outside the segment
350+
# grammar, such as keeping hyphens. It does not catch a changed prefix, which
351+
# is what the exact-value cases above are for.
352+
_REVERSE_DNS_RE = re.compile(r"^com\.example\.[a-z][a-z0-9_]*$")
353+
354+
355+
@pytest.mark.parametrize(("name", "expected"), _APP_ID_TYPED_CASES)
356+
def test_cli_app_id_from_name_typed_names(name: str, expected: str) -> None:
357+
assert pn_cli._NAME_RE.fullmatch(name), f"{name!r} should be typeable"
358+
assert pn_cli._app_id_from_name(name) == expected
359+
360+
361+
@pytest.mark.parametrize(("name", "expected"), _APP_ID_DERIVED_ONLY_CASES)
362+
def test_cli_app_id_from_name_derived_only_names(name: str, expected: str) -> None:
363+
assert not pn_cli._NAME_RE.fullmatch(name), f"{name!r} should not be typeable"
364+
assert pn_cli._app_id_from_name(name) == expected
365+
366+
367+
@pytest.mark.parametrize(("name", "expected"), _APP_ID_ALL_CASES)
368+
def test_cli_app_id_from_name_is_reverse_dns(name: str, expected: str) -> None:
369+
assert _REVERSE_DNS_RE.fullmatch(pn_cli._app_id_from_name(name))
370+
371+
372+
def test_cli_app_id_from_name_collides_on_hyphens() -> None:
373+
# Deleting hyphens is lossy, so distinct names share an id. Recorded so
374+
# the collision is known rather than discovered when two projects install
375+
# over each other.
376+
assert pn_cli._app_id_from_name("my-app") == pn_cli._app_id_from_name("myapp")
377+
assert pn_cli._app_id_from_name("a-b-c") == pn_cli._app_id_from_name("abc")
378+
# Mapping "-" to "_" would not fix this, only move it: _NAME_RE permits
379+
# both characters, so this pair, distinct today, would merge under that
380+
# scheme. Across every typed-legal name up to length 4 the two schemes
381+
# lose the same number of names to collision.
382+
assert pn_cli._app_id_from_name("my-app") != pn_cli._app_id_from_name("my_app")
383+
384+
385+
def _init_in_directory_named(tmp_path: Path, dirname: str) -> str:
386+
"""`pn init` with no name inside a directory called ``dirname``."""
387+
project_dir = tmp_path / dirname
316388
project_dir.mkdir()
317389

318390
result = run_pn(["init"], str(project_dir))
319391

320392
assert result.returncode == 0, result.stdout
321-
toml_text = (project_dir / "pythonnative.toml").read_text(encoding="utf-8")
393+
return (project_dir / "pythonnative.toml").read_text(encoding="utf-8")
394+
395+
396+
def test_cli_init_without_name_reaches_the_app_prefix(tmp_path: Path) -> None:
397+
# The prefix branch is unreachable from a typed name, so this is the only
398+
# end-to-end path to it: a directory whose name starts with a digit.
399+
toml_text = _init_in_directory_named(tmp_path, "3d_viewer")
400+
401+
assert 'id = "com.example.app3d_viewer"' in toml_text
402+
assert 'name = "3d_viewer"' in toml_text
403+
404+
405+
def test_cli_init_without_name_accepts_a_cwd_that_fails_the_pattern(tmp_path: Path) -> None:
406+
# Validation covers the typed name only. A directory named MyProject is
407+
# extremely ordinary, and `pn init` there must keep working.
408+
toml_text = _init_in_directory_named(tmp_path, "MyProject")
409+
322410
assert 'name = "MyProject"' in toml_text
323411
assert 'id = "com.example.myproject"' in toml_text
324412

0 commit comments

Comments
 (0)