gabriel / muse public
test_cmd_cherry_pick.py python
190 lines 7.7 KB
Raw
sha256:b636f72dcba9e190afb980bece906fa5b717fbde014b76ef023df8cb96e01eb9 docs: expand cache plan with all seven testing tiers and do… Sonnet 4.6 131 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, fake_id, blob_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 = fake_id("repo")
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 repo_id=repo_id,
65 parent_ids=[parent_id] if parent_id else [],
66 snapshot_id=snap_id, message=message,
67 committed_at_iso=committed_at.isoformat(),
68 )
69 write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=m))
70 write_commit(root, CommitRecord(
71 commit_id=commit_id, repo_id=repo_id, created_on_branch=branch,
72 snapshot_id=snap_id, message=message, committed_at=committed_at,
73 parent_commit_id=parent_id,
74 ))
75 ref_file.parent.mkdir(parents=True, exist_ok=True)
76 ref_file.write_text(commit_id, encoding="utf-8")
77 return commit_id
78
79
80 def _write_object(root: pathlib.Path, content: bytes) -> str:
81 from muse.core.object_store import write_object
82 oid = blob_id(content)
83 write_object(root, oid, content)
84 return oid
85
86
87 # ---------------------------------------------------------------------------
88 # Parser flag tests
89 # ---------------------------------------------------------------------------
90
91 class TestRegisterFlags:
92 def _parse(self, *args: str) -> "argparse.Namespace":
93 import argparse
94 from muse.cli.commands.cherry_pick import register
95 p = argparse.ArgumentParser()
96 sub = p.add_subparsers()
97 register(sub)
98 return p.parse_args(["cherry-pick", *args])
99
100 def test_default_json_out_is_false(self) -> None:
101 ns = self._parse("abc123")
102 assert ns.json_out is False
103
104 def test_json_flag_sets_json_out(self) -> None:
105 ns = self._parse("--json", "abc123")
106 assert ns.json_out is True
107
108 def test_j_shorthand_sets_json_out(self) -> None:
109 ns = self._parse("-j", "abc123")
110 assert ns.json_out is True
111
112
113 # ---------------------------------------------------------------------------
114 # Tests
115 # ---------------------------------------------------------------------------
116
117 class TestCherryPickCLI:
118 def test_cherry_pick_commit_from_another_branch(self, tmp_path: pathlib.Path) -> None:
119 root, repo_id = _init_repo(tmp_path)
120 base = _make_commit(root, repo_id, branch="main", message="base")
121 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
122 obj = _write_object(root, b"feature content")
123 feature_commit = _make_commit(root, repo_id, branch="feature",
124 message="feature work",
125 manifest={"new.mid": obj})
126 result = runner.invoke(
127 cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False
128 )
129 assert result.exit_code == 0
130
131 def test_cherry_pick_invalid_commit_fails(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, ["cherry-pick", "deadbeef" * 8], env=_env(root))
135 assert result.exit_code != 0
136
137 def test_cherry_pick_creates_new_commit(self, tmp_path: pathlib.Path) -> None:
138 root, repo_id = _init_repo(tmp_path)
139 base = _make_commit(root, repo_id, branch="main", message="base")
140 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
141 obj = _write_object(root, b"cherry content")
142 feature_commit = _make_commit(root, repo_id, branch="feature",
143 message="cherry", manifest={"c.mid": obj})
144 original_head = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
145 runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False)
146 new_head = (root / ".muse" / "refs" / "heads" / "main").read_text().strip()
147 assert new_head != original_head
148
149 def test_cherry_pick_format_json(self, tmp_path: pathlib.Path) -> None:
150 root, repo_id = _init_repo(tmp_path)
151 base = _make_commit(root, repo_id, branch="main", message="base")
152 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
153 obj = _write_object(root, b"json pick")
154 feature_commit = _make_commit(root, repo_id, branch="feature",
155 message="json", manifest={"j.mid": obj})
156 result = runner.invoke(
157 cli, ["cherry-pick", "--json", feature_commit],
158 env=_env(root), catch_exceptions=False
159 )
160 assert result.exit_code == 0
161 data = json.loads(result.output)
162 assert isinstance(data, dict)
163
164 def test_cherry_pick_output_sanitized(self, tmp_path: pathlib.Path) -> None:
165 root, repo_id = _init_repo(tmp_path)
166 base = _make_commit(root, repo_id, branch="main", message="base")
167 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
168 obj = _write_object(root, b"safe content")
169 feature_commit = _make_commit(root, repo_id, branch="feature",
170 message="safe", manifest={"s.mid": obj})
171 result = runner.invoke(cli, ["cherry-pick", feature_commit], env=_env(root), catch_exceptions=False)
172 assert "\x1b" not in result.output
173
174
175 class TestCherryPickStress:
176 def test_cherry_pick_sequence(self, tmp_path: pathlib.Path) -> None:
177 root, repo_id = _init_repo(tmp_path)
178 base = _make_commit(root, repo_id, branch="main", message="base")
179 (root / ".muse" / "refs" / "heads" / "feature").write_text(base)
180 commits = []
181 for i in range(5):
182 obj = _write_object(root, f"content {i}".encode())
183 c = _make_commit(root, repo_id, branch="feature",
184 message=f"commit {i}", manifest={f"f{i}.mid": obj})
185 commits.append(c)
186 for commit_id in commits:
187 result = runner.invoke(
188 cli, ["cherry-pick", commit_id], env=_env(root), catch_exceptions=False
189 )
190 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 131 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 140 days ago