fix: Validate app name in adk create command

The `adk create` command now checks if the provided agent name is a valid Python identifier. An invalid name, such as one containing hyphens, will raise a `click.BadParameter` error before any files are created.

Close #3977

Co-authored-by: George Weale <gweale@google.com>
PiperOrigin-RevId: 853001295
This commit is contained in:
George Weale
2026-01-06 17:37:36 -08:00
committed by Copybara-Service
parent 9403a44f34
commit 742c9265a2
2 changed files with 25 additions and 0 deletions
+8
View File
@@ -21,6 +21,8 @@ from typing import Tuple
import click
from ..apps.app import validate_app_name
_INIT_PY_TEMPLATE = """\
from . import agent
"""
@@ -294,6 +296,12 @@ def run_cmd(
VertexAI as backend.
type: Optional[str], Whether to define agent with config file or code.
"""
app_name = os.path.basename(os.path.normpath(agent_name))
try:
validate_app_name(app_name)
except ValueError as exc:
raise click.BadParameter(str(exc)) from exc
agent_folder = os.path.join(os.getcwd(), agent_name)
# check folder doesn't exist or it's empty. Otherwise, throw
if os.path.exists(agent_folder) and os.listdir(agent_folder):
@@ -156,6 +156,23 @@ def test_run_cmd_overwrite_reject(
)
def test_run_cmd_invalid_app_name(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Invalid app names should be rejected before creating any files."""
monkeypatch.setattr(os, "getcwd", lambda: str(tmp_path))
with pytest.raises(click.BadParameter, match="Invalid app name"):
cli_create.run_cmd(
"my-agent",
model="gemini-2.0-flash-001",
google_api_key=None,
google_cloud_project=None,
google_cloud_region=None,
type="code",
)
def test_run_cmd_with_type_config(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None: