From 28601a2c7a975416717be498df35d546c71b99bc Mon Sep 17 00:00:00 2001 From: Himanshu Kumar Date: Fri, 17 Jul 2026 20:21:16 +0530 Subject: [PATCH] fix(reports): add extension handling to TerminalReport.generate_to_file TerminalReport.generate_to_file wrote the raw output path with no extension logic, while markdown, JSON, and HTML generators default to a format extension or normalize a wrong suffix. Now terminal defaults to report.txt when no extension is given and normalizes a non-.txt suffix to .txt, matching the other generators (issue #83). --- openagent_eval/reports/terminal.py | 7 ++++- tests/unit/test_reports/test_terminal.py | 39 +++++++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/openagent_eval/reports/terminal.py b/openagent_eval/reports/terminal.py index e5f952b..bf5779a 100644 --- a/openagent_eval/reports/terminal.py +++ b/openagent_eval/reports/terminal.py @@ -107,7 +107,12 @@ def generate_to_file(self, report: Any, output_path: Path | str) -> Path: Returns: Path to the written file. """ - path = self._prepare_output_file(output_path) + path = Path(output_path) + if path.suffix == "": + path = path / "report.txt" + elif path.suffix.lower() != ".txt": + path = path.with_suffix(".txt") + path = self._prepare_output_file(path) content = self.generate(report) path.write_text(content, encoding="utf-8") return path diff --git a/tests/unit/test_reports/test_terminal.py b/tests/unit/test_reports/test_terminal.py index fdd428c..0b7a5e7 100644 --- a/tests/unit/test_reports/test_terminal.py +++ b/tests/unit/test_reports/test_terminal.py @@ -75,12 +75,49 @@ def test_generate_to_file_creates_directory( ) -> None: """generate_to_file() creates parent directories.""" report = TerminalReport() - output_path = tmp_path / "subdir" / "report.txt" + output_path = Path(tmp_path / "subdir" / "report.txt") result_path = report.generate_to_file(evaluation_report, output_path) assert result_path.exists() assert result_path.parent.exists() + def test_generate_to_file_defaults_to_txt( + self, evaluation_report: Any, tmp_path: Path + ) -> None: + """generate_to_file() appends report.txt when no extension is given.""" + report = TerminalReport() + output_path = tmp_path / "my_report" + result_path = report.generate_to_file(evaluation_report, output_path) + + assert result_path.name == "report.txt" + assert result_path.exists() + content = result_path.read_text(encoding="utf-8") + assert "SUMMARY" in content + + def test_generate_to_file_normalizes_wrong_suffix( + self, evaluation_report: Any, tmp_path: Path + ) -> None: + """generate_to_file() normalizes a non-.txt suffix to .txt.""" + report = TerminalReport() + output_path = tmp_path / "my_report.log" + result_path = report.generate_to_file(evaluation_report, output_path) + + assert result_path.suffix.lower() == ".txt" + assert result_path.exists() + content = result_path.read_text(encoding="utf-8") + assert "SUMMARY" in content + + def test_generate_to_file_keeps_txt_suffix( + self, evaluation_report: Any, tmp_path: Path + ) -> None: + """generate_to_file() preserves an explicit .txt path.""" + report = TerminalReport() + output_path = tmp_path / "explicit.txt" + result_path = report.generate_to_file(evaluation_report, output_path) + + assert result_path.name == "explicit.txt" + assert result_path.exists() + def test_print_report( self, evaluation_report: Any, tmp_path: Path ) -> None: