|
| 1 | +"""CLI export exit codes for bulk export (partial / total failure).""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import re |
| 6 | +import sys |
| 7 | +import types |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 13 | +sys.path.insert(0, str(REPO_ROOT)) |
| 14 | + |
| 15 | +import scripts.export as export # noqa: E402 |
| 16 | +from tests.test_cli_e2e import _run_cli, _seed_base_dir # noqa: E402 |
| 17 | +from utils.jsonl_parser import parse_session # noqa: E402 |
| 18 | + |
| 19 | +_SUMMARY_RE = re.compile( |
| 20 | + r"Exported \d+ of \d+ sessions \(\d+ failed\)", |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _export_args(tmp_path: Path, base: Path, out_dir: Path) -> types.SimpleNamespace: |
| 25 | + return types.SimpleNamespace( |
| 26 | + base_dir=str(base), |
| 27 | + out=str(out_dir), |
| 28 | + since="all", |
| 29 | + no_zip=True, |
| 30 | + project=None, |
| 31 | + format="md", |
| 32 | + session=None, |
| 33 | + exclude_rules=None, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def test_cli_export_clean_exits_zero(tmp_path): |
| 38 | + base = _seed_base_dir(tmp_path) |
| 39 | + out_dir = tmp_path / "out" |
| 40 | + proc = _run_cli([ |
| 41 | + "export", |
| 42 | + "--base-dir", |
| 43 | + str(base), |
| 44 | + "--since", |
| 45 | + "all", |
| 46 | + "--no-zip", |
| 47 | + "--out", |
| 48 | + str(out_dir), |
| 49 | + ]) |
| 50 | + assert proc.returncode == 0, proc.stderr |
| 51 | + assert list(out_dir.rglob("*.md")) |
| 52 | + if proc.stderr.strip(): |
| 53 | + assert "failed" not in proc.stderr.lower() or "0 failed" in proc.stderr |
| 54 | + |
| 55 | + |
| 56 | +def test_cli_export_partial_failure_exits_two( |
| 57 | + tmp_path, monkeypatch, capsys |
| 58 | +): |
| 59 | + """One session exports; a second fails parse (simulated corrupt file).""" |
| 60 | + base = _seed_base_dir(tmp_path) |
| 61 | + project_dir = next(base.iterdir()) |
| 62 | + bad = project_dir / "session_bad.jsonl" |
| 63 | + bad.write_text('{"type": "user"}\n', encoding="utf-8") |
| 64 | + out_dir = tmp_path / "out" |
| 65 | + |
| 66 | + state_dir = tmp_path / "state" |
| 67 | + state_dir.mkdir() |
| 68 | + monkeypatch.setattr(export, "STATE_FILE", str(state_dir / "export_state.json")) |
| 69 | + monkeypatch.setattr(export, "STATE_DIR", str(state_dir)) |
| 70 | + |
| 71 | + real_parse = parse_session |
| 72 | + |
| 73 | + def _parse(path: str): |
| 74 | + if bad.name in path.replace("\\", "/"): |
| 75 | + raise ValueError("simulated corrupt jsonl") |
| 76 | + return real_parse(path) |
| 77 | + |
| 78 | + monkeypatch.setattr("utils.export_engine.parse_session", _parse) |
| 79 | + |
| 80 | + with pytest.raises(SystemExit) as exc_info: |
| 81 | + export.cmd_export(_export_args(tmp_path, base, out_dir)) |
| 82 | + |
| 83 | + assert exc_info.value.code == 2 |
| 84 | + captured = capsys.readouterr() |
| 85 | + assert _SUMMARY_RE.search(captured.err), captured.err |
| 86 | + assert "Exported 1 of 2 sessions (1 failed)" in captured.err |
| 87 | + assert len(list(out_dir.rglob("*.md"))) == 1 |
| 88 | + |
| 89 | + |
| 90 | +def test_cli_export_total_failure_exits_one(tmp_path, monkeypatch, capsys): |
| 91 | + project_dir = tmp_path / "test-project" |
| 92 | + project_dir.mkdir(parents=True) |
| 93 | + (project_dir / "bad_a.jsonl").write_text("{}", encoding="utf-8") |
| 94 | + (project_dir / "bad_b.jsonl").write_text("{}", encoding="utf-8") |
| 95 | + out_dir = tmp_path / "out" |
| 96 | + |
| 97 | + state_dir = tmp_path / "state" |
| 98 | + state_dir.mkdir() |
| 99 | + monkeypatch.setattr(export, "STATE_FILE", str(state_dir / "export_state.json")) |
| 100 | + monkeypatch.setattr(export, "STATE_DIR", str(state_dir)) |
| 101 | + |
| 102 | + def _parse(_path: str): |
| 103 | + raise ValueError("simulated corrupt jsonl") |
| 104 | + |
| 105 | + monkeypatch.setattr("utils.export_engine.parse_session", _parse) |
| 106 | + |
| 107 | + with pytest.raises(SystemExit) as exc_info: |
| 108 | + export.cmd_export(_export_args(tmp_path, tmp_path, out_dir)) |
| 109 | + |
| 110 | + assert exc_info.value.code == 1 |
| 111 | + captured = capsys.readouterr() |
| 112 | + assert "Exported 0 of 2 sessions (2 failed)" in captured.err |
| 113 | + assert "Nothing to export." in captured.out |
| 114 | + assert list(out_dir.rglob("*.md")) == [] |
0 commit comments