gabriel / muse public
test_cmd_cherry_pick.py python
166 lines 7.0 KB
Raw
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3 docs: docstring sprint contract→find-symbol — idiomatic run… Sonnet 4.6 patch 139 days ago
1 """Comprehensive tests for ``muse cherry-pick``.
2
3 Covers:
4 - E2E: cherry-pick a specific commit onto current branch
5 - Integration: commit is replayed, creates new commit
6 - Security: sanitized output for conflict paths
7 - Stress: cherry-pick many commits
8 """
9
10 from __future__ import annotations
11
12 import datetime
13 import json
14 import pathlib
15 import uuid
16
17 import pytest
18 from tests.cli_test_helper import CliRunner
19 from muse.core._types import long_id
20
21 cli = None # argparse migration — CliRunner ignores this arg
22
23 runner = CliRunner()
24
25
26 # ---------------------------------------------------------------------------
27 # Shared helpers
28 # ---------------------------------------------------------------------------
29
30 def _env(root: pathlib.Path) -> Manifest:
31 return {"MUSE_REPO_ROOT": str(root)}
32
33
34 def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]:
35 muse_dir = tmp_path / ".muse"
36 muse_dir.mkdir()
37 repo_id = str(uuid.uuid4())
38 (muse_dir / "repo.json").write_text(json.dumps({
39 "repo_id": repo_id,
40 "domain": "code",
41 "default_branch": "main",
42 "created_at": "2025-01-01T00:00:00+00:00",
43 }), encoding="utf-8")
44 (muse_dir / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8")
45 (muse_dir / "refs" / "heads").mkdir(parents=True)
46 (muse_dir / "snapshots").mkdir()
47 (muse_dir / "commits").mkdir()
48 (muse_dir / "objects").mkdir()
49 return tmp_path, repo_id
50
51
52 def _make_commit(root: pathlib.Path, repo_id: str, branch: str = "main",
53 message: str = "test",
54 manifest: Manifest | None = None) -> str:
55 from muse.core.store import CommitRecord, SnapshotRecord, write_commit, write_snapshot
56 from muse.core.snapshot import compute_snapshot_id, compute_commit_id
57
58 ref_file = root / ".muse" / "refs" / "heads" / branch
59 parent_id = ref_file.read_text().strip() if ref_file.exists() else None
60 m = manifest or {}
61 snap_id = compute_snapshot_id(m)
62 committed_at = datetime.datetime.now(datetime.timezone.utc)
63 commit_id = compute_commit_id(
64 parent_ids=[parent_id] if parent_id else [],
65 snapshot_id=snap_id, message=message,
66 committed_at_iso=committed_at.isoformat(),
67 )
68 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=m))
69 write_commit(root, CommitRecord(
70 commit_id=commit_id, repo_id=repo_id, branch=branch,
71 snapshot_id=snap_id, message=message, committed_at=committed_at,
72 parent_commit_id=parent_id,
73 ))
74 ref_file.parent.mkdir(parents=True, exist_ok=True)
75 ref_file.write_text(commit_id, encoding="utf-8")
76 return commit_id
77
78
79 def _write_object(root: pathlib.Path, content: bytes) -> str:
80 import hashlib
81 hex_id = hashlib.sha256(content).hexdigest()
82 obj_path = root / ".muse" / "objects" / hex_id[:2] / hex_id[2:]
83 obj_path.parent.mkdir(parents=True, exist_ok=True)
84 obj_path.write_bytes(content)
85 # Canonical Muse object ID includes the sha256: prefix
86 return long_id(hex_id)
87
88
89 # ---------------------------------------------------------------------------
90 # Tests
91 # ---------------------------------------------------------------------------
92
93 class TestCherryPickCLI:
94 def test_cherry_pick_commit_from_another_branch(self, tmp_path: pathlib.Path) -> None:
95 root, repo_id = _init_repo(tmp_path)
96 base = _make_commit(root, repo_id, branch="main", message="base")
97 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
98 obj = _write_object(root, b"feature content")
99 feature_commit = _make_commit(root, repo_id, branch="feature",
100 message="feature work",
101 manifest={"new.mid": obj})
102 result = runner.invoke(
103 cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False
104 )
105 assert result.exit_code == 0
106
107 def test_cherry_pick_invalid_commit_fails(self, tmp_path: pathlib.Path) -> None:
108 root, repo_id = _init_repo(tmp_path)
109 _make_commit(root, repo_id)
110 result = runner.invoke(cli, ["cherry-pick", "deadbeef" * 8], env=_env(root))
111 assert result.exit_code != 0
112
113 def test_cherry_pick_creates_new_commit(self, tmp_path: pathlib.Path) -> None:
114 root, repo_id = _init_repo(tmp_path)
115 base = _make_commit(root, repo_id, branch="main", message="base")
116 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
117 obj = _write_object(root, b"cherry content")
118 feature_commit = _make_commit(root, repo_id, branch="feature",
119 message="cherry", manifest={"c.mid": obj})
120 original_head = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
121 runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False)
122 new_head = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
123 assert new_head != original_head
124
125 def test_cherry_pick_format_json(self, tmp_path: pathlib.Path) -> None:
126 root, repo_id = _init_repo(tmp_path)
127 base = _make_commit(root, repo_id, branch="main", message="base")
128 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
129 obj = _write_object(root, b"json pick")
130 feature_commit = _make_commit(root, repo_id, branch="feature",
131 message="json", manifest={"j.mid": obj})
132 result = runner.invoke(
133 cli, ["cherry-pick", "--format", "json", feature_commit],
134 env=_env(root), catch_exceptions=False
135 )
136 assert result.exit_code == 0
137 data = json.loads(result.output)
138 assert isinstance(data, dict)
139
140 def test_cherry_pick_output_sanitized(self, tmp_path: pathlib.Path) -> None:
141 root, repo_id = _init_repo(tmp_path)
142 base = _make_commit(root, repo_id, branch="main", message="base")
143 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
144 obj = _write_object(root, b"safe content")
145 feature_commit = _make_commit(root, repo_id, branch="feature",
146 message="safe", manifest={"s.mid": obj})
147 result = runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False)
148 assert "\x1b" not in result.output
149
150
151 class TestCherryPickStress:
152 def test_cherry_pick_sequence(self, tmp_path: pathlib.Path) -> None:
153 root, repo_id = _init_repo(tmp_path)
154 base = _make_commit(root, repo_id, branch="main", message="base")
155 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
156 commits = []
157 for i in range(5):
158 obj = _write_object(root, f"content {i}".encode())
159 c = _make_commit(root, repo_id, branch="feature",
160 message=f"commit {i}", manifest={f"f{i}.mid": obj})
161 commits.append(c)
162 for commit_id in commits:
163 result = runner.invoke(
164 cli, ["cherry-pick", commit_id], env=_env(root), catch_exceptions=False
165 )
166 assert result.exit_code == 0
File History 2 commits
sha256:88ac91129873e6a496e9189515aa690eb893ae25d69c8f72af141a2be5068eb3 docs: docstring sprint contract→find-symbol — idiomatic run… Sonnet 4.6 patch 139 days ago
sha256:a09b1b4f6838754495547f200aa0ce88e2f56ffc5b20b900f6f0cff2c3cdede9 fix(cursorignore): remove git-ism (.git/worktrees) Human minor 142 days ago