Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reflex init app name validator, prevent import failure during reflex run #2336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions reflex/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def _init(
# Show system info
exec.output_system_info()

# Get the app name.
app_name = prerequisites.get_default_app_name() if name is None else name
# Validate the app name.
app_name = prerequisites.validate_app_name(name)
console.rule(f"[bold]Initializing {app_name}")

prerequisites.check_latest_package_version(constants.Reflex.MODULE_NAME)
Expand Down
23 changes: 17 additions & 6 deletions reflex/utils/prerequisites.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,37 @@ def get_production_backend_url() -> str:
)


def get_default_app_name() -> str:
"""Get the default app name.
def validate_app_name(app_name: str | None = None) -> str:
"""Validate the app name.

The default app name is the name of the current directory.

Args:
app_name: the name passed by user during reflex init

Returns:
The default app name.
The app name after validation.

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".
if app_name == constants.Reflex.MODULE_NAME:
console.error(
f"The app directory cannot be named [bold]{constants.Reflex.MODULE_NAME}[/bold]."
)
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


Expand Down
9 changes: 6 additions & 3 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ def test_initialize_non_existent_gitignore(tmp_path, mocker, gitignore_exists):
assert set(file_content) - expected == set()


def test_app_default_name(tmp_path, mocker):
"""Test that an error is raised if the app name is reflex.
def test_validate_app_name(tmp_path, mocker):
"""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:
tmp_path: Test working dir.
Expand All @@ -319,7 +319,10 @@ def test_app_default_name(tmp_path, mocker):
mocker.patch("reflex.utils.prerequisites.os.getcwd", return_value=str(reflex))

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):
Expand Down