Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pythonlings/core/curriculum.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,19 @@ def _sync_originals(root: Path, src_root: Path) -> None:

def init_workspace(path: Path, *, force: bool = False) -> Path:
path = path.expanduser().resolve()
src_root = source_root().resolve()
if path == src_root:
raise WorkspaceError(
f"cannot init the curriculum source at {path}; "
"pick another location with --path <dir>"
)
if path.exists() and any(path.iterdir()) and not force:
raise WorkspaceError(
f"{path} isn't empty and isn't a pythonlings workspace. "
"Pick another location with --path <dir>, or --force to set up here anyway."
)
path.mkdir(parents=True, exist_ok=True)

src_root = source_root()
_copy_path(src_root / "info.toml", path / "info.toml", overwrite=True)
for dirname in CURRICULUM_DIRS:
_copy_path(src_root / dirname, path / dirname, overwrite=True)
Expand Down
28 changes: 23 additions & 5 deletions tests/unit/test_curriculum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

import pytest

from pythonlings.core import curriculum


Expand Down Expand Up @@ -35,12 +37,28 @@ def test_init_workspace_refuses_non_empty_directory(tmp_path: Path) -> None:
target.mkdir()
(target / "notes.txt").write_text("keep me", encoding="utf-8")

try:
with pytest.raises(curriculum.WorkspaceError) as excinfo:
curriculum.init_workspace(target)
except curriculum.WorkspaceError as exc:
assert "isn't empty and isn't a pythonlings workspace" in str(exc)
else:
raise AssertionError("expected WorkspaceError")

assert "isn't empty and isn't a pythonlings workspace" in str(excinfo.value)


def test_init_workspace_rejects_curriculum_source(tmp_path: Path) -> None:
src_root = curriculum.source_root().resolve()

with pytest.raises(curriculum.WorkspaceError) as excinfo:
curriculum.init_workspace(src_root)

assert "cannot init the curriculum source" in str(excinfo.value)


def test_init_workspace_rejects_curriculum_source_with_force() -> None:
src_root = curriculum.source_root().resolve()

with pytest.raises(curriculum.WorkspaceError) as excinfo:
curriculum.init_workspace(src_root, force=True)

assert "cannot init the curriculum source" in str(excinfo.value)


def test_force_init_preserves_existing_gitignore_entries(tmp_path: Path) -> None:
Expand Down
Loading