Skip to content

Commit e947ced

Browse files
fix(cli): apply exit codes on --since last early returns
Call _exit_bulk_export(export_result) when cmd_export returns early for latest_day is None or zero overlap, matching the empty-export path so recorded failures map to exit 1/2 instead of always exiting 0.
1 parent 3ab5ac1 commit e947ced

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

scripts/export.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ def _on_export_error(sid: str, exc: Exception) -> None:
479479
if since == "last":
480480
if latest_day is None:
481481
print("Nothing to export (no qualifying sessions in scope).")
482+
_exit_bulk_export(export_result)
482483
return
483484
print(
484485
f"Latest activity end-date (UTC): {latest_day.isoformat()} — "
@@ -489,6 +490,7 @@ def _on_export_error(sid: str, exc: Exception) -> None:
489490
f"No sessions overlap {latest_day.isoformat()} (UTC); "
490491
"nothing to export."
491492
)
493+
_exit_bulk_export(export_result)
492494
return
493495
elif since == "incremental":
494496
skipped_mtime_unchanged = export_result.skipped_mtime_unchanged_count

tests/test_cli_export_exit_codes.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import scripts.export as export # noqa: E402
1616
from tests.test_cli_e2e import _run_cli, _seed_base_dir # noqa: E402
17+
from utils.export_engine import BulkExportResult # noqa: E402
1718
from utils.jsonl_parser import parse_session # noqa: E402
1819

1920
_SUMMARY_RE = re.compile(
@@ -87,6 +88,51 @@ def _parse(path: str):
8788
assert len(list(out_dir.rglob("*.md"))) == 1
8889

8990

91+
def test_since_last_early_return_invokes_exit_bulk_export(
92+
tmp_path, monkeypatch, capsys
93+
):
94+
"""cmd_export --since last must call _exit_bulk_export on early-return paths."""
95+
exit_calls: list[BulkExportResult] = []
96+
97+
def _track_exit(result: BulkExportResult) -> None:
98+
exit_calls.append(result)
99+
if result.failure_count > 0:
100+
raise SystemExit(1)
101+
102+
fake_result = BulkExportResult(
103+
total_candidates=3,
104+
failure_count=1,
105+
latest_day=None,
106+
)
107+
108+
monkeypatch.setattr(export, "_exit_bulk_export", _track_exit)
109+
monkeypatch.setattr(
110+
export,
111+
"run_bulk_export",
112+
lambda **kwargs: fake_result,
113+
)
114+
monkeypatch.setattr(export, "list_projects", lambda base: [{"name": "p", "path": "/p"}])
115+
116+
args = types.SimpleNamespace(
117+
base_dir=str(tmp_path),
118+
out=str(tmp_path / "out"),
119+
since="last",
120+
no_zip=True,
121+
project=None,
122+
format="md",
123+
session=None,
124+
exclude_rules=None,
125+
)
126+
127+
with pytest.raises(SystemExit) as exc_info:
128+
export.cmd_export(args)
129+
130+
assert exc_info.value.code == 1
131+
assert len(exit_calls) == 1
132+
assert exit_calls[0] is fake_result
133+
assert "no qualifying sessions" in capsys.readouterr().out.lower()
134+
135+
90136
def test_cli_export_total_failure_exits_one(tmp_path, monkeypatch, capsys):
91137
project_dir = tmp_path / "test-project"
92138
project_dir.mkdir(parents=True)

0 commit comments

Comments
 (0)