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
36 changes: 29 additions & 7 deletions scripts/patch_research_edition_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,20 @@

@dataclass(frozen=True)
class Patch:
"""One exact, unique substring replacement in one file."""
"""One exact substring replacement in one file.

``counts`` lists the occurrence counts that are acceptable for ``old``.
It defaults to exactly one, which keeps every patch fail-closed. A site
that upstream duplicated across two call paths may declare ``(1, 2)``;
every allowed occurrence is then rewritten and any other count still
aborts, so a future third copy cannot slip through unpatched.
"""

path: str
old: str
new: str
why: str
counts: tuple[int, ...] = (1,)


# --- profile fallbacks ------------------------------------------------------
Expand Down Expand Up @@ -247,9 +255,16 @@ def _python_profile_patches(path: str) -> List[Patch]:
),
Patch(
"aw-qt/aw_qt/config.py",
" default_port = 5666 if is_testing(profile) else 5600\n",
f" default_port = 5666 if is_testing(profile) else {RESEARCH_PORT}\n",
"aw-qt _read_server_port fallback",
# aw-qt reads this fallback in two places: `_read_server_port` (the
# tray/manager probe) and, since aw-qt#144, `dashboard_auth` (which
# compares a config file's port against the live one). Both must be
# the research port or the tray opens an unauthenticated dashboard
# against a server on 5667. The pinned aw-qt predates `dashboard_auth`
# and has one copy; the upstream tip has two, hence ``counts=(1, 2)``.
"default_port = 5666 if is_testing(profile) else 5600",
f"default_port = 5666 if is_testing(profile) else {RESEARCH_PORT}",
"aw-qt port fallbacks (server-port read and dashboard auth)",
counts=(1, 2),
),
Patch(
"aw-qt/aw_qt/manager.py",
Expand Down Expand Up @@ -684,14 +699,21 @@ def apply_patches(
"patch a tree twice"
)
occurrences = text.count(patch.old)
if occurrences != 1:
if occurrences not in patch.counts:
expected = (
"exactly one match"
if patch.counts == (1,)
else "one of {} matches".format(
", ".join(str(count) for count in patch.counts)
)
)
raise PatchError(
f"{rel_path}: expected exactly one match for [{patch.why}], "
f"{rel_path}: expected {expected} for [{patch.why}], "
f"found {occurrences}. The source drifted from what this "
"patcher targets - refusing to ship a research build with "
"the default profile/port/bundle id."
)
text = text.replace(patch.old, patch.new, 1)
text = text.replace(patch.old, patch.new)
applied.append(f"{rel_path}: {patch.why}")

if not check:
Expand Down
41 changes: 41 additions & 0 deletions scripts/tests/test_patch_research_edition_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import sys
from pathlib import Path
from string import Template
from typing import get_type_hints

import pytest

Expand Down Expand Up @@ -118,6 +119,46 @@ def test_ambiguous_token_fails_closed(tmp_path: Path):
patcher.apply_patches(root, patches, check=False)


def test_patch_type_hints_resolve():
assert get_type_hints(patcher.Patch)["counts"] == tuple[int, ...]


@pytest.mark.parametrize("copies", [1, 2])
def test_declared_multi_site_patch_rewrites_every_copy(tmp_path: Path, copies: int):
rel = "aw-qt/aw_qt/config.py"
file_path = tmp_path / rel
file_path.parent.mkdir(parents=True)
file_path.write_text(
"".join(
f"{' ' * indent}default_port = 5666 if is_testing(profile) else 5600\n"
for indent in (4, 12)[:copies]
),
encoding="utf-8",
)
patch = next(p for p in patcher.PORT_PATCHES_QT if p.path == rel)

applied = patcher.apply_patches(tmp_path, [patch], check=False)

assert applied == [f"{rel}: {patch.why}"]
text = file_path.read_text(encoding="utf-8")
assert "else 5600" not in text
assert text.count("else 5667") == copies


def test_declared_multi_site_patch_fails_closed_on_a_third_copy(tmp_path: Path):
rel = "aw-qt/aw_qt/config.py"
file_path = tmp_path / rel
file_path.parent.mkdir(parents=True)
file_path.write_text(
"default_port = 5666 if is_testing(profile) else 5600\n" * 3,
encoding="utf-8",
)
patch = next(p for p in patcher.PORT_PATCHES_QT if p.path == rel)

with pytest.raises(patcher.PatchError, match="found 3"):
patcher.apply_patches(tmp_path, [patch], check=False)


def test_nothing_written_when_one_site_fails(tmp_path: Path):
root = make_fixture_root(tmp_path, "qt")
patches = patcher.TARGETS["qt"]
Expand Down
Loading