fix: correct folder name validation logic to match test expectations

- Fix validation regex to catch names starting with invalid characters like '@#/'
- Ensure validation properly raises ValueError for cases expected by tests
- Maintain support for valid cases like 'my.project/' -> 'myproject'
- Address lucasgomide's comment about valid Python module names

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-26 13:18:41 +00:00
parent eddeacebcc
commit 911e02da8e
2 changed files with 11 additions and 9 deletions

View File

@@ -25,6 +25,10 @@ def create_folder_structure(name, parent_folder=None):
folder_name = name.replace(" ", "_").replace("-", "_").lower()
folder_name = re.sub(r'[^a-zA-Z0-9_]', '', folder_name)
# Check if the name starts with invalid characters or is primarily invalid
if re.match(r'^[^a-zA-Z0-9_-]+', name):
raise ValueError(f"Project name '{name}' contains no valid characters for a Python module name")
if not folder_name:
raise ValueError(f"Project name '{name}' contains no valid characters for a Python module name")

View File

@@ -1,3 +1,4 @@
import keyword
import shutil
import tempfile
from pathlib import Path
@@ -169,8 +170,6 @@ def test_create_folder_structure_raises_error_for_invalid_names():
def test_create_folder_structure_validates_names():
import keyword
with tempfile.TemporaryDirectory() as temp_dir:
valid_cases = [
("hello/", "hello", "Hello"),
@@ -207,6 +206,12 @@ def test_create_crew_with_parent_folder_and_trailing_slash(mock_load_env, mock_w
with tempfile.TemporaryDirectory() as work_dir:
parent_path = Path(work_dir) / "parent"
parent_path.mkdir()
create_crew("child-crew/", skip_provider=True, parent_folder=parent_path)
crew_path = parent_path / "child_crew"
assert crew_path.exists()
assert not (crew_path / "src").exists()
def test_create_folder_structure_folder_name_validation():
@@ -239,10 +244,3 @@ def test_create_folder_structure_folder_name_validation():
if folder_path.exists():
shutil.rmtree(folder_path)
create_crew("child-crew/", skip_provider=True, parent_folder=parent_path)
crew_path = parent_path / "child_crew"
assert crew_path.exists()
assert not (crew_path / "src").exists()