add: reflex init app name validator (#2336)
This commit is contained in:
parent
5c80e7a969
commit
c5c42665eb
@ -74,8 +74,8 @@ def _init(
|
|||||||
# Show system info
|
# Show system info
|
||||||
exec.output_system_info()
|
exec.output_system_info()
|
||||||
|
|
||||||
# Get the app name.
|
# Validate the app name.
|
||||||
app_name = prerequisites.get_default_app_name() if name is None else name
|
app_name = prerequisites.validate_app_name(name)
|
||||||
console.rule(f"[bold]Initializing {app_name}")
|
console.rule(f"[bold]Initializing {app_name}")
|
||||||
|
|
||||||
prerequisites.check_latest_package_version(constants.Reflex.MODULE_NAME)
|
prerequisites.check_latest_package_version(constants.Reflex.MODULE_NAME)
|
||||||
|
@ -215,19 +215,23 @@ def get_production_backend_url() -> str:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_default_app_name() -> str:
|
def validate_app_name(app_name: str | None = None) -> str:
|
||||||
"""Get the default app name.
|
"""Validate the app name.
|
||||||
|
|
||||||
The default app name is the name of the current directory.
|
The default app name is the name of the current directory.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
app_name: the name passed by user during reflex init
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The default app name.
|
The app name after validation.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
Exit: if the app directory name is reflex.
|
Exit: if the app directory name is reflex or if the name is not standard for a python package name.
|
||||||
"""
|
"""
|
||||||
app_name = os.getcwd().split(os.path.sep)[-1].replace("-", "_")
|
app_name = (
|
||||||
|
app_name if app_name else os.getcwd().split(os.path.sep)[-1].replace("-", "_")
|
||||||
|
)
|
||||||
# Make sure the app is not named "reflex".
|
# Make sure the app is not named "reflex".
|
||||||
if app_name == constants.Reflex.MODULE_NAME:
|
if app_name == constants.Reflex.MODULE_NAME:
|
||||||
console.error(
|
console.error(
|
||||||
@ -235,6 +239,13 @@ def get_default_app_name() -> str:
|
|||||||
)
|
)
|
||||||
raise typer.Exit(1)
|
raise typer.Exit(1)
|
||||||
|
|
||||||
|
# Make sure the app name is standard for a python package name.
|
||||||
|
if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", app_name):
|
||||||
|
console.error(
|
||||||
|
"The app directory name must start with a letter and can contain letters, numbers, and underscores."
|
||||||
|
)
|
||||||
|
raise typer.Exit(1)
|
||||||
|
|
||||||
return app_name
|
return app_name
|
||||||
|
|
||||||
|
|
||||||
|
@ -306,8 +306,8 @@ def test_initialize_non_existent_gitignore(tmp_path, mocker, gitignore_exists):
|
|||||||
assert set(file_content) - expected == set()
|
assert set(file_content) - expected == set()
|
||||||
|
|
||||||
|
|
||||||
def test_app_default_name(tmp_path, mocker):
|
def test_validate_app_name(tmp_path, mocker):
|
||||||
"""Test that an error is raised if the app name is reflex.
|
"""Test that an error is raised if the app name is reflex or if the name is not according to python package naming conventions.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
tmp_path: Test working dir.
|
tmp_path: Test working dir.
|
||||||
@ -319,7 +319,10 @@ def test_app_default_name(tmp_path, mocker):
|
|||||||
mocker.patch("reflex.utils.prerequisites.os.getcwd", return_value=str(reflex))
|
mocker.patch("reflex.utils.prerequisites.os.getcwd", return_value=str(reflex))
|
||||||
|
|
||||||
with pytest.raises(typer.Exit):
|
with pytest.raises(typer.Exit):
|
||||||
prerequisites.get_default_app_name()
|
prerequisites.validate_app_name()
|
||||||
|
|
||||||
|
with pytest.raises(typer.Exit):
|
||||||
|
prerequisites.validate_app_name(app_name="1_test")
|
||||||
|
|
||||||
|
|
||||||
def test_node_install_windows(tmp_path, mocker):
|
def test_node_install_windows(tmp_path, mocker):
|
||||||
|
Loading…
Reference in New Issue
Block a user