-
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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
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,114 @@ | ||
| """CLI export exit codes for bulk export (partial / total failure).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| 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.jsonl_parser import parse_session # noqa: E402 | ||
|
|
||
| _SUMMARY_RE = re.compile( | ||
| r"Exported \d+ of \d+ sessions \(\d+ failed\)", | ||
| ) | ||
|
|
||
|
|
||
| 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")) | ||
| if proc.stderr.strip(): | ||
| assert "failed" not in proc.stderr.lower() or "0 failed" in proc.stderr | ||
|
|
||
|
|
||
| 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 = next(base.iterdir()) | ||
| 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_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.