refactor: pass detection and version flags in full

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent 714683d63c
commit 57ee0cdd8f
4 files changed
+50 -57

No files matched your search

+18 -16
View File
@@ -201,23 +201,25 @@ def cited_paths(report: ProfileReport) -> list[str]:
def detect_upstream_changes(
args: argparse.Namespace, profile: dict, report: ProfileReport,
repo: upstream.Repo,
profile: dict, report: ProfileReport, repo: upstream.Repo,
cache_dir: str, offline: bool, files: bool, hashes: bool,
) -> None:
names, hashes = declared_names(profile), declared_hashes(profile)
new_files: list[dict] = []
new_hashes: list[dict] = []
"""Record on the report the undeclared names (files) and the hash
literals added since the pin (hashes) of the cited files at HEAD."""
names, known = declared_names(profile), declared_hashes(profile)
found_files: list[dict] = []
found_hashes: list[dict] = []
for path in cited_paths(report):
head_lines = upstream.fetch_file(
repo, report.head, path, args.cache_dir, args.offline
repo, report.head, path, cache_dir, offline
)
if head_lines is None:
continue
pin_lines = (
upstream.fetch_file(repo, report.pin, path, args.cache_dir, args.offline)
upstream.fetch_file(repo, report.pin, path, cache_dir, offline)
or []
)
if args.detect_new_files:
if files:
found, elided = detect_new_files(head_lines, names, path=path)
at_pin = {
name.casefold()
@@ -225,27 +227,27 @@ def detect_upstream_changes(
pin_lines, names, cap=sys.maxsize, path=path
)[0]
}
new_files.extend(
found_files.extend(
{"path": path, "line": number, "name": name,
"since_pin": name.casefold() not in at_pin}
for number, name in found
)
if elided:
report.new_files_elided[path] = elided
if args.watch_hashes:
if hashes:
added = [
line
for line in difflib.unified_diff(pin_lines, head_lines, n=0)
if line.startswith("+") and not line.startswith("+++")
]
new_hashes.extend(
found_hashes.extend(
{"path": path, "kind": kind, "value": value}
for kind, value in watch_hashes(added, hashes)
for kind, value in watch_hashes(added, known)
)
if args.detect_new_files:
report.new_files = new_files
if args.watch_hashes:
report.new_hashes = new_hashes
if files:
report.new_files = found_files
if hashes:
report.new_hashes = found_hashes
def print_extras(
+11 -8
View File
@@ -222,8 +222,7 @@ def _gained_since_pin(
# A held pin is not advanced at all: bump_commit says so itself.
return False
if report.new_files is None and report.new_hashes is None:
probe = argparse.Namespace(**{**vars(args), "detect_new_files": True, "watch_hashes": True})
_attach_detection(probe, profile, report)
_attach_detection(profile, report, args.cache_dir, args.offline, True, True)
gained = sorted({item["name"] for item in report.new_files or () if item.get("since_pin")})
added = report.new_hashes or []
if not (gained or added or report.detection_skipped):
@@ -240,7 +239,8 @@ def _gained_since_pin(
def _attach_detection(
args: argparse.Namespace, profile: dict, report: ProfileReport
profile: dict, report: ProfileReport, cache_dir: str, offline: bool,
files: bool, hashes: bool,
) -> None:
"""Record undeclared names and hashes of the cited files on the report.
@@ -249,13 +249,13 @@ def _attach_detection(
report already muted it, and one refusal must not end a pass over every
profile. A quota signal still ends the profile.
"""
if not report.head or not (args.detect_new_files or args.watch_hashes):
if not report.head or not (files or hashes):
return
repo = select_repo(profile)
if repo is None:
return
try:
detect_upstream_changes(args, profile, report, repo)
detect_upstream_changes(profile, report, repo, cache_dir, offline, files, hashes)
except upstream.RateLimitError:
raise
except upstream.UpstreamError as exc:
@@ -288,9 +288,9 @@ def _attach_version(
relation = upstream.ancestry(
repo, version.tag_commit, report.pin, args.cache_dir, args.offline
)
passed = latest_passed_release(args, repo, version, report.pin)
passed = latest_passed_release(repo, version, report.pin, args.cache_dir, args.offline)
newer = None if passed or profile.get("pin_frozen") else newer_release(
args, repo, version, report.pin
repo, version, report.pin, args.cache_dir, args.offline
)
except upstream.RateLimitError:
raise
@@ -430,7 +430,10 @@ def _build_full_report(
profile["version_source"].get("no_genuine_copy") or "") or None
if args.check_version:
_attach_version(args, profile, report)
_attach_detection(args, profile, report)
_attach_detection(
profile, report, args.cache_dir, args.offline,
args.detect_new_files, args.watch_hashes,
)
return report
+10 -9
View File
@@ -2,7 +2,6 @@
from __future__ import annotations
import argparse
import re
import sys
from dataclasses import dataclass
@@ -150,10 +149,11 @@ def _later_number(candidate: str, declared: str) -> bool:
def newer_release(
args: argparse.Namespace,
repo: upstream.Repo,
version: VersionReport | None,
pin: str | None,
cache_dir: str,
offline: bool,
) -> str | None:
"""The latest release, when it comes after both the declared tag and the pin.
@@ -166,23 +166,24 @@ def newer_release(
return None
if published == version.declared:
return None
commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline)
commit = upstream.tag_commit(repo, published, cache_dir, offline)
if not commit or commit == pin:
return None
after_tag = upstream.ancestry(repo, version.tag_commit, commit, args.cache_dir, args.offline)
after_tag = upstream.ancestry(repo, version.tag_commit, commit, cache_dir, offline)
if not after_tag or after_tag[0] == 0 or after_tag[1] != 0:
return None
after_pin = upstream.ancestry(repo, pin, commit, args.cache_dir, args.offline)
after_pin = upstream.ancestry(repo, pin, commit, cache_dir, offline)
if not after_pin or after_pin[0] == 0:
return None
return published
def latest_passed_release(
args: argparse.Namespace,
repo: upstream.Repo,
version: VersionReport | None,
pin: str | None,
cache_dir: str,
offline: bool,
) -> str | None:
"""The latest release, when the pin already contains it and core_version
names another one.
@@ -201,18 +202,18 @@ def latest_passed_release(
# version says it is older. "latest", "continuous" or 1.53 under a
# declared 1.61 say nothing.
return None
commit = upstream.tag_commit(repo, published, args.cache_dir, args.offline)
commit = upstream.tag_commit(repo, published, cache_dir, offline)
if not commit:
return None
if version.tag_commit and version.tag_commit != commit:
# A project can publish a release older than its newest tag:
# pcsx2x6's latest release is v0.2.20, its declared tag v0.2.22.
older = upstream.ancestry(repo, commit, version.tag_commit, args.cache_dir, args.offline)
older = upstream.ancestry(repo, commit, version.tag_commit, cache_dir, offline)
if older and older[1] == 0:
return None
if commit == pin:
return published
relation = upstream.ancestry(repo, commit, pin, args.cache_dir, args.offline)
relation = upstream.ancestry(repo, commit, pin, cache_dir, offline)
if relation and relation[1] == 0:
return published
return None
+11 -24
View File
@@ -2189,13 +2189,6 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase):
"""A pass over every profile used to die on the first forge answering
403 to the detection reads, after the ref report had already muted it."""
def _args(self):
return argparse.Namespace(
check_version=False, detect_new_files=True, watch_hashes=False,
full_diff=False, tree_diff=False, cache_dir="x", offline=False,
ref=None, context=3,
)
def _profile(self):
return {"emulator": "T", "source": "https://github.com/o/n", "files": []}
@@ -2208,7 +2201,7 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase):
upstream.fetch_file = refuse
try:
report = _sample_report()
profiling_run._attach_detection(self._args(), self._profile(), report)
profiling_run._attach_detection(self._profile(), report, "x", False, True, False)
finally:
upstream.fetch_file = original
self.assertEqual(report.detection_skipped, "https://x/y: HTTP 403")
@@ -2223,11 +2216,9 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase):
}
original = upstream.fetch_file
upstream.fetch_file = lambda repo, rev, path, *a, **k: files[rev]
args = self._args()
args.watch_hashes = True
report = _sample_report()
try:
profiling_run._attach_detection(args, self._profile(), report)
profiling_run._attach_detection(self._profile(), report, "x", False, True, True)
finally:
upstream.fetch_file = original
by_name = {(i["path"], i["name"]): i["since_pin"] for i in report.new_files}
@@ -2250,7 +2241,7 @@ class TestExtrasSurviveARefusingForge(unittest.TestCase):
try:
with self.assertRaises(upstream.RateLimitError):
profiling_run._attach_detection(
self._args(), self._profile(), _sample_report()
self._profile(), _sample_report(), "x", False, True, False
)
finally:
upstream.fetch_file = original
@@ -2649,7 +2640,7 @@ class TestWriteDryRun(unittest.TestCase):
# The cited files gained nothing since the pin in these fixtures.
from unittest import mock
def nothing_gained(args, profile, report):
def nothing_gained(profile, report, *_):
report.new_files, report.new_hashes = [], []
self._detection = mock.patch.object(
@@ -2666,7 +2657,7 @@ class TestWriteDryRun(unittest.TestCase):
# only shifted.
from unittest import mock
def gained(args, profile, report):
def gained(profile, report, *_):
report.new_files = [{"path": "zn.c", "name": "coh3002t.zip", "since_pin": True}]
report.new_hashes = []
@@ -3676,10 +3667,9 @@ class APassedReleaseIsNewerThanTheTag(unittest.TestCase):
version = versions.VersionReport(
"v0.2.22", "v0.2.22", "v0.2.20", "2026-07-25", True, "tag22"
)
args = argparse.Namespace(cache_dir="c", offline=True)
with mock.patch.object(upstream, "tag_commit", return_value="rel20"), \
mock.patch.object(upstream, "ancestry", return_value=(5, 0)):
self.assertIsNone(versions.latest_passed_release(args, "repo", version, "pin"))
self.assertIsNone(versions.latest_passed_release("repo", version, "pin", "c", True))
class ANewerReleaseThanTheTag(unittest.TestCase):
@@ -3689,11 +3679,10 @@ class ANewerReleaseThanTheTag(unittest.TestCase):
from unittest import mock
version = versions.VersionReport("2.8", "2.9", "2.9", "2026-10-01", True, "tag28")
args = argparse.Namespace(cache_dir="c", offline=True)
answers = iter([after_tag, after_pin])
with mock.patch.object(upstream, "tag_commit", return_value="rel29"), \
mock.patch.object(upstream, "ancestry", side_effect=lambda *a, **k: next(answers)):
return versions.newer_release(args, "repo", version, "tag28")
return versions.newer_release("repo", version, "tag28", "c", True)
def test_a_later_release_is_named(self):
self.assertEqual(self._newer((12, 0), (12, 0)), "2.9")
@@ -3727,10 +3716,9 @@ class AProjectThatOnlyTags(unittest.TestCase):
from unittest import mock
version = versions.VersionReport("v1.2", "v1.3", None, None, True, "tag12")
args = argparse.Namespace(cache_dir="c", offline=True)
with mock.patch.object(upstream, "tag_commit", return_value="tag13"), \
mock.patch.object(upstream, "ancestry", return_value=(7, 0)):
self.assertEqual(versions.newer_release(args, "repo", version, "tag12"), "v1.3")
self.assertEqual(versions.newer_release("repo", version, "tag12", "c", True), "v1.3")
class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase):
@@ -3745,7 +3733,6 @@ class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase):
def test_only_a_higher_number_counts_without_a_tag(self):
from unittest import mock
args = argparse.Namespace(cache_dir="c", offline=True)
cases = {("9.0.5", "v9.0-6"): "v9.0-6", ("1.61", "1.53"): None,
("v0.1-12070", "latest"): None, ("Git", "v2.7"): None, ("5.2", "v5.2"): None,
("v1", "v1.0"): None, ("0.83", "20120302"): None, ("0.160", "v1.4.1"): None,
@@ -3755,7 +3742,7 @@ class APassedReleaseUnderAnUntaggedVersion(unittest.TestCase):
with self.subTest(declared=declared, release=release), \
mock.patch.object(upstream, "tag_commit", return_value="rel"), \
mock.patch.object(upstream, "ancestry", return_value=(3, 0)):
self.assertEqual(versions.latest_passed_release(args, "repo", version, "pin"), expected)
self.assertEqual(versions.latest_passed_release("repo", version, "pin", "c", True), expected)
def test_no_tag_and_nothing_passed_stays_quiet(self):
version = versions.VersionReport("9.0.5", None, None, None, False, None)
@@ -5375,7 +5362,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase):
each report's text: refusing the pair left the finding unwritten."""
from unittest import mock
def detect(args, profile, report):
def detect(profile, report, *_):
report.new_files = [
{"path": "a.c", "line": 3, "name": "new.rom", "since_pin": True}
]
@@ -5401,7 +5388,7 @@ class TestOutputModesApplyTheirFlags(unittest.TestCase):
only in the text report stayed a human reading."""
from unittest import mock
def detect(args, profile, report):
def detect(profile, report, *_):
report.new_files = [
{"path": "a.c", "line": 3, "name": "new.rom", "since_pin": True}
]