test_reflog_format.py
python
sha256:5e26071aa96fb5024f64ff92210d420ce11305b44d09ed92207df404c6c6a44e
feat: muse#54 item 2 — muse reflog --format oneline/patch
Sonnet 5
patch
12 hours ago
| 1 | """muse#54 item 2 -- `muse reflog --format oneline` / `--format patch`. |
| 2 | |
| 3 | Machine-parseable one-line output, and a patch format that follows each |
| 4 | entry's oneline header with the diff it introduced. |
| 5 | """ |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import json |
| 9 | import pathlib |
| 10 | |
| 11 | from tests.cli_test_helper import CliRunner |
| 12 | from muse.core.types import fake_id, Manifest |
| 13 | from muse.core.paths import muse_dir |
| 14 | |
| 15 | cli = None |
| 16 | runner = CliRunner() |
| 17 | |
| 18 | |
| 19 | def _env(root: pathlib.Path) -> Manifest: |
| 20 | return {"MUSE_REPO_ROOT": str(root)} |
| 21 | |
| 22 | |
| 23 | def _run(root: pathlib.Path, *args: str): |
| 24 | return runner.invoke(cli, list(args), env=_env(root)) |
| 25 | |
| 26 | |
| 27 | def _init_repo(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: |
| 28 | dot_muse = muse_dir(tmp_path) |
| 29 | dot_muse.mkdir() |
| 30 | repo_id = fake_id("repo") |
| 31 | (dot_muse / "repo.json").write_text(json.dumps({ |
| 32 | "repo_id": repo_id, "domain": "code", "default_branch": "main", |
| 33 | "created_at": "2025-01-01T00:00:00+00:00", |
| 34 | }), encoding="utf-8") |
| 35 | (dot_muse / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8") |
| 36 | (dot_muse / "refs" / "heads").mkdir(parents=True) |
| 37 | (dot_muse / "snapshots").mkdir() |
| 38 | (dot_muse / "commits").mkdir() |
| 39 | (dot_muse / "objects").mkdir() |
| 40 | return tmp_path, repo_id |
| 41 | |
| 42 | |
| 43 | def _commit_file(root: pathlib.Path, content: str, message: str) -> str: |
| 44 | (root / "f.txt").write_text(content) |
| 45 | add = _run(root, "code", "add", "f.txt") |
| 46 | assert add.exit_code == 0, add.output |
| 47 | commit = _run(root, "commit", "-m", message, "--json") |
| 48 | assert commit.exit_code == 0, commit.output |
| 49 | return json.loads(commit.output)["commit_id"] |
| 50 | |
| 51 | |
| 52 | class TestReflogFormatOneline: |
| 53 | def test_oneline_one_row_per_entry(self, tmp_path: pathlib.Path) -> None: |
| 54 | root, repo_id = _init_repo(tmp_path) |
| 55 | c1 = _commit_file(root, "v1\n", "c1") |
| 56 | c2 = _commit_file(root, "v2\n", "c2") |
| 57 | |
| 58 | result = _run(root, "reflog", "--format", "oneline") |
| 59 | assert result.exit_code == 0, result.output |
| 60 | lines = [l for l in result.output.splitlines() if l.strip()] |
| 61 | assert len(lines) == 2 |
| 62 | assert lines[0].startswith("@{0} " + c2) |
| 63 | assert "c2" in lines[0] |
| 64 | assert lines[1].startswith("@{1} " + c1) |
| 65 | |
| 66 | def test_oneline_mutually_exclusive_with_json(self, tmp_path: pathlib.Path) -> None: |
| 67 | root, repo_id = _init_repo(tmp_path) |
| 68 | _commit_file(root, "v1\n", "c1") |
| 69 | |
| 70 | result = _run(root, "reflog", "--format", "oneline", "--json") |
| 71 | assert result.exit_code == 1 |
| 72 | |
| 73 | |
| 74 | class TestReflogFormatPatch: |
| 75 | def test_patch_shows_diff_after_header(self, tmp_path: pathlib.Path) -> None: |
| 76 | root, repo_id = _init_repo(tmp_path) |
| 77 | _commit_file(root, "v1\n", "c1") |
| 78 | c2 = _commit_file(root, "v2\n", "c2") |
| 79 | |
| 80 | result = _run(root, "reflog", "--format", "patch", "--limit", "1") |
| 81 | assert result.exit_code == 0, result.output |
| 82 | assert result.output.startswith("@{0} " + c2) |
| 83 | assert "f.txt" in result.output |
| 84 | |
| 85 | def test_patch_initial_entry_has_no_diff_crash(self, tmp_path: pathlib.Path) -> None: |
| 86 | root, repo_id = _init_repo(tmp_path) |
| 87 | _commit_file(root, "v1\n", "c1") |
| 88 | |
| 89 | result = _run(root, "reflog", "--format", "patch") |
| 90 | assert result.exit_code == 0, result.output |
| 91 | assert "initial entry" in result.output |
File History
1 commit
sha256:5e26071aa96fb5024f64ff92210d420ce11305b44d09ed92207df404c6c6a44e
feat: muse#54 item 2 — muse reflog --format oneline/patch
Sonnet 5
patch
12 hours ago