-
Notifications
You must be signed in to change notification settings - Fork 1
feat(cli): non-zero exit codes on partial export failure #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ab5ac1
feat(cli): non-zero exit codes on partial export failure
clean6378-max-it e947ced
fix(cli): apply exit codes on --since last early returns
clean6378-max-it 719fb9a
fix(cli): skip stderr summary on incremental no-op exports
clean6378-max-it fa8564b
fix(cli): accurate attempt count, elif, stdout on success in exit sum…
clean6378-max-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
clean6378-max-it marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| """CLI export exit codes for bulk export (partial / total failure).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import re | ||
| import sys | ||
| import types | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parent.parent | ||
| sys.path.insert(0, str(REPO_ROOT)) | ||
|
|
||
| import scripts.export as export # noqa: E402 | ||
| from tests.test_cli_e2e import _run_cli, _seed_base_dir # noqa: E402 | ||
| from utils.export_engine import BulkExportResult # noqa: E402 | ||
| from utils.jsonl_parser import parse_session # noqa: E402 | ||
|
|
||
| _SUMMARY_RE = re.compile( | ||
| r"Exported \d+ of \d+ sessions \(\d+ failed\)", | ||
| ) | ||
|
|
||
|
|
||
| def _isolated_home_env(tmp_path: Path) -> dict[str, str]: | ||
| """Redirect ~/.claude-code-chat-browser export state for subprocess CLI runs.""" | ||
| home = str(tmp_path / "home") | ||
| return {"HOME": home, "USERPROFILE": home} | ||
|
|
||
|
|
||
| def _export_args(tmp_path: Path, base: Path, out_dir: Path) -> types.SimpleNamespace: | ||
| return types.SimpleNamespace( | ||
| base_dir=str(base), | ||
| out=str(out_dir), | ||
| since="all", | ||
| no_zip=True, | ||
| project=None, | ||
| format="md", | ||
| session=None, | ||
| exclude_rules=None, | ||
| ) | ||
|
|
||
|
|
||
| def test_cli_export_clean_exits_zero(tmp_path): | ||
| base = _seed_base_dir(tmp_path) | ||
| out_dir = tmp_path / "out" | ||
| proc = _run_cli([ | ||
| "export", | ||
| "--base-dir", | ||
| str(base), | ||
| "--since", | ||
| "all", | ||
| "--no-zip", | ||
| "--out", | ||
| str(out_dir), | ||
| ]) | ||
| assert proc.returncode == 0, proc.stderr | ||
| assert list(out_dir.rglob("*.md")) | ||
| # Success summary must go to stdout, not stderr | ||
| assert "Exported" not in proc.stderr | ||
| assert "Exported 1 of 1 sessions (0 failed)" in proc.stdout | ||
|
|
||
|
|
||
| def test_cli_export_partial_failure_exits_two( | ||
| tmp_path, monkeypatch, capsys | ||
| ): | ||
| """One session exports; a second fails parse (simulated corrupt file).""" | ||
| base = _seed_base_dir(tmp_path) | ||
| project_dir = base / "test-project" | ||
| bad = project_dir / "session_bad.jsonl" | ||
| bad.write_text('{"type": "user"}\n', encoding="utf-8") | ||
| out_dir = tmp_path / "out" | ||
|
|
||
| state_dir = tmp_path / "state" | ||
| state_dir.mkdir() | ||
| monkeypatch.setattr(export, "STATE_FILE", str(state_dir / "export_state.json")) | ||
| monkeypatch.setattr(export, "STATE_DIR", str(state_dir)) | ||
|
|
||
| real_parse = parse_session | ||
|
|
||
| def _parse(path: str): | ||
| if bad.name in path.replace("\\", "/"): | ||
| raise ValueError("simulated corrupt jsonl") | ||
| return real_parse(path) | ||
|
|
||
| monkeypatch.setattr("utils.export_engine.parse_session", _parse) | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| export.cmd_export(_export_args(tmp_path, base, out_dir)) | ||
|
|
||
| assert exc_info.value.code == 2 | ||
| captured = capsys.readouterr() | ||
| assert _SUMMARY_RE.search(captured.err), captured.err | ||
| assert "Exported 1 of 2 sessions (1 failed)" in captured.err | ||
| assert len(list(out_dir.rglob("*.md"))) == 1 | ||
|
|
||
|
|
||
| def test_since_last_early_return_invokes_exit_bulk_export( | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
| tmp_path, monkeypatch, capsys | ||
| ): | ||
| """cmd_export --since last must call _exit_bulk_export on early-return paths.""" | ||
| exit_calls: list[BulkExportResult] = [] | ||
|
|
||
| def _track_exit(result: BulkExportResult) -> None: | ||
| exit_calls.append(result) | ||
|
|
||
| fake_result = BulkExportResult(latest_day=None) | ||
|
|
||
| monkeypatch.setattr(export, "_exit_bulk_export", _track_exit) | ||
| monkeypatch.setattr( | ||
| export, | ||
| "run_bulk_export", | ||
| lambda **kwargs: fake_result, | ||
| ) | ||
| monkeypatch.setattr(export, "list_projects", lambda base: [{"name": "p", "path": "/p"}]) | ||
|
|
||
| args = types.SimpleNamespace( | ||
| base_dir=str(tmp_path), | ||
| out=str(tmp_path / "out"), | ||
| since="last", | ||
| no_zip=True, | ||
| project=None, | ||
| format="md", | ||
| session=None, | ||
| exclude_rules=None, | ||
| ) | ||
|
|
||
| export.cmd_export(args) | ||
|
|
||
| assert len(exit_calls) == 1 | ||
| assert exit_calls[0] is fake_result | ||
| captured = capsys.readouterr() | ||
| assert "no qualifying sessions" in captured.out.lower() | ||
| assert "Exported" not in captured.err | ||
|
|
||
|
|
||
| def test_since_last_early_return_exits_one_on_failure( | ||
| tmp_path, monkeypatch, capsys | ||
| ): | ||
| """Since-last early-return with failure_count>0 must produce real exit code 1.""" | ||
| fake_result = BulkExportResult(latest_day=None, failure_count=1) | ||
|
|
||
| monkeypatch.setattr(export, "run_bulk_export", lambda **kwargs: fake_result) | ||
| monkeypatch.setattr(export, "list_projects", lambda base: [{"name": "p", "path": "/p"}]) | ||
|
|
||
| args = types.SimpleNamespace( | ||
| base_dir=str(tmp_path), | ||
| out=str(tmp_path / "out"), | ||
| since="last", | ||
| no_zip=True, | ||
| project=None, | ||
| format="md", | ||
| session=None, | ||
| exclude_rules=None, | ||
| ) | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| export.cmd_export(args) | ||
|
|
||
| assert exc_info.value.code == 1 | ||
| captured = capsys.readouterr() | ||
| assert "Exported 0 of 1 sessions (1 failed)" in captured.err | ||
|
|
||
|
|
||
| def test_cli_export_incremental_noop_no_stderr_summary(tmp_path): | ||
| """Second incremental run after state is saved: exit 0, no stderr summary.""" | ||
| base = _seed_base_dir(tmp_path) | ||
| out_dir = tmp_path / "out" | ||
| home_env = _isolated_home_env(tmp_path) | ||
| argv = [ | ||
| "export", | ||
| "--base-dir", | ||
| str(base), | ||
| "--no-zip", | ||
| "--out", | ||
| str(out_dir), | ||
| ] | ||
| first = _run_cli([*argv, "--since", "all"], env=home_env) | ||
| assert first.returncode == 0, first.stderr | ||
| assert list(out_dir.rglob("*.md")) | ||
|
|
||
| second = _run_cli([*argv, "--since", "incremental"], env=home_env) | ||
| assert second.returncode == 0, second.stderr | ||
| assert "Exported" not in second.stderr | ||
| assert "Nothing to export" in second.stdout | ||
|
|
||
|
|
||
| def test_cli_export_total_failure_exits_one(tmp_path, monkeypatch, capsys): | ||
| project_dir = tmp_path / "test-project" | ||
| project_dir.mkdir(parents=True) | ||
| (project_dir / "bad_a.jsonl").write_text("{}", encoding="utf-8") | ||
| (project_dir / "bad_b.jsonl").write_text("{}", encoding="utf-8") | ||
| out_dir = tmp_path / "out" | ||
|
|
||
| state_dir = tmp_path / "state" | ||
| state_dir.mkdir() | ||
| monkeypatch.setattr(export, "STATE_FILE", str(state_dir / "export_state.json")) | ||
| monkeypatch.setattr(export, "STATE_DIR", str(state_dir)) | ||
|
|
||
| def _parse(_path: str): | ||
| raise ValueError("simulated corrupt jsonl") | ||
|
|
||
| monkeypatch.setattr("utils.export_engine.parse_session", _parse) | ||
|
|
||
| with pytest.raises(SystemExit) as exc_info: | ||
| export.cmd_export(_export_args(tmp_path, tmp_path, out_dir)) | ||
|
|
||
| assert exc_info.value.code == 1 | ||
| captured = capsys.readouterr() | ||
| assert "Exported 0 of 2 sessions (2 failed)" in captured.err | ||
| assert "Nothing to export." in captured.out | ||
| assert list(out_dir.rglob("*.md")) == [] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.