diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cddddf..12da37a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Attribute OpenAI chat `developer` messages to the system segment instead of the user segment. +- `report --out` to a missing parent directory now exits cleanly with an + `error:` line instead of an unhandled traceback. ## [0.1.1] - 2026-08-06 diff --git a/src/ctxlens/cli.py b/src/ctxlens/cli.py index ef13178..ef87fdd 100644 --- a/src/ctxlens/cli.py +++ b/src/ctxlens/cli.py @@ -99,7 +99,11 @@ def report( out = path.with_suffix(path.suffix + default_ext) if out is not None: - out.write_text(content, encoding="utf-8") + try: + out.write_text(content, encoding="utf-8") + except OSError as exc: + err_console.print(f"[red]error:[/red] {exc}") + raise typer.Exit(EXIT_ERROR) from exc err_console.print(f"[green]Wrote[/green] {out}") else: sys.stdout.write(content + "\n") diff --git a/tests/test_cli.py b/tests/test_cli.py index b4b77f4..19cc563 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -70,6 +70,14 @@ def test_report_html_to_file(tmp_path, codex_session): assert out.read_text().startswith("") +def test_report_missing_parent_dir(tmp_path, codex_session): + out = tmp_path / "missing" / "dir" / "r.html" + result = runner.invoke(app, ["report", str(codex_session), "--html", "-o", str(out)]) + assert result.exit_code == 1 + assert "error:" in result.stderr + assert "Traceback" not in result.stderr + + def test_diff_json(openai_array, openai_chat): result = runner.invoke( app, ["diff", str(openai_array), str(openai_chat), "--json"]