Add scaffold validation: name rule and target refusal

This commit is contained in:
jay
2026-11-09 13:28:04 -05:00
parent 91d55d6008
commit 29306b0f25
2 changed files with 75 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
"""Create new python projects from the versioned template."""
import re
from pathlib import Path
NAME_PATTERN = re.compile(r"^[a-z]+(-[a-z]+)*$")
NAME_RULE = "lowercase a-z words separated by single hyphens"
class ScaffoldError(Exception):
"""A refusal caused by invalid input or an unusable target."""
def validate_name(name: str) -> str:
"""Return *name* when it matches the naming rule, else refuse loudly."""
if not NAME_PATTERN.match(name):
raise ScaffoldError(f"invalid name {name!r}: must be {NAME_RULE}")
return name
def target_dir(name: str, parent: Path) -> Path:
"""Return *parent* / *name*, refusing when that directory already exists."""
target = parent / name
if target.exists():
raise ScaffoldError(f"target directory already exists: {target}")
return target
+48
View File
@@ -0,0 +1,48 @@
"""Tests for scaffold input validation."""
from pathlib import Path
import pytest
from new_python.scaffold import ScaffoldError, target_dir, validate_name
@pytest.mark.parametrize(
"bad",
[
"Demo-Project",
"demo_project",
"-demo",
"demo-",
"demo--project",
"demo project",
"demo1",
"demo.project",
"",
],
)
def test_validate_name_refuses_invalid_names_with_the_rule(bad):
"""Names outside lowercase-words-with-hyphens refuse, stating the rule."""
with pytest.raises(ScaffoldError) as exc:
validate_name(bad)
assert "lowercase" in str(exc.value)
def test_validate_name_accepts_dashed_and_single_words():
"""Valid forms pass through unchanged."""
assert validate_name("demo-project") == "demo-project"
assert validate_name("demo") == "demo"
def test_target_dir_refuses_existing_directory(tmp_path):
"""An existing target directory refuses loudly with the path."""
(tmp_path / "demo-project").mkdir()
with pytest.raises(ScaffoldError) as exc:
target_dir("demo-project", tmp_path)
assert "exists" in str(exc.value)
assert "demo-project" in str(exc.value)
def test_target_dir_resolves_parent_slash_name(tmp_path: Path):
"""A fresh target resolves to parent/name."""
assert target_dir("demo-project", tmp_path) == tmp_path / "demo-project"