67 lines
2.0 KiB
Python
67 lines
2.0 KiB
Python
"""Tests for scaffold input validation."""
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from new_python.scaffold import ScaffoldError, parent_dir, 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"
|
|
|
|
|
|
def test_parent_dir_defaults_to_cwd(monkeypatch, tmp_path: Path):
|
|
"""Without --dir the parent is the current directory."""
|
|
monkeypatch.chdir(tmp_path)
|
|
assert parent_dir(None) == tmp_path
|
|
|
|
|
|
def test_parent_dir_uses_the_explicit_path(tmp_path: Path):
|
|
"""An explicit --dir selects the parent directory."""
|
|
assert parent_dir(tmp_path) == tmp_path
|
|
|
|
|
|
def test_parent_dir_refuses_missing_directory(tmp_path: Path):
|
|
"""A --dir that is not an existing directory refuses loudly."""
|
|
with pytest.raises(ScaffoldError) as exc:
|
|
parent_dir(tmp_path / "nope")
|
|
assert "--dir" in str(exc.value)
|