Skip to content

Commit

Permalink
Validate that package name contains a namespace (#36)
Browse files Browse the repository at this point in the history
* Validate that package name contains a namespace

* fix tests

* changelog
  • Loading branch information
davisagli authored Aug 26, 2024
1 parent 178ec0d commit efac259
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
16 changes: 9 additions & 7 deletions cookieplone/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ def validate_language_code(value: str) -> str:

def validate_python_package_name(value: str) -> str:
"""Validate python_package_name is an identifier."""
status = False
if "." in value:
namespace, package = value.split(".")
status = namespace.isidentifier() and package.isidentifier()
else:
status = value.isidentifier()
return "" if status else f"'{value}' is not a valid Python identifier."
parts = value.split(".")
if any(not part.isidentifier() for part in parts):
return f"'{value}' is not a valid Python identifier."
if len(parts) != 2:
return (
"The Python package name must contain a single namespace "
"(e.g. collective.something)"
)
return ""


def validate_hostname(value: str) -> str:
Expand Down
1 change: 1 addition & 0 deletions news/36.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show a more helpful error message if user enters a Python package name without a namespace. @davisagli
18 changes: 15 additions & 3 deletions tests/utils/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,22 @@ def test_validate_language_code(value: str, expected: str):
(" ", "' ' is not a valid Python identifier."),
("", "'' is not a valid Python identifier."),
("project title", "'project title' is not a valid Python identifier."),
("project_title", ""),
(
"project_title",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
("project-title", "'project-title' is not a valid Python identifier."),
("projecttitle", ""),
("projectTitle", ""),
(
"projecttitle",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
(
"projectTitle",
"The Python package name must contain a single namespace "
"(e.g. collective.something)",
),
("project.title", ""),
),
)
Expand Down

0 comments on commit efac259

Please sign in to comment.