test_cmd_worktree.py
python
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago
| 1 | """Comprehensive tests for ``muse worktree``. |
| 2 | |
| 3 | Covers: |
| 4 | - E2E: add, list, remove worktrees |
| 5 | - Integration: worktree directory created and removed |
| 6 | - Security: sanitized path output |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import datetime |
| 12 | import json |
| 13 | import pathlib |
| 14 | import uuid |
| 15 | |
| 16 | import pytest |
| 17 | from tests.cli_test_helper import CliRunner |
| 18 | from muse.core._types import fake_id |
| 19 | |
| 20 | cli = None # argparse migration — CliRunner ignores this arg |
| 21 | |
| 22 | runner = CliRunner() |
| 23 | |
| 24 | |
| 25 | # --------------------------------------------------------------------------- |
| 26 | # Shared helpers |
| 27 | # --------------------------------------------------------------------------- |
| 28 | |
| 29 | def _env(root: pathlib.Path) -> Manifest: |
| 30 | return {"MUSE_REPO_ROOT": str(root)} |
| 31 | |
| 32 | |
| 33 | def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: |
| 34 | muse_dir = tmp_path / ".muse" |
| 35 | muse_dir.mkdir() |
| 36 | repo_id = fake_id("repo") |
| 37 | (muse_dir / "repo.json").write_text(json.dumps({ |
| 38 | "repo_id": repo_id, |
| 39 | "domain": "midi", |
| 40 | "default_branch": "main", |
| 41 | "created_at": "2025-01-01T00:00:00+00:00", |
| 42 | }), encoding="utf-8") |
| 43 | (muse_dir / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 44 | (muse_dir / "refs" / "heads").mkdir(parents=True) |
| 45 | (muse_dir / "snapshots").mkdir() |
| 46 | (muse_dir / "commits").mkdir() |
| 47 | (muse_dir / "objects").mkdir() |
| 48 | return tmp_path, repo_id |
| 49 | |
| 50 | |
| 51 | def _make_commit(root: pathlib.Path, repo_id: str, message: str = "test") -> str: |
| 52 | from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot |
| 53 | from muse.core.snapshot import compute_snapshot_id, compute_commit_id |
| 54 | |
| 55 | ref_file = root / ".muse" / "refs" / "heads" / "main" |
| 56 | parent_id = ref_file.read_text().strip() if ref_file.exists() else None |
| 57 | manifest: Manifest = {} |
| 58 | snap_id = compute_snapshot_id(manifest) |
| 59 | committed_at = datetime.datetime.now(datetime.timezone.utc) |
| 60 | commit_id = compute_commit_id( |
| 61 | repo_id=repo_id, |
| 62 | parent_ids=[parent_id] if parent_id else [], |
| 63 | snapshot_id=snap_id, message=message, |
| 64 | committed_at_iso=committed_at.isoformat(), |
| 65 | ) |
| 66 | write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) |
| 67 | write_commit(root, CommitRecord( |
| 68 | commit_id=commit_id, repo_id=repo_id, created_on_branch="main", |
| 69 | snapshot_id=snap_id, message=message, committed_at=committed_at, |
| 70 | parent_commit_id=parent_id, |
| 71 | )) |
| 72 | ref_file.parent.mkdir(parents=True, exist_ok=True) |
| 73 | ref_file.write_text(commit_id, encoding="utf-8") |
| 74 | return commit_id |
| 75 | |
| 76 | |
| 77 | # --------------------------------------------------------------------------- |
| 78 | # Tests |
| 79 | # --------------------------------------------------------------------------- |
| 80 | |
| 81 | class TestWorktreeCLI: |
| 82 | def test_worktree_list_empty(self, tmp_path: pathlib.Path) -> None: |
| 83 | root, _ = _init_repo(tmp_path) |
| 84 | result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False) |
| 85 | assert result.exit_code == 0 |
| 86 | |
| 87 | def test_worktree_add_creates_directory(self, tmp_path: pathlib.Path) -> None: |
| 88 | root, repo_id = _init_repo(tmp_path) |
| 89 | _make_commit(root, repo_id) |
| 90 | result = runner.invoke( |
| 91 | cli, ["worktree", "add", "my-wt", "main"], |
| 92 | env=_env(root), catch_exceptions=False |
| 93 | ) |
| 94 | assert result.exit_code == 0 |
| 95 | |
| 96 | def test_worktree_list_after_add(self, tmp_path: pathlib.Path) -> None: |
| 97 | root, repo_id = _init_repo(tmp_path) |
| 98 | _make_commit(root, repo_id) |
| 99 | runner.invoke( |
| 100 | cli, ["worktree", "add", "my-wt2", "main"], |
| 101 | env=_env(root), catch_exceptions=False |
| 102 | ) |
| 103 | result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False) |
| 104 | assert result.exit_code == 0 |
| 105 | |
| 106 | def test_worktree_remove(self, tmp_path: pathlib.Path) -> None: |
| 107 | root, repo_id = _init_repo(tmp_path) |
| 108 | _make_commit(root, repo_id) |
| 109 | runner.invoke( |
| 110 | cli, ["worktree", "add", "rm-wt", "main"], |
| 111 | env=_env(root), catch_exceptions=False |
| 112 | ) |
| 113 | result = runner.invoke( |
| 114 | cli, ["worktree", "remove", "rm-wt"], |
| 115 | env=_env(root), catch_exceptions=False |
| 116 | ) |
| 117 | assert result.exit_code == 0 |
| 118 | |
| 119 | def test_worktree_list_format_json(self, tmp_path: pathlib.Path) -> None: |
| 120 | root, _ = _init_repo(tmp_path) |
| 121 | result = runner.invoke( |
| 122 | cli, ["worktree", "list", "--json"], |
| 123 | env=_env(root), catch_exceptions=False |
| 124 | ) |
| 125 | assert result.exit_code == 0 |
| 126 | data = json.loads(result.output) |
| 127 | envelope = data if isinstance(data, dict) else {} |
| 128 | worktrees = envelope.get("worktrees", data) |
| 129 | assert isinstance(worktrees, list) |
| 130 | |
| 131 | def test_worktree_output_sanitized(self, tmp_path: pathlib.Path) -> None: |
| 132 | root, repo_id = _init_repo(tmp_path) |
| 133 | _make_commit(root, repo_id) |
| 134 | result = runner.invoke(cli, ["worktree", "list"], env=_env(root), catch_exceptions=False) |
| 135 | assert "\x1b" not in result.output |
| 136 | |
| 137 | def test_worktree_remove_nonexistent_fails(self, tmp_path: pathlib.Path) -> None: |
| 138 | root, _ = _init_repo(tmp_path) |
| 139 | result = runner.invoke(cli, ["worktree", "remove", "nonexistent"], env=_env(root)) |
| 140 | assert result.exit_code != 0 |
File History
3 commits
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9
docs: expand cache plan with all seven testing tiers and do…
Sonnet 4.6
132 days ago
sha256:7f9e2ef5286aedad9c1e6011b4c46ca27f39dbdad6e3409357e36b26e46b3b7c
docs: docstring sprint for-each-ref→hotspots — idiomatic ru…
Sonnet 4.6
patch
138 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9
fix(cursorignore): remove git-ism (.git/worktrees)
Human
minor
⚠
141 days ago