Less stack-centric and location-centric model.
This commit is contained in:
72
cli/tests/test_add.py
Normal file
72
cli/tests/test_add.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""Tests for castle add — adopting existing repos as programs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from castle_cli.config import load_config
|
||||
|
||||
|
||||
def _run_add(castle_root: Path, **kwargs: object) -> object:
|
||||
with (
|
||||
patch("castle_cli.commands.add.load_config") as mock_load,
|
||||
patch("castle_cli.commands.add.save_config"),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
from castle_cli.commands.add import run_add
|
||||
|
||||
args = Namespace(name=None, description="", **kwargs)
|
||||
rc = run_add(args)
|
||||
return rc, config
|
||||
|
||||
|
||||
class TestAdd:
|
||||
def test_adopt_python_path_assigns_stack(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
repo = tmp_path / "mytool"
|
||||
repo.mkdir()
|
||||
(repo / "pyproject.toml").write_text('[project]\nname = "mytool"\ndependencies = []\n')
|
||||
rc, config = _run_add(castle_root, target=str(repo))
|
||||
assert rc == 0
|
||||
prog = config.programs["mytool"]
|
||||
assert prog.stack == "python-cli" # detected
|
||||
assert prog.source == str(repo.resolve())
|
||||
|
||||
def test_adopt_fastapi_detects_daemon(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
repo = tmp_path / "svc"
|
||||
repo.mkdir()
|
||||
(repo / "pyproject.toml").write_text(
|
||||
'[project]\nname = "svc"\ndependencies = ["fastapi>=0.1"]\n'
|
||||
)
|
||||
rc, config = _run_add(castle_root, target=str(repo))
|
||||
assert rc == 0
|
||||
assert config.programs["svc"].stack == "python-fastapi"
|
||||
assert config.programs["svc"].behavior == "daemon"
|
||||
|
||||
def test_adopt_rust_declares_commands(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
repo = tmp_path / "rusty"
|
||||
repo.mkdir()
|
||||
(repo / "Cargo.toml").write_text("[package]\nname = \"rusty\"\n")
|
||||
rc, config = _run_add(castle_root, target=str(repo))
|
||||
assert rc == 0
|
||||
prog = config.programs["rusty"]
|
||||
assert prog.stack is None # no castle stack for rust
|
||||
# build lands in BuildSpec; other verbs in CommandsSpec
|
||||
assert prog.build is not None
|
||||
assert prog.build.commands == [["cargo", "build", "--release"]]
|
||||
assert prog.commands is not None
|
||||
assert prog.commands.run == [["cargo", "run"]]
|
||||
|
||||
def test_adopt_git_url_records_repo(self, castle_root: Path) -> None:
|
||||
rc, config = _run_add(
|
||||
castle_root, target="https://github.com/someone/widget.git"
|
||||
)
|
||||
assert rc == 0
|
||||
prog = config.programs["widget"]
|
||||
assert prog.repo == "https://github.com/someone/widget.git"
|
||||
|
||||
def test_missing_path_fails(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
rc, _ = _run_add(castle_root, target=str(tmp_path / "nope"))
|
||||
assert rc == 1
|
||||
@@ -12,11 +12,13 @@ from castle_cli.config import load_config
|
||||
class TestCreateCommand:
|
||||
"""Tests for the create command."""
|
||||
|
||||
def test_create_service(self, castle_root: Path) -> None:
|
||||
def test_create_service(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
"""Create a new service project."""
|
||||
repos = tmp_path / "repos"
|
||||
with (
|
||||
patch("castle_cli.commands.create.load_config") as mock_load,
|
||||
patch("castle_cli.commands.create.save_config") as mock_save,
|
||||
patch("castle_cli.commands.create.REPOS_DIR", repos),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
@@ -32,7 +34,7 @@ class TestCreateCommand:
|
||||
result = run_create(args)
|
||||
|
||||
assert result == 0
|
||||
project_dir = castle_root / "code" / "my-api"
|
||||
project_dir = repos / "my-api"
|
||||
assert project_dir.exists()
|
||||
assert (project_dir / "pyproject.toml").exists()
|
||||
assert (project_dir / "src" / "my_api" / "main.py").exists()
|
||||
@@ -49,11 +51,13 @@ class TestCreateCommand:
|
||||
assert svc.component == "my-api"
|
||||
mock_save.assert_called_once()
|
||||
|
||||
def test_create_tool(self, castle_root: Path) -> None:
|
||||
def test_create_tool(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
"""Create a new tool project."""
|
||||
repos = tmp_path / "repos"
|
||||
with (
|
||||
patch("castle_cli.commands.create.load_config") as mock_load,
|
||||
patch("castle_cli.commands.create.save_config"),
|
||||
patch("castle_cli.commands.create.REPOS_DIR", repos),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
@@ -64,7 +68,7 @@ class TestCreateCommand:
|
||||
result = run_create(args)
|
||||
|
||||
assert result == 0
|
||||
project_dir = castle_root / "code" / "my-tool2"
|
||||
project_dir = repos / "my-tool2"
|
||||
assert project_dir.exists()
|
||||
assert (project_dir / "src" / "my_tool2" / "main.py").exists()
|
||||
assert (project_dir / "CLAUDE.md").exists()
|
||||
@@ -91,11 +95,12 @@ class TestCreateCommand:
|
||||
|
||||
assert result == 1
|
||||
|
||||
def test_create_auto_port(self, castle_root: Path) -> None:
|
||||
def test_create_auto_port(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
"""Service creation auto-assigns next available port."""
|
||||
with (
|
||||
patch("castle_cli.commands.create.load_config") as mock_load,
|
||||
patch("castle_cli.commands.create.save_config"),
|
||||
patch("castle_cli.commands.create.REPOS_DIR", tmp_path / "repos"),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
|
||||
67
cli/tests/test_delete.py
Normal file
67
cli/tests/test_delete.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""Tests for castle delete."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from argparse import Namespace
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from castle_cli.config import load_config
|
||||
|
||||
|
||||
def _run_delete(castle_root: Path, **kwargs: object) -> object:
|
||||
with (
|
||||
patch("castle_cli.commands.delete.load_config") as mock_load,
|
||||
patch("castle_cli.commands.delete.save_config"),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
from castle_cli.commands.delete import run_delete
|
||||
|
||||
args = Namespace(source=False, yes=True, **kwargs)
|
||||
rc = run_delete(args)
|
||||
return rc, config
|
||||
|
||||
|
||||
class TestDelete:
|
||||
def test_delete_program(self, castle_root: Path) -> None:
|
||||
rc, config = _run_delete(castle_root, name="test-tool")
|
||||
assert rc == 0
|
||||
assert "test-tool" not in config.programs
|
||||
|
||||
def test_delete_unknown_fails(self, castle_root: Path) -> None:
|
||||
rc, _ = _run_delete(castle_root, name="does-not-exist")
|
||||
assert rc == 1
|
||||
|
||||
def test_delete_source_removes_dir(self, castle_root: Path, tmp_path: Path) -> None:
|
||||
# Point a program's source at a real temp dir, then delete with --source.
|
||||
src = tmp_path / "victim"
|
||||
src.mkdir()
|
||||
(src / "file.txt").write_text("x")
|
||||
with (
|
||||
patch("castle_cli.commands.delete.load_config") as mock_load,
|
||||
patch("castle_cli.commands.delete.save_config"),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
config.programs["test-tool"].source = str(src)
|
||||
mock_load.return_value = config
|
||||
from castle_cli.commands.delete import run_delete
|
||||
|
||||
rc = run_delete(Namespace(name="test-tool", source=True, yes=True))
|
||||
assert rc == 0
|
||||
assert not src.exists()
|
||||
|
||||
def test_abort_without_yes_and_no_input(self, castle_root: Path) -> None:
|
||||
# No --yes and no stdin → aborts safely (returns 1, leaves entry).
|
||||
with (
|
||||
patch("castle_cli.commands.delete.load_config") as mock_load,
|
||||
patch("castle_cli.commands.delete.save_config"),
|
||||
patch("builtins.input", side_effect=EOFError),
|
||||
):
|
||||
config = load_config(castle_root)
|
||||
mock_load.return_value = config
|
||||
from castle_cli.commands.delete import run_delete
|
||||
|
||||
rc = run_delete(Namespace(name="test-tool", source=False, yes=False))
|
||||
assert rc == 1
|
||||
assert "test-tool" in config.programs
|
||||
Reference in New Issue
Block a user