Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions insight/reporter.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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")
31 changes: 31 additions & 0 deletions tests/test_reporter.py
Original file line number Diff line number Diff line change
@@ -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