fix: render detection in the report text

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent beefb32cc8
commit 2025a71cb9
4 files changed
+53 -29

No files matched your search

+1 -13
View File
@@ -248,28 +248,16 @@ def detect_upstream_changes(
report.new_hashes = new_hashes
def _print_detection(report: ProfileReport) -> None:
if report.detection_skipped:
print(f" extras skipped: {report.detection_skipped}")
for item in report.new_files or ():
print(f" new file: {item['path']}:{item['line']} {item['name']}")
for path, elided in report.new_files_elided.items():
print(f" new file: {path}: {elided} further matches not shown")
for item in report.new_hashes or ():
print(f" new {item['kind']}: {item['path']} {item['value']}")
def print_extras(
args: argparse.Namespace, profile: dict, report: ProfileReport
) -> None:
"""Optional per-profile sections beyond the ref report.
"""--full-diff and --tree-diff, read from the forge after the ref report.
The ref report mutes a forge that refuses, but the extras read the same
forge again, so a refusal here is reported on the profile's own line
instead of ending a pass over every profile. A quota signal still
ends the profile.
"""
_print_detection(report)
if not (args.full_diff or args.tree_diff) or not report.head:
return
repo = select_repo(profile)
+5 -7
View File
@@ -95,17 +95,15 @@ def refuse_flag_combinations(args: argparse.Namespace) -> None:
("--triage", args.triage),
) if on
]
# The version check lives on the report, so the outputs built from the
# report (text, --json, --markdown) carry it. A URL list and a ranking
# do not. Detection lives on the report too, and --json carries it for
# check_freshness. The markdown table has no column for it.
# The version check and detection live on the report, so the outputs
# built from the report (text, --json, --markdown) carry them. A URL
# list and a ranking do not.
lists_only = args.fetch_plan or args.triage
off_json = lists_only or args.markdown
extras = [
flag for flag, on in (
("--check-version", args.check_version and lists_only),
("--detect-new-files", args.detect_new_files and off_json),
("--watch-hashes", args.watch_hashes and off_json),
("--detect-new-files", args.detect_new_files and lists_only),
("--watch-hashes", args.watch_hashes and lists_only),
("--full-diff", args.full_diff),
("--tree-diff", args.tree_diff),
) if on
+23 -2
View File
@@ -254,13 +254,34 @@ def _report_body(report: ProfileReport, changed_only: bool) -> list[str]:
return lines
def _detection_lines(report: ProfileReport) -> list[str]:
"""--detect-new-files and --watch-hashes findings of one report."""
lines = []
if report.detection_skipped:
lines.append(f" extras skipped: {report.detection_skipped}")
lines.extend(
f" new file: {item['path']}:{item['line']} {item['name']}"
for item in report.new_files or ()
)
lines.extend(
f" new file: {path}: {elided} further matches not shown"
for path, elided in report.new_files_elided.items()
)
lines.extend(
f" new {item['kind']}: {item['path']} {item['value']}"
for item in report.new_hashes or ()
)
return lines
def format_report(report: ProfileReport, changed_only: bool = False) -> str:
"""Human-readable report for one profile."""
lines = _report_header(report)
if report.skipped:
lines.append(f" skipped: {report.skipped}")
return "\n".join(lines)
return "\n".join(lines + _report_body(report, changed_only))
else:
lines.extend(_report_body(report, changed_only))
return "\n".join(lines + _detection_lines(report))
def report_to_dict(report: ProfileReport) -> dict:
+24 -7
View File
@@ -22,7 +22,6 @@ import profile_sync
from profiling import anchoring
from profiling import bump
from profiling import citations as profiling_citations
from profiling import detection
from profiling import proseedit
from profiling import realign
from profiling import rebase
@@ -2210,13 +2209,10 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase):
try:
report = _sample_report()
profiling_run._attach_detection(self._args(), self._profile(), report)
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
detection.print_extras(self._args(), self._profile(), report)
finally:
upstream.fetch_file = original
self.assertEqual(report.detection_skipped, "https://x/y: HTTP 403")
self.assertIn("extras skipped: https://x/y: HTTP 403", buffer.getvalue())
self.assertIn("extras skipped: https://x/y: HTTP 403", format_report(report))
def test_detection_lands_on_the_report(self):
"""A name the pinned file already carried is listed, but only a name
@@ -5340,8 +5336,29 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase):
self.assertEqual(code, 1)
self.assertIn("--full-diff", err)
def test_markdown_refuses_detection(self):
code, _, err = self._main("--markdown", "--detect-new-files")
def test_markdown_carries_detection(self):
"""Detection lives on the report, and the markdown document embeds
each report's text: refusing the pair left the finding unwritten."""
from unittest import mock
def detect(args, profile, report):
report.new_files = [
{"path": "a.c", "line": 3, "name": "new.rom", "since_pin": True}
]
report.new_hashes = [{"path": "a.c", "kind": "md5", "value": "0" * 32}]
with tempfile.TemporaryDirectory() as tmp, \
mock.patch.object(profiling_run, "_attach_detection", side_effect=detect):
code, _, err = self._main(
"--markdown", "--detect-new-files", "--watch-hashes", "--report-dir", tmp,
)
self.assertEqual(code, 0, err)
document = next(Path(tmp).glob("profile-sync-*.md")).read_text(encoding="utf-8")
self.assertIn(" new file: a.c:3 new.rom", document)
self.assertIn(f" new md5: a.c {'0' * 32}", document)
def test_rankings_refuse_detection(self):
code, _, err = self._main("--triage", "--detect-new-files")
self.assertEqual(code, 1)
self.assertIn("--detect-new-files", err)