diff --git a/insight/reporter.py b/insight/reporter.py index 92e0c5a..8b25c15 100644 --- a/insight/reporter.py +++ b/insight/reporter.py @@ -1,7 +1,15 @@ import os + +def _report_filename(file_path): + normalized_path = os.path.normpath(file_path) + if os.altsep: + normalized_path = normalized_path.replace(os.altsep, "_") + return normalized_path.replace(os.sep, "_").replace(":", "_") + ".md" + + def save_file_report(file_info, output_dir): - filename = os.path.basename(file_info["file"]) + ".md" + filename = _report_filename(file_info["file"]) filepath = os.path.join(output_dir, filename) with open(filepath, "w") as f: f.write(f"# Report for `{file_info['file']}`\n\n") @@ -30,5 +38,5 @@ def generate_report(analysis, output_dir="report"): f.write(f"**Total lines of code:** {total_lines}\n\n") f.write("## Files Included\n") for file_info in analysis: - short_name = os.path.basename(file_info["file"]) - f.write(f"- [{short_name}]({short_name}.md) ({file_info['total_lines']} lines)\n") + report_name = _report_filename(file_info["file"]) + f.write(f"- [{report_name}]({report_name}) ({file_info['total_lines']} lines)\n") diff --git a/tests/test_reporter.py b/tests/test_reporter.py new file mode 100644 index 0000000..c4eb73e --- /dev/null +++ b/tests/test_reporter.py @@ -0,0 +1,31 @@ +from pathlib import Path + +from insight.reporter import generate_report + + +def test_reports_keep_same_basenames_distinct_and_linkable(tmp_path: Path) -> None: + analysis = [ + { + "file": "pkg_a/user.py", + "total_lines": 1, + "explanation": "first file", + "preview": [], + }, + { + "file": "pkg_b/user.py", + "total_lines": 2, + "explanation": "second file", + "preview": [], + }, + ] + + generate_report(analysis, tmp_path) + + first_report = tmp_path / "pkg_a_user.py.md" + second_report = tmp_path / "pkg_b_user.py.md" + summary = (tmp_path / "summary.md").read_text(encoding="utf-8") + + assert first_report.read_text(encoding="utf-8").count("first file") == 1 + assert second_report.read_text(encoding="utf-8").count("second file") == 1 + assert "- [pkg_a_user.py.md](pkg_a_user.py.md) (1 lines)" in summary + assert "- [pkg_b_user.py.md](pkg_b_user.py.md) (2 lines)" in summary