Add scaffold parent selection: --dir defaults to cwd
This commit is contained in:
@@ -25,3 +25,12 @@ def target_dir(name: str, parent: Path) -> Path:
|
|||||||
if target.exists():
|
if target.exists():
|
||||||
raise ScaffoldError(f"target directory already exists: {target}")
|
raise ScaffoldError(f"target directory already exists: {target}")
|
||||||
return 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
|
||||||
|
|||||||
+19
-1
@@ -4,7 +4,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
import pytest
|
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(
|
@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):
|
def test_target_dir_resolves_parent_slash_name(tmp_path: Path):
|
||||||
"""A fresh target resolves to parent/name."""
|
"""A fresh target resolves to parent/name."""
|
||||||
assert target_dir("demo-project", tmp_path) == tmp_path / "demo-project"
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user