diff --git a/new_python/scaffold.py b/new_python/scaffold.py index 7b44a54..58642f8 100644 --- a/new_python/scaffold.py +++ b/new_python/scaffold.py @@ -25,3 +25,12 @@ def target_dir(name: str, parent: Path) -> Path: if target.exists(): raise ScaffoldError(f"target directory already exists: {target}") return target + + +def parent_dir(explicit: Path | None) -> Path: + """Return the parent for a new project: *explicit* when given, else cwd.""" + if explicit is None: + return Path.cwd() + if not explicit.is_dir(): + raise ScaffoldError(f"--dir is not an existing directory: {explicit}") + return explicit diff --git a/tests/test_scaffold.py b/tests/test_scaffold.py index 7079bb3..2031550 100644 --- a/tests/test_scaffold.py +++ b/tests/test_scaffold.py @@ -4,7 +4,7 @@ from pathlib import Path import pytest -from new_python.scaffold import ScaffoldError, target_dir, validate_name +from new_python.scaffold import ScaffoldError, parent_dir, target_dir, validate_name @pytest.mark.parametrize( @@ -46,3 +46,21 @@ def test_target_dir_refuses_existing_directory(tmp_path): 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)