diff --git a/src/ocr_sprint/tools/export_ground_truth.py b/src/ocr_sprint/tools/export_ground_truth.py index b89e470..8017be2 100644 --- a/src/ocr_sprint/tools/export_ground_truth.py +++ b/src/ocr_sprint/tools/export_ground_truth.py @@ -101,9 +101,8 @@ def main(argv: list[str] | None = None) -> int: ) out: Path = args.out - count = 0 if str(out) == "-": - _write_stream(sys.stdout.buffer, filters) + count = _write_stream(sys.stdout.buffer, filters) else: out.parent.mkdir(parents=True, exist_ok=True) with out.open("wb") as fh: diff --git a/tests/unit/test_cli_export_ground_truth.py b/tests/unit/test_cli_export_ground_truth.py index 1cece54..1544070 100644 --- a/tests/unit/test_cli_export_ground_truth.py +++ b/tests/unit/test_cli_export_ground_truth.py @@ -65,6 +65,22 @@ def test_cli_respects_limit(db_ready: None, tmp_path: Path) -> None: assert len(lines) == 1 +def test_cli_stdout_reports_correct_count( + db_ready: None, capsys: pytest.CaptureFixture[str] +) -> None: + """``--out -`` writes JSONL to stdout; the "wrote N" message must + reflect what actually streamed, not 0.""" + _seed_two_approved_jobs() + exit_code = export_main(["--out", "-"]) + assert exit_code == 0 + captured = capsys.readouterr() + stdout_lines = [line for line in captured.out.splitlines() if line.strip()] + assert len(stdout_lines) == 2 + for line in stdout_lines: + assert json.loads(line)["approved"] is True + assert "wrote 2 sample(s)" in captured.err + + def test_cli_print_stats_emits_json_to_stderr( db_ready: None, tmp_path: Path, capsys: pytest.CaptureFixture[str] ) -> None: