test_revert_symlog.py
python
sha256:7cc8abb049561fa05083f7bdf6b66bd5cf0466506a38600bc92ac3e6f4cf7b3a
fix: muse mist push crashed unconditionally with AttributeE…
Sonnet 5
patch
1 hour ago
| 1 | """muse#53 item 11 -- symlog entries for `muse revert`. |
| 2 | |
| 3 | `muse revert` records a reflog entry for the branch ref move, but |
| 4 | previously wrote no symlog entries for the symbols it touched. A revert |
| 5 | of a commit that modified a symbol should produce a symbol-modified |
| 6 | entry in that symbol's journal, just like a normal `muse commit` would. |
| 7 | """ |
| 8 | |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import json |
| 12 | import pathlib |
| 13 | from collections.abc import Mapping |
| 14 | |
| 15 | import pytest |
| 16 | |
| 17 | from tests.cli_test_helper import CliRunner |
| 18 | |
| 19 | cli = None |
| 20 | runner = CliRunner() |
| 21 | |
| 22 | |
| 23 | def _env(root: pathlib.Path) -> Mapping[str, str]: |
| 24 | return {"MUSE_REPO_ROOT": str(root)} |
| 25 | |
| 26 | |
| 27 | @pytest.fixture() |
| 28 | def repo(tmp_path: pathlib.Path, monkeypatch: pytest.MonkeyPatch) -> pathlib.Path: |
| 29 | monkeypatch.chdir(tmp_path) |
| 30 | monkeypatch.setenv("MUSE_REPO_ROOT", str(tmp_path)) |
| 31 | r = runner.invoke(cli, ["init"], env=_env(tmp_path), catch_exceptions=False) |
| 32 | assert r.exit_code == 0, r.output |
| 33 | return tmp_path |
| 34 | |
| 35 | |
| 36 | def _write(repo: pathlib.Path, rel: str, content: str) -> None: |
| 37 | p = repo / rel |
| 38 | p.parent.mkdir(parents=True, exist_ok=True) |
| 39 | p.write_text(content) |
| 40 | |
| 41 | |
| 42 | def _commit(repo: pathlib.Path, msg: str) -> str: |
| 43 | runner.invoke(cli, ["code", "add", "."], env=_env(repo), catch_exceptions=False) |
| 44 | result = runner.invoke(cli, ["commit", "-m", msg, "--json"], env=_env(repo), catch_exceptions=False) |
| 45 | assert result.exit_code == 0, result.output |
| 46 | return json.loads(result.output)["commit_id"] |
| 47 | |
| 48 | |
| 49 | def _symlog(repo: pathlib.Path, *args: str): |
| 50 | return runner.invoke(cli, ["symlog", *args], env=_env(repo), catch_exceptions=False) |
| 51 | |
| 52 | |
| 53 | class TestRevertWritesSymlog: |
| 54 | def test_revert_of_symbol_modifying_commit_writes_symlog_entry(self, repo: pathlib.Path) -> None: |
| 55 | _write(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 56 | _commit(repo, "add compute_total") |
| 57 | _write(repo, "src/billing.py", "def compute_total(items):\n return sum(items) * 2\n") |
| 58 | doubled_commit = _commit(repo, "double the total") |
| 59 | |
| 60 | # Before revert: 2 symlog entries (created + modified). |
| 61 | before = json.loads(_symlog(repo, "src/billing.py::compute_total", "--json").output) |
| 62 | assert before["total"] == 2 |
| 63 | |
| 64 | # Revert the "double the total" commit, restoring the prior body. |
| 65 | result = runner.invoke( |
| 66 | cli, ["revert", doubled_commit, "--json"], env=_env(repo), catch_exceptions=False, |
| 67 | ) |
| 68 | assert result.exit_code == 0, result.output |
| 69 | |
| 70 | after = json.loads(_symlog(repo, "src/billing.py::compute_total", "--json").output) |
| 71 | assert after["total"] == 3 |
| 72 | assert after["entries"][0]["operation"].startswith("symbol-modified") |
| 73 | |
| 74 | def test_revert_no_commit_does_not_write_symlog(self, repo: pathlib.Path) -> None: |
| 75 | """--no-commit only stages changes -- no commit record exists yet, |
| 76 | so there's nothing for a symlog entry to attach to.""" |
| 77 | _write(repo, "src/billing.py", "def compute_total(items):\n return sum(items)\n") |
| 78 | _commit(repo, "add compute_total") |
| 79 | _write(repo, "src/billing.py", "def compute_total(items):\n return sum(items) * 2\n") |
| 80 | doubled_commit = _commit(repo, "double the total") |
| 81 | |
| 82 | before = json.loads(_symlog(repo, "src/billing.py::compute_total", "--json").output) |
| 83 | |
| 84 | result = runner.invoke( |
| 85 | cli, ["revert", doubled_commit, "--no-commit", "--json"], env=_env(repo), catch_exceptions=False, |
| 86 | ) |
| 87 | assert result.exit_code == 0, result.output |
| 88 | |
| 89 | after = json.loads(_symlog(repo, "src/billing.py::compute_total", "--json").output) |
| 90 | assert after["total"] == before["total"] |
File History
1 commit
sha256:7cc8abb049561fa05083f7bdf6b66bd5cf0466506a38600bc92ac3e6f4cf7b3a
fix: muse mist push crashed unconditionally with AttributeE…
Sonnet 5
patch
1 hour ago