|
1 | 1 | import argparse |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +import re |
4 | 5 | import shutil |
5 | 6 | import subprocess |
6 | 7 | import sys |
@@ -309,16 +310,103 @@ def test_cli_init_suggestion_is_stable_for_legal_names() -> None: |
309 | 310 | assert pn_cli._sanitize_name(name) == name |
310 | 311 |
|
311 | 312 |
|
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 |
316 | 388 | project_dir.mkdir() |
317 | 389 |
|
318 | 390 | result = run_pn(["init"], str(project_dir)) |
319 | 391 |
|
320 | 392 | 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 | + |
322 | 410 | assert 'name = "MyProject"' in toml_text |
323 | 411 | assert 'id = "com.example.myproject"' in toml_text |
324 | 412 |
|
|
0 commit comments