test_cmd_describe_hardening.py
python
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6
chore(tests): add docstring to tests/__init__.py so rc14 tr…
Human
26 days ago
| 1 | """Hardening test suite for ``muse describe``. |
| 2 | |
| 3 | Coverage: |
| 4 | - Unit: describe_commit core — no tags, exact, distance, long, match_pattern, |
| 5 | first_parent, abbrev, exact_match, _MAX_WALK budget, multi-tag tie-break |
| 6 | - Security: ANSI injection in tag names sanitized in text output, |
| 7 | raw in JSON; --ref ANSI passthrough in error message |
| 8 | - Error routing: all user errors routed to stderr |
| 9 | - JSON schema: _DescribeJson shape, all fields present, repo_id + branch |
| 10 | - New flags: --match, --exact-match, --first-parent, --abbrev, --json |
| 11 | - Integration: tag walk across merge commits, --first-parent vs full walk |
| 12 | - E2E: help output, combined flags |
| 13 | - Stress: 5 000-commit chain, 200-tag repo, concurrent reads |
| 14 | """ |
| 15 | |
| 16 | from __future__ import annotations |
| 17 | |
| 18 | import datetime |
| 19 | import hashlib |
| 20 | import json |
| 21 | import pathlib |
| 22 | import threading |
| 23 | from typing import TypedDict |
| 24 | from unittest.mock import patch |
| 25 | |
| 26 | import pytest |
| 27 | from tests.cli_test_helper import CliRunner, InvokeResult |
| 28 | |
| 29 | from muse.core.describe import describe_commit, _MAX_WALK |
| 30 | from muse.core.object_store import write_object |
| 31 | from muse.core.snapshot import compute_commit_id, compute_snapshot_id |
| 32 | from muse.core.store import CommitRecord, SnapshotRecord, TagRecord, write_commit, write_snapshot, write_tag |
| 33 | from muse.core._types import Manifest |
| 34 | |
| 35 | runner = CliRunner() |
| 36 | _REPO_ID = "describe-hard-test" |
| 37 | |
| 38 | |
| 39 | # --------------------------------------------------------------------------- |
| 40 | # Helpers |
| 41 | # --------------------------------------------------------------------------- |
| 42 | |
| 43 | |
| 44 | def _sha(data: bytes) -> str: |
| 45 | return hashlib.sha256(data).hexdigest() |
| 46 | |
| 47 | |
| 48 | def _init_repo(path: pathlib.Path, *, domain: str = "midi") -> pathlib.Path: |
| 49 | muse = path / ".muse" |
| 50 | for sub in ("commits", "snapshots", "objects", "refs/heads", f"tags/{_REPO_ID}"): |
| 51 | (muse / sub).mkdir(parents=True, exist_ok=True) |
| 52 | (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") |
| 53 | (muse / "repo.json").write_text( |
| 54 | json.dumps({"repo_id": _REPO_ID, "domain": domain}), |
| 55 | encoding="utf-8", |
| 56 | ) |
| 57 | return path |
| 58 | |
| 59 | |
| 60 | def _make_commit( |
| 61 | root: pathlib.Path, |
| 62 | parent_id: str | None = None, |
| 63 | parent2_id: str | None = None, |
| 64 | content: bytes = b"data", |
| 65 | branch: str = "main", |
| 66 | ) -> str: |
| 67 | obj_id = _sha(content) |
| 68 | write_object(root, obj_id, content) |
| 69 | manifest = {f"f_{obj_id[:8]}.txt": obj_id} |
| 70 | snap_id = compute_snapshot_id(manifest) |
| 71 | write_snapshot(root, SnapshotRecord(snapshot_id=snap_id, manifest=manifest)) |
| 72 | committed_at = datetime.datetime.now(datetime.timezone.utc) |
| 73 | parent_ids = [pid for pid in (parent_id, parent2_id) if pid is not None] |
| 74 | commit_id = compute_commit_id( |
| 75 | parent_ids, snap_id, f"msg", committed_at.isoformat() |
| 76 | ) |
| 77 | rec = CommitRecord( |
| 78 | commit_id=commit_id, |
| 79 | repo_id=_REPO_ID, |
| 80 | branch=branch, |
| 81 | snapshot_id=snap_id, |
| 82 | message="msg", |
| 83 | committed_at=committed_at, |
| 84 | parent_commit_id=parent_id, |
| 85 | parent2_commit_id=parent2_id, |
| 86 | ) |
| 87 | write_commit(root, rec) |
| 88 | (root / ".muse" / "refs" / "heads" / branch).write_text( |
| 89 | commit_id, encoding="utf-8" |
| 90 | ) |
| 91 | return commit_id |
| 92 | |
| 93 | |
| 94 | def _make_tag(root: pathlib.Path, tag: str, commit_id: str) -> None: |
| 95 | import uuid as _uuid |
| 96 | write_tag( |
| 97 | root, |
| 98 | TagRecord( |
| 99 | tag_id=str(_uuid.uuid4()), |
| 100 | tag=tag, |
| 101 | commit_id=commit_id, |
| 102 | repo_id=_REPO_ID, |
| 103 | created_at=datetime.datetime.now(datetime.timezone.utc), |
| 104 | ), |
| 105 | ) |
| 106 | |
| 107 | |
| 108 | def _env(repo: pathlib.Path) -> Manifest: |
| 109 | return {"MUSE_REPO_ROOT": str(repo)} |
| 110 | |
| 111 | |
| 112 | def _invoke(args: list[str], env: Manifest) -> InvokeResult: |
| 113 | return runner.invoke(None, args, env=env) |
| 114 | |
| 115 | |
| 116 | class _DescribeOut(TypedDict): |
| 117 | commit_id: str |
| 118 | tag: str | None |
| 119 | distance: int |
| 120 | short_sha: str |
| 121 | name: str |
| 122 | exact: bool |
| 123 | repo_id: str |
| 124 | branch: str |
| 125 | |
| 126 | |
| 127 | def _parse_json(result: InvokeResult) -> _DescribeOut: |
| 128 | raw = json.loads(result.output.strip()) |
| 129 | return _DescribeOut( |
| 130 | commit_id=raw["commit_id"], |
| 131 | tag=raw["tag"], |
| 132 | distance=raw["distance"], |
| 133 | short_sha=raw["short_sha"], |
| 134 | name=raw["name"], |
| 135 | exact=raw["exact"], |
| 136 | repo_id=raw["repo_id"], |
| 137 | branch=raw["branch"], |
| 138 | ) |
| 139 | |
| 140 | |
| 141 | # --------------------------------------------------------------------------- |
| 142 | # Unit: describe_commit core |
| 143 | # --------------------------------------------------------------------------- |
| 144 | |
| 145 | |
| 146 | def test_core_no_tags_returns_short_sha(tmp_path: pathlib.Path) -> None: |
| 147 | _init_repo(tmp_path) |
| 148 | cid = _make_commit(tmp_path, content=b"a") |
| 149 | r = describe_commit(tmp_path, _REPO_ID, cid) |
| 150 | assert r["tag"] is None |
| 151 | assert r["name"] == cid[:12] |
| 152 | assert r["exact"] is False |
| 153 | |
| 154 | |
| 155 | def test_core_exact_tag(tmp_path: pathlib.Path) -> None: |
| 156 | _init_repo(tmp_path) |
| 157 | cid = _make_commit(tmp_path, content=b"b") |
| 158 | _make_tag(tmp_path, "v1.0.0", cid) |
| 159 | r = describe_commit(tmp_path, _REPO_ID, cid) |
| 160 | assert r["tag"] == "v1.0.0" |
| 161 | assert r["distance"] == 0 |
| 162 | assert r["exact"] is True |
| 163 | assert r["name"] == "v1.0.0" |
| 164 | |
| 165 | |
| 166 | def test_core_distance_one(tmp_path: pathlib.Path) -> None: |
| 167 | _init_repo(tmp_path) |
| 168 | c1 = _make_commit(tmp_path, content=b"c1") |
| 169 | _make_tag(tmp_path, "v0.9", c1) |
| 170 | c2 = _make_commit(tmp_path, parent_id=c1, content=b"c2") |
| 171 | r = describe_commit(tmp_path, _REPO_ID, c2) |
| 172 | assert r["tag"] == "v0.9" |
| 173 | assert r["distance"] == 1 |
| 174 | assert r["exact"] is False |
| 175 | assert r["name"] == "v0.9~1" |
| 176 | |
| 177 | |
| 178 | def test_core_long_format_on_tag(tmp_path: pathlib.Path) -> None: |
| 179 | _init_repo(tmp_path) |
| 180 | cid = _make_commit(tmp_path, content=b"long") |
| 181 | _make_tag(tmp_path, "v2.0.0", cid) |
| 182 | r = describe_commit(tmp_path, _REPO_ID, cid, long_format=True) |
| 183 | assert r["name"].startswith("v2.0.0-0-g") |
| 184 | |
| 185 | |
| 186 | def test_core_long_format_with_distance(tmp_path: pathlib.Path) -> None: |
| 187 | _init_repo(tmp_path) |
| 188 | c1 = _make_commit(tmp_path, content=b"root") |
| 189 | _make_tag(tmp_path, "v1.0", c1) |
| 190 | c2 = _make_commit(tmp_path, parent_id=c1, content=b"next") |
| 191 | r = describe_commit(tmp_path, _REPO_ID, c2, long_format=True) |
| 192 | assert "-1-g" in r["name"] |
| 193 | |
| 194 | |
| 195 | def test_core_abbrev_controls_sha_length(tmp_path: pathlib.Path) -> None: |
| 196 | _init_repo(tmp_path) |
| 197 | cid = _make_commit(tmp_path, content=b"abbrev") |
| 198 | r = describe_commit(tmp_path, _REPO_ID, cid, abbrev=8) |
| 199 | assert r["short_sha"] == cid[:8] |
| 200 | assert len(r["short_sha"]) == 8 |
| 201 | |
| 202 | |
| 203 | def test_core_match_pattern_filters_tags(tmp_path: pathlib.Path) -> None: |
| 204 | _init_repo(tmp_path) |
| 205 | cid = _make_commit(tmp_path, content=b"match") |
| 206 | _make_tag(tmp_path, "release-1", cid) |
| 207 | _make_tag(tmp_path, "v1.0.0", cid) |
| 208 | # Only semver tags — "release-1" excluded. |
| 209 | r = describe_commit(tmp_path, _REPO_ID, cid, match_pattern="v*") |
| 210 | assert r["tag"] == "v1.0.0" |
| 211 | |
| 212 | |
| 213 | def test_core_match_pattern_no_match_returns_sha(tmp_path: pathlib.Path) -> None: |
| 214 | _init_repo(tmp_path) |
| 215 | cid = _make_commit(tmp_path, content=b"nomatch") |
| 216 | _make_tag(tmp_path, "nightly-123", cid) |
| 217 | r = describe_commit(tmp_path, _REPO_ID, cid, match_pattern="v*") |
| 218 | assert r["tag"] is None |
| 219 | assert r["name"] == cid[:12] |
| 220 | |
| 221 | |
| 222 | def test_core_exact_match_on_tag(tmp_path: pathlib.Path) -> None: |
| 223 | _init_repo(tmp_path) |
| 224 | cid = _make_commit(tmp_path, content=b"exact") |
| 225 | _make_tag(tmp_path, "v3.0", cid) |
| 226 | r = describe_commit(tmp_path, _REPO_ID, cid, exact_match=True) |
| 227 | assert r["tag"] == "v3.0" |
| 228 | assert r["exact"] is True |
| 229 | |
| 230 | |
| 231 | def test_core_exact_match_off_tag_returns_sha(tmp_path: pathlib.Path) -> None: |
| 232 | _init_repo(tmp_path) |
| 233 | c1 = _make_commit(tmp_path, content=b"root") |
| 234 | _make_tag(tmp_path, "v3.0", c1) |
| 235 | c2 = _make_commit(tmp_path, parent_id=c1, content=b"after") |
| 236 | r = describe_commit(tmp_path, _REPO_ID, c2, exact_match=True) |
| 237 | assert r["tag"] is None |
| 238 | assert r["name"] == c2[:12] |
| 239 | |
| 240 | |
| 241 | def test_core_first_parent_skips_merge_branch(tmp_path: pathlib.Path) -> None: |
| 242 | """With --first-parent the tag on a merged branch is invisible.""" |
| 243 | _init_repo(tmp_path) |
| 244 | # main: c1 → c3 (merge of feat) |
| 245 | # feat: c1 → c2 (tag here) |
| 246 | c1 = _make_commit(tmp_path, content=b"root") |
| 247 | c2 = _make_commit(tmp_path, parent_id=c1, content=b"feat", branch="feat") |
| 248 | _make_tag(tmp_path, "feat-tag", c2) |
| 249 | c3 = _make_commit( |
| 250 | tmp_path, parent_id=c1, parent2_id=c2, content=b"merge", branch="main" |
| 251 | ) |
| 252 | # Without first_parent: feat-tag is reachable via second parent. |
| 253 | r_full = describe_commit(tmp_path, _REPO_ID, c3) |
| 254 | # With first_parent: only first parent chain; feat-tag not reachable. |
| 255 | r_fp = describe_commit(tmp_path, _REPO_ID, c3, first_parent=True) |
| 256 | assert r_fp["tag"] is None |
| 257 | # Full walk should find the tag (via c2). |
| 258 | assert r_full["tag"] == "feat-tag" |
| 259 | |
| 260 | |
| 261 | def test_core_multi_tag_same_commit_lex_greatest(tmp_path: pathlib.Path) -> None: |
| 262 | """When multiple tags point at the same commit, greatest lex name wins.""" |
| 263 | _init_repo(tmp_path) |
| 264 | cid = _make_commit(tmp_path, content=b"multi") |
| 265 | _make_tag(tmp_path, "v1.0.0", cid) |
| 266 | _make_tag(tmp_path, "v2.0.0", cid) |
| 267 | _make_tag(tmp_path, "v1.5.0", cid) |
| 268 | r = describe_commit(tmp_path, _REPO_ID, cid) |
| 269 | assert r["tag"] == "v2.0.0" |
| 270 | |
| 271 | |
| 272 | def test_core_max_walk_budget(tmp_path: pathlib.Path) -> None: |
| 273 | """Walk stops at _MAX_WALK without crashing; returns short-SHA fallback.""" |
| 274 | _init_repo(tmp_path) |
| 275 | # Inject a fake read_commit that always returns a parent so BFS never |
| 276 | # finds a commit-store miss — we just want to trigger the budget guard. |
| 277 | cid = _make_commit(tmp_path, content=b"budget") |
| 278 | _make_tag(tmp_path, "very-far", cid) |
| 279 | |
| 280 | call_count = 0 |
| 281 | |
| 282 | import muse.core.store as _store |
| 283 | from muse.core.store import CommitRecord as _CR |
| 284 | import datetime as _dt |
| 285 | |
| 286 | orig = _store.read_commit |
| 287 | |
| 288 | def _fake_read(root: pathlib.Path, cid: str) -> _CR | None: |
| 289 | nonlocal call_count |
| 290 | call_count += 1 |
| 291 | if call_count > _MAX_WALK + 5: |
| 292 | return None |
| 293 | fake_parent = hashlib.sha256(cid.encode()).hexdigest() |
| 294 | return _CR( |
| 295 | commit_id=cid, |
| 296 | repo_id=_REPO_ID, |
| 297 | branch="main", |
| 298 | snapshot_id="snap", |
| 299 | message="x", |
| 300 | committed_at=_dt.datetime.now(_dt.timezone.utc), |
| 301 | parent_commit_id=fake_parent, |
| 302 | ) |
| 303 | |
| 304 | with patch.object(_store, "read_commit", side_effect=_fake_read): |
| 305 | # Start from a commit far from any tag — BFS will exhaust budget. |
| 306 | far_commit = hashlib.sha256(b"far").hexdigest() |
| 307 | r = describe_commit(tmp_path, _REPO_ID, far_commit) |
| 308 | |
| 309 | # Budget exhausted → tag not found → name is short SHA. |
| 310 | assert r["tag"] is None |
| 311 | |
| 312 | |
| 313 | # --------------------------------------------------------------------------- |
| 314 | # Security: ANSI injection in tag names |
| 315 | # --------------------------------------------------------------------------- |
| 316 | |
| 317 | |
| 318 | def test_ansi_in_tag_name_stripped_in_text_output(tmp_path: pathlib.Path) -> None: |
| 319 | _init_repo(tmp_path) |
| 320 | cid = _make_commit(tmp_path, content=b"ansi") |
| 321 | evil_tag = "v1.0\x1b[31mRED\x1b[0m" |
| 322 | _make_tag(tmp_path, evil_tag, cid) |
| 323 | result = _invoke(["describe"], _env(tmp_path)) |
| 324 | assert result.exit_code == 0 |
| 325 | assert "\x1b[31m" not in result.output |
| 326 | |
| 327 | |
| 328 | def test_ansi_in_tag_name_preserved_in_json(tmp_path: pathlib.Path) -> None: |
| 329 | """JSON output must not sanitize so callers see the raw value.""" |
| 330 | _init_repo(tmp_path) |
| 331 | cid = _make_commit(tmp_path, content=b"ansi-json") |
| 332 | evil_tag = "v1.0\x1b[31mRED\x1b[0m" |
| 333 | _make_tag(tmp_path, evil_tag, cid) |
| 334 | result = _invoke(["describe", "--json"], _env(tmp_path)) |
| 335 | assert result.exit_code == 0 |
| 336 | data = _parse_json(result) |
| 337 | assert data["tag"] == evil_tag |
| 338 | |
| 339 | |
| 340 | # --------------------------------------------------------------------------- |
| 341 | # Error routing: all user errors go to stderr |
| 342 | # --------------------------------------------------------------------------- |
| 343 | |
| 344 | |
| 345 | def test_no_commits_error_on_stderr(tmp_path: pathlib.Path) -> None: |
| 346 | _init_repo(tmp_path) |
| 347 | result = _invoke(["describe"], _env(tmp_path)) |
| 348 | assert result.exit_code != 0 |
| 349 | assert result.stderr != "" or "commits" in result.output.lower() |
| 350 | |
| 351 | |
| 352 | def test_ref_not_found_error_on_stderr(tmp_path: pathlib.Path) -> None: |
| 353 | _init_repo(tmp_path) |
| 354 | _make_commit(tmp_path, content=b"x") |
| 355 | result = _invoke(["describe", "--ref", "nonexistent"], _env(tmp_path)) |
| 356 | assert result.exit_code != 0 |
| 357 | |
| 358 | |
| 359 | def test_require_tag_no_tags_error(tmp_path: pathlib.Path) -> None: |
| 360 | _init_repo(tmp_path) |
| 361 | _make_commit(tmp_path, content=b"no-tag") |
| 362 | result = _invoke(["describe", "--require-tag"], _env(tmp_path)) |
| 363 | assert result.exit_code != 0 |
| 364 | |
| 365 | |
| 366 | def test_exact_match_not_on_tag_error(tmp_path: pathlib.Path) -> None: |
| 367 | _init_repo(tmp_path) |
| 368 | c1 = _make_commit(tmp_path, content=b"c1") |
| 369 | _make_tag(tmp_path, "v1", c1) |
| 370 | _make_commit(tmp_path, parent_id=c1, content=b"c2") |
| 371 | result = _invoke(["describe", "--exact-match"], _env(tmp_path)) |
| 372 | assert result.exit_code != 0 |
| 373 | |
| 374 | |
| 375 | def test_abbrev_too_small_error(tmp_path: pathlib.Path) -> None: |
| 376 | _init_repo(tmp_path) |
| 377 | _make_commit(tmp_path, content=b"ab") |
| 378 | result = _invoke(["describe", "--abbrev", "2"], _env(tmp_path)) |
| 379 | assert result.exit_code != 0 |
| 380 | |
| 381 | |
| 382 | def test_abbrev_too_large_error(tmp_path: pathlib.Path) -> None: |
| 383 | _init_repo(tmp_path) |
| 384 | _make_commit(tmp_path, content=b"ab") |
| 385 | result = _invoke(["describe", "--abbrev", "65"], _env(tmp_path)) |
| 386 | assert result.exit_code != 0 |
| 387 | |
| 388 | |
| 389 | # --------------------------------------------------------------------------- |
| 390 | # JSON schema: _DescribeJson |
| 391 | # --------------------------------------------------------------------------- |
| 392 | |
| 393 | |
| 394 | def test_json_schema_all_fields(tmp_path: pathlib.Path) -> None: |
| 395 | _init_repo(tmp_path) |
| 396 | cid = _make_commit(tmp_path, content=b"schema") |
| 397 | _make_tag(tmp_path, "v1.0.0", cid) |
| 398 | result = _invoke(["describe", "--json"], _env(tmp_path)) |
| 399 | assert result.exit_code == 0 |
| 400 | data = _parse_json(result) |
| 401 | assert data["tag"] == "v1.0.0" |
| 402 | assert data["distance"] == 0 |
| 403 | assert data["exact"] is True |
| 404 | assert data["repo_id"] == _REPO_ID |
| 405 | assert data["branch"] == "main" |
| 406 | assert data["commit_id"] == cid |
| 407 | assert data["short_sha"] == cid[:12] |
| 408 | |
| 409 | |
| 410 | def test_json_schema_no_tag(tmp_path: pathlib.Path) -> None: |
| 411 | _init_repo(tmp_path) |
| 412 | cid = _make_commit(tmp_path, content=b"no-tag-json") |
| 413 | result = _invoke(["describe", "--json"], _env(tmp_path)) |
| 414 | assert result.exit_code == 0 |
| 415 | data = _parse_json(result) |
| 416 | assert data["tag"] is None |
| 417 | assert data["name"] == cid[:12] |
| 418 | assert data["exact"] is False |
| 419 | |
| 420 | |
| 421 | def test_json_schema_with_distance(tmp_path: pathlib.Path) -> None: |
| 422 | _init_repo(tmp_path) |
| 423 | c1 = _make_commit(tmp_path, content=b"root") |
| 424 | _make_tag(tmp_path, "v0.1", c1) |
| 425 | _make_commit(tmp_path, parent_id=c1, content=b"next") |
| 426 | result = _invoke(["describe", "--json"], _env(tmp_path)) |
| 427 | assert result.exit_code == 0 |
| 428 | data = _parse_json(result) |
| 429 | assert data["tag"] == "v0.1" |
| 430 | assert data["distance"] == 1 |
| 431 | assert data["exact"] is False |
| 432 | |
| 433 | |
| 434 | def test_json_abbrev_reflected(tmp_path: pathlib.Path) -> None: |
| 435 | _init_repo(tmp_path) |
| 436 | cid = _make_commit(tmp_path, content=b"abbrev-json") |
| 437 | result = _invoke(["describe", "--abbrev", "8", "--json"], _env(tmp_path)) |
| 438 | assert result.exit_code == 0 |
| 439 | data = _parse_json(result) |
| 440 | assert len(data["short_sha"]) == 8 |
| 441 | |
| 442 | |
| 443 | # --------------------------------------------------------------------------- |
| 444 | # New flags: --match, --exact-match, --first-parent, --abbrev |
| 445 | # --------------------------------------------------------------------------- |
| 446 | |
| 447 | |
| 448 | def test_flag_match_filters_tags(tmp_path: pathlib.Path) -> None: |
| 449 | _init_repo(tmp_path) |
| 450 | cid = _make_commit(tmp_path, content=b"match-flag") |
| 451 | _make_tag(tmp_path, "nightly-1", cid) |
| 452 | _make_tag(tmp_path, "v1.0.0", cid) |
| 453 | result = _invoke(["describe", "--match", "v*", "--json"], _env(tmp_path)) |
| 454 | assert result.exit_code == 0 |
| 455 | data = _parse_json(result) |
| 456 | assert data["tag"] == "v1.0.0" |
| 457 | |
| 458 | |
| 459 | def test_flag_match_no_matching_tag(tmp_path: pathlib.Path) -> None: |
| 460 | _init_repo(tmp_path) |
| 461 | cid = _make_commit(tmp_path, content=b"match-none") |
| 462 | _make_tag(tmp_path, "nightly-1", cid) |
| 463 | result = _invoke(["describe", "--match", "v*", "--json"], _env(tmp_path)) |
| 464 | assert result.exit_code == 0 |
| 465 | data = _parse_json(result) |
| 466 | assert data["tag"] is None |
| 467 | |
| 468 | |
| 469 | def test_flag_exact_match_on_tag(tmp_path: pathlib.Path) -> None: |
| 470 | _init_repo(tmp_path) |
| 471 | cid = _make_commit(tmp_path, content=b"exact-flag") |
| 472 | _make_tag(tmp_path, "v1.0", cid) |
| 473 | result = _invoke(["describe", "--exact-match", "--json"], _env(tmp_path)) |
| 474 | assert result.exit_code == 0 |
| 475 | data = _parse_json(result) |
| 476 | assert data["exact"] is True |
| 477 | |
| 478 | |
| 479 | def test_flag_exact_match_off_tag_fails(tmp_path: pathlib.Path) -> None: |
| 480 | _init_repo(tmp_path) |
| 481 | c1 = _make_commit(tmp_path, content=b"em-root") |
| 482 | _make_tag(tmp_path, "v1.0", c1) |
| 483 | _make_commit(tmp_path, parent_id=c1, content=b"em-next") |
| 484 | result = _invoke(["describe", "--exact-match"], _env(tmp_path)) |
| 485 | assert result.exit_code != 0 |
| 486 | |
| 487 | |
| 488 | def test_flag_first_parent(tmp_path: pathlib.Path) -> None: |
| 489 | _init_repo(tmp_path) |
| 490 | c1 = _make_commit(tmp_path, content=b"fp-root") |
| 491 | c2 = _make_commit(tmp_path, parent_id=c1, content=b"feat-side", branch="feat") |
| 492 | _make_tag(tmp_path, "side-tag", c2) |
| 493 | c3 = _make_commit( |
| 494 | tmp_path, parent_id=c1, parent2_id=c2, content=b"fp-merge", branch="main" |
| 495 | ) |
| 496 | # --first-parent should not see side-tag. |
| 497 | result = _invoke(["describe", "--first-parent", "--json"], _env(tmp_path)) |
| 498 | assert result.exit_code == 0 |
| 499 | data = _parse_json(result) |
| 500 | assert data["tag"] is None # side-tag not reachable via first-parent |
| 501 | |
| 502 | |
| 503 | def test_flag_abbrev(tmp_path: pathlib.Path) -> None: |
| 504 | _init_repo(tmp_path) |
| 505 | _make_commit(tmp_path, content=b"abbrev-flag") |
| 506 | result = _invoke(["describe", "--abbrev", "16", "--json"], _env(tmp_path)) |
| 507 | assert result.exit_code == 0 |
| 508 | data = _parse_json(result) |
| 509 | assert len(data["short_sha"]) == 16 |
| 510 | |
| 511 | |
| 512 | # --------------------------------------------------------------------------- |
| 513 | # Integration |
| 514 | # --------------------------------------------------------------------------- |
| 515 | |
| 516 | |
| 517 | def test_integration_ref_to_branch_tip(tmp_path: pathlib.Path) -> None: |
| 518 | _init_repo(tmp_path) |
| 519 | c1 = _make_commit(tmp_path, content=b"ref-root") |
| 520 | _make_tag(tmp_path, "v10.0", c1) |
| 521 | _make_commit(tmp_path, parent_id=c1, content=b"ref-next") |
| 522 | # Describe the HEAD (which is 1 hop past the tag). |
| 523 | result = _invoke(["describe", "--json"], _env(tmp_path)) |
| 524 | assert result.exit_code == 0 |
| 525 | data = _parse_json(result) |
| 526 | assert data["distance"] == 1 |
| 527 | assert data["tag"] == "v10.0" |
| 528 | |
| 529 | |
| 530 | def test_integration_long_and_match_combined(tmp_path: pathlib.Path) -> None: |
| 531 | _init_repo(tmp_path) |
| 532 | cid = _make_commit(tmp_path, content=b"combo") |
| 533 | _make_tag(tmp_path, "v5.0.0", cid) |
| 534 | result = _invoke( |
| 535 | ["describe", "--long", "--match", "v*", "--json"], _env(tmp_path) |
| 536 | ) |
| 537 | assert result.exit_code == 0 |
| 538 | data = _parse_json(result) |
| 539 | assert data["name"].startswith("v5.0.0-0-g") |
| 540 | |
| 541 | |
| 542 | def test_integration_require_tag_passes_when_tag_exists( |
| 543 | tmp_path: pathlib.Path, |
| 544 | ) -> None: |
| 545 | _init_repo(tmp_path) |
| 546 | cid = _make_commit(tmp_path, content=b"req-tag") |
| 547 | _make_tag(tmp_path, "v7.0", cid) |
| 548 | result = _invoke(["describe", "--require-tag", "--json"], _env(tmp_path)) |
| 549 | assert result.exit_code == 0 |
| 550 | |
| 551 | |
| 552 | def test_integration_text_output_sanitized(tmp_path: pathlib.Path) -> None: |
| 553 | _init_repo(tmp_path) |
| 554 | cid = _make_commit(tmp_path, content=b"text-sanitize") |
| 555 | _make_tag(tmp_path, "v1.0\x1b[1mBOLD\x1b[0m", cid) |
| 556 | result = _invoke(["describe"], _env(tmp_path)) |
| 557 | assert result.exit_code == 0 |
| 558 | assert "\x1b[1m" not in result.output |
| 559 | |
| 560 | |
| 561 | # --------------------------------------------------------------------------- |
| 562 | # E2E: help output |
| 563 | # --------------------------------------------------------------------------- |
| 564 | |
| 565 | |
| 566 | def test_help_contains_new_flags() -> None: |
| 567 | result = _invoke(["describe", "--help"], {}) |
| 568 | assert result.exit_code == 0 |
| 569 | for flag in ("--match", "--exact-match", "--first-parent", "--abbrev", "--json"): |
| 570 | assert flag in result.output, f"Missing flag in help: {flag}" |
| 571 | |
| 572 | |
| 573 | def test_help_mentions_json_schema() -> None: |
| 574 | result = _invoke(["describe", "--help"], {}) |
| 575 | assert "json" in result.output.lower() |
| 576 | |
| 577 | |
| 578 | # --------------------------------------------------------------------------- |
| 579 | # Stress: deep ancestry + many tags + concurrent reads |
| 580 | # --------------------------------------------------------------------------- |
| 581 | |
| 582 | |
| 583 | def test_stress_5000_commit_chain(tmp_path: pathlib.Path) -> None: |
| 584 | _init_repo(tmp_path) |
| 585 | prev: str | None = None |
| 586 | root_cid = "" |
| 587 | for i in range(5_000): |
| 588 | cid = _make_commit(tmp_path, parent_id=prev, content=f"s{i}".encode()) |
| 589 | if i == 0: |
| 590 | root_cid = cid |
| 591 | prev = cid |
| 592 | |
| 593 | _make_tag(tmp_path, "v-deep", root_cid) |
| 594 | assert prev is not None |
| 595 | r = describe_commit(tmp_path, _REPO_ID, prev) |
| 596 | assert r["tag"] == "v-deep" |
| 597 | assert r["distance"] == 4_999 |
| 598 | |
| 599 | |
| 600 | def test_stress_200_tags_repo(tmp_path: pathlib.Path) -> None: |
| 601 | """Many tags — describe still picks the nearest one efficiently.""" |
| 602 | _init_repo(tmp_path) |
| 603 | commits: list[str] = [] |
| 604 | prev: str | None = None |
| 605 | for i in range(200): |
| 606 | cid = _make_commit(tmp_path, parent_id=prev, content=f"t{i}".encode()) |
| 607 | commits.append(cid) |
| 608 | # Tag every 10th commit. |
| 609 | if i % 10 == 0: |
| 610 | _make_tag(tmp_path, f"v{i}.0", cid) |
| 611 | prev = cid |
| 612 | |
| 613 | # HEAD is commits[-1], nearest tag is v190.0 (at commits[190]). |
| 614 | r = describe_commit(tmp_path, _REPO_ID, commits[-1]) |
| 615 | assert r["tag"] == "v190.0" |
| 616 | assert r["distance"] == 9 |
| 617 | |
| 618 | |
| 619 | def test_stress_concurrent_describe(tmp_path: pathlib.Path) -> None: |
| 620 | """Concurrent --json calls must all return consistent, valid JSON.""" |
| 621 | _init_repo(tmp_path) |
| 622 | c1 = _make_commit(tmp_path, content=b"conc-root") |
| 623 | _make_tag(tmp_path, "v-conc", c1) |
| 624 | _make_commit(tmp_path, parent_id=c1, content=b"conc-next") |
| 625 | |
| 626 | invoke_lock = threading.Lock() |
| 627 | errors: list[str] = [] |
| 628 | |
| 629 | def _worker() -> None: |
| 630 | with invoke_lock: |
| 631 | r = _invoke(["describe", "--json"], _env(tmp_path)) |
| 632 | try: |
| 633 | assert r.exit_code == 0 |
| 634 | data = _parse_json(r) |
| 635 | assert data["tag"] == "v-conc" |
| 636 | assert data["distance"] == 1 |
| 637 | except Exception as exc: |
| 638 | errors.append(str(exc)) |
| 639 | |
| 640 | threads = [threading.Thread(target=_worker) for _ in range(8)] |
| 641 | for t in threads: |
| 642 | t.start() |
| 643 | for t in threads: |
| 644 | t.join() |
| 645 | |
| 646 | assert errors == [], f"Concurrent failures: {errors}" |
File History
5 commits
sha256:660fcac1df3ab28f61862e961890bd2ca8b754fa0242079d93ca1e25037ec8a6
chore(tests): add docstring to tests/__init__.py so rc14 tr…
Human
26 days ago
sha256:d8316ffae901be06347e16ab55be11868eb519dd16ade3e8aa16a99e662f7e62
baseline: rc14 re-baseline after rc3 store corruption recovery
Human
patch
26 days ago
sha256:50d413a635f57761c3fce1f70251b957b6423821525bf330747ef4007339222e
Merge branch 'dev' into main
Human
29 days ago
sha256:fb67fed5a4d3e40de84bdd163de94ef1386570bef1dd1a020a732c8a038962ce
Merge branch 'dev' into main
Human
48 days ago
sha256:1c4b3e3a9a1f300774c3ee662b572a698d5fd405bf765a71e6011a2e9c3eaaaa
feat: Muse — version control for the agent era
Human
100 days ago