Skip to content

Add unit tests and a docstring for _app_id_from_name in the pn CLI #62

Closed

Description

@owenthcarey

Problem

_app_id_from_name in src/pythonnative/cli/pn.py (lines 130-134) derives the default Android application id / iOS bundle id that pn init writes into pythonnative.toml:

def _app_id_from_name(name: str) -> str:
    slug = re.sub(r"[^a-z0-9_]", "", name.lower())
    if not slug or not slug[0].isalpha():
        slug = "app" + slug
    return f"com.example.{slug}"

Two gaps:

  1. No direct tests. tests/test_cli.py only exercises the happy path indirectly, by scaffolding my_app and asserting id = "com.example.my_app" appears in the generated TOML (lines 54 and 93), plus one directory-named case (widgets, line 109). The interesting behavior is untested: hyphens are stripped (my-app becomes com.example.myapp, because Java package segments can't contain -), and a digit-leading name gets an app prefix (3d_viewer becomes com.example.app3d_viewer). Its neighbor _sanitize_name (lines 107-127) has dedicated unit tests at lines 270-282.
  2. No docstring. _sanitize_name directly above has a full Google-style docstring explaining its rules; _app_id_from_name has none, so the hyphen-stripping and prefix behavior is only discoverable by reading the regex.

Ruff doesn't flag this because the function is private, but the file otherwise documents its helpers, and the behavior is user-visible in every scaffolded project.

Suggested fix

  • Add unit tests in tests/test_cli.py next to the _sanitize_name tests that call pn_cli._app_id_from_name directly and cover: a plain name, a name with hyphens, a name with uppercase letters, a digit-leading name, and an empty string. Assert each result matches a reverse-DNS pattern such as ^com\.example\.[a-z][a-z0-9_]*$.
  • Add a Google-style docstring to _app_id_from_name modeled on _sanitize_name's, with Args: and Returns: sections and one sentence on why hyphens are dropped.
  • Don't change the function's behavior; existing scaffolded projects depend on it.

Verification

  • uv run pytest tests/test_cli.py -q passes.
  • ./scripts/check.sh passes.

Suggested commit message

test(cli): cover app id derivation from project names

Activity

  1. added
    enhancement
    good first issue
    on Sep 2, 2026
  2. Adebowale-Morakinyo commented on Sep 2, 2026

    @Adebowale-Morakinyo
    Contributor

    @owenthcarey third is the charm😁. PS, this is also related to the hyphen behavior I flagged. Thanks!

  3. TheoGoulart333 commented on Sep 2, 2026

    @TheoGoulart333
    Contributor

    Hi! I'd like to work on this issue. Could you please assign it to me?

  4. Adebowale-Morakinyo commented on Sep 4, 2026

    @Adebowale-Morakinyo
    Contributor

    Some findings from digging in, two of which change the shape of the work.

    The headline example can't be typed. The issue names 3d_viewer → com.example.app3d_viewer as the interesting untested behavior, but _NAME_RE rejects 3d_viewer, so pn init 3d_viewer exits 1 on the name guard and never reaches this function. The app-prefix branch is reachable only via pn init with no name, where the project name comes from the directory and is unvalidated by design. Same for the empty-string and uppercase cases. From a typed name, hyphen removal is the only live transform. The tests will cover both paths and say which reaches what.

    The suggested assertion can't fail in the way I originally described. I fuzzed ^com\.example\.[a-z][a-z0-9_]*$ over ~2.2M inputs — exhaustive ASCII to length 3, every Unicode character whose .lower() changes length, plus random control characters and non-ASCII — with zero failures. It holds structurally for the current derivation: re.sub leaves the slug inside [a-z0-9_], so either the first character is [a-z] or the app prefix makes it a. That said, the assertion can still catch a change that admits a character outside the segment grammar, so it is worth keeping alongside exact-value assertions rather than relying on it alone. It does not, however, pin the prefix.

    Current coverage pins neither behavior this issue exists to cover. Mutating the function to keep hyphens breaks zero tests; changing the prefix from app to x breaks zero tests. Every existing fixture uses my_app, widgets, or MyProject — no hyphens, nothing digit- or underscore-leading. What the six downstream assertions actually pin is underscore preservation and lowercasing, incidentally. The new tests will be mutation-checked against exactly those two mutations.

    Separately, and I'd like to open an issue for it: pn init class exits 0 and writes id = "com.example.class", which _validate_app_id then rejects — all 53 entries in _JAVA_KEYWORDS pass _NAME_RE. The scaffold is dead on arrival: app-id, deps, and run all exit 1 afterwards with an error naming a config field the user never wrote. doctor does flag it. The derived path reaches it from more directions too, since case and punctuation are stripped — Class, CLASS, cla-ss all derive the same id. Out of scope here, and the fix is a product call (reject at pn init, suffix the slug, or fall back). Want me to file it?

    One more for the record, not something I'd change here: hyphen deletion makes the mapping non-injective, so my-app and myapp both give com.example.myapp and would install over each other on a device. Mapping - to _ would not solve that: _NAME_RE permits both characters, so my-app and my_app would both derive com.example.my_app. Making the derivation injective would need an escaping scheme, not a simple substitution. So hyphen deletion isn't uniquely problematic; it is simply lossy.

    @owenthcarey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementgood first issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions