fix(resource): update validate_clone_into_url to raise ValueError for invalid URLs
- Change validate_clone_into_url() return type from bool to None - Raise ValueError for empty or invalid git repository URLs - Update BDD steps to catch ValueError and set clone_url_valid accordingly - Aligns with contract requirement from PR #8304 review feedback
This commit is contained in:
@@ -25,13 +25,25 @@ from cleveragents.resource.handlers.clone_into import (
|
||||
@when('I validate clone-into URL "{url}"')
|
||||
def step_validate_url(context: Context, url: str) -> None:
|
||||
"""Validate a clone-into URL."""
|
||||
context.clone_url_valid = validate_clone_into_url(url)
|
||||
context.clone_url_error = None
|
||||
try:
|
||||
validate_clone_into_url(url)
|
||||
context.clone_url_valid = True
|
||||
except ValueError as exc:
|
||||
context.clone_url_valid = False
|
||||
context.clone_url_error = exc
|
||||
|
||||
|
||||
@when("I validate clone-into URL with whitespace only")
|
||||
def step_validate_whitespace_url(context: Context) -> None:
|
||||
"""Validate a whitespace-only clone-into URL."""
|
||||
context.clone_url_valid = validate_clone_into_url(" ")
|
||||
context.clone_url_error = None
|
||||
try:
|
||||
validate_clone_into_url(" ")
|
||||
context.clone_url_valid = True
|
||||
except ValueError as exc:
|
||||
context.clone_url_valid = False
|
||||
context.clone_url_error = exc
|
||||
|
||||
|
||||
@when("I call clone_repo_into_container with empty container_id")
|
||||
|
||||
@@ -131,7 +131,7 @@ def clone_repo_into_container(
|
||||
return target_dir
|
||||
|
||||
|
||||
def validate_clone_into_url(url: str) -> bool:
|
||||
def validate_clone_into_url(url: str) -> None:
|
||||
"""Validate that a URL is a plausible git repository URL.
|
||||
|
||||
Accepts:
|
||||
@@ -143,12 +143,11 @@ def validate_clone_into_url(url: str) -> bool:
|
||||
Args:
|
||||
url: The URL string to validate.
|
||||
|
||||
Returns:
|
||||
``True`` if the URL looks like a valid git repository URL,
|
||||
``False`` otherwise.
|
||||
Raises:
|
||||
ValueError: If the URL is empty or does not match any valid prefix.
|
||||
"""
|
||||
if not url or not url.strip():
|
||||
return False
|
||||
raise ValueError("URL must be a non-empty string")
|
||||
url = url.strip()
|
||||
valid_prefixes = (
|
||||
"https://",
|
||||
@@ -160,4 +159,8 @@ def validate_clone_into_url(url: str) -> bool:
|
||||
"./",
|
||||
"../",
|
||||
)
|
||||
return any(url.startswith(prefix) for prefix in valid_prefixes)
|
||||
if not any(url.startswith(prefix) for prefix in valid_prefixes):
|
||||
raise ValueError(
|
||||
f"Invalid git repository URL: {url!r}. "
|
||||
f"Must start with one of: {', '.join(valid_prefixes)}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user