|
| 1 | +""" |
| 2 | +Tests for tools/check_patches.py — the patch-set integrity linter. |
| 3 | +
|
| 4 | +The linter gates every PR, so a regression in it could silently start passing bad patches: |
| 5 | +the one thing it exists to prevent. Each test builds a tiny fixture patch-set in a temp |
| 6 | +directory and asserts that a given check fails on exactly the violation it targets — and |
| 7 | +that a clean set passes every check. |
| 8 | +
|
| 9 | +Run: pytest tools/tests -q |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +# Make `import check_patches` work when tests run from the repo root or from tools/. |
| 16 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) |
| 17 | +import check_patches as cp # noqa: E402 |
| 18 | + |
| 19 | + |
| 20 | +# --------------------------------------------------------------------------- fixture helpers |
| 21 | +def make_patch(path="src/foo.cc", added=(" int x = 0;",), *, |
| 22 | + hunk=True, headers=True, second_file=None) -> str: |
| 23 | + """Build a unified-diff patch. Defaults are clean and well-formed; the keyword args let a |
| 24 | + test break exactly one property (drop the hunk / the file headers, add a second file).""" |
| 25 | + lines = [f"diff --git a/{path} b/{path}", "index 1234567..89abcde 100644"] |
| 26 | + if headers: |
| 27 | + lines += [f"--- a/{path}", f"+++ b/{path}"] |
| 28 | + if hunk: |
| 29 | + lines.append(f"@@ -1,2 +1,{2 + len(added)} @@") |
| 30 | + lines.append(" context above") |
| 31 | + lines += [f"+{a}" for a in added] |
| 32 | + lines.append(" context below") |
| 33 | + if second_file: # a second `diff --git` -> two surfaces in one patch |
| 34 | + lines += [f"diff --git a/{second_file} b/{second_file}", |
| 35 | + f"--- a/{second_file}", f"+++ b/{second_file}", |
| 36 | + "@@ -1 +1,2 @@", " ctx", "+ added"] |
| 37 | + return "\n".join(lines) + "\n" |
| 38 | + |
| 39 | + |
| 40 | +def write_set(root: Path, patches: dict[str, str], series: list[str] | None = None) -> Path: |
| 41 | + """Write patch files + a series into `root`. Series defaults to the files in sorted order.""" |
| 42 | + for name, body in patches.items(): |
| 43 | + (root / name).write_text(body, encoding="utf-8") |
| 44 | + if series is None: |
| 45 | + series = sorted(patches) |
| 46 | + (root / "series").write_text("\n".join(series) + "\n", encoding="utf-8") |
| 47 | + return root |
| 48 | + |
| 49 | + |
| 50 | +# A clean two-patch set: contiguous numbering, in-sync series, one file each, well-formed, |
| 51 | +# a uxr- switch (exercises the uxr-only pass path), no brand literals. |
| 52 | +def clean_patches() -> dict[str, str]: |
| 53 | + return { |
| 54 | + "0001-alpha.patch": make_patch("core/alpha.cc", |
| 55 | + added=(' if (cmd.HasSwitch("uxr-alpha")) return;',)), |
| 56 | + "0002-beta.patch": make_patch("core/beta.cc", added=(" int y = 1;",)), |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +# --------------------------------------------------------------------------- the clean baseline |
| 61 | +def test_clean_set_passes_every_check(tmp_path): |
| 62 | + rep = cp.run_checks(write_set(tmp_path, clean_patches())) |
| 63 | + assert rep.failures == [] |
| 64 | + |
| 65 | + |
| 66 | +# --------------------------------------------------------------------------- series-sync |
| 67 | +def test_series_sync_missing_entry(tmp_path): |
| 68 | + # A patch file exists but is not listed -> apply-patches.sh would silently skip it. |
| 69 | + write_set(tmp_path, clean_patches(), series=["0001-alpha.patch"]) |
| 70 | + assert cp.run_checks(tmp_path).failures == ["series-sync"] |
| 71 | + |
| 72 | + |
| 73 | +def test_series_sync_missing_file(tmp_path): |
| 74 | + # Series lists a patch with no backing file -> the build breaks. |
| 75 | + write_set(tmp_path, clean_patches(), |
| 76 | + series=["0001-alpha.patch", "0002-beta.patch", "0003-ghost.patch"]) |
| 77 | + assert cp.run_checks(tmp_path).failures == ["series-sync"] |
| 78 | + |
| 79 | + |
| 80 | +def test_series_sync_duplicate_entry(tmp_path): |
| 81 | + write_set(tmp_path, clean_patches(), |
| 82 | + series=["0001-alpha.patch", "0001-alpha.patch", "0002-beta.patch"]) |
| 83 | + assert cp.run_checks(tmp_path).failures == ["series-sync"] |
| 84 | + |
| 85 | + |
| 86 | +def test_series_sync_wrong_order(tmp_path): |
| 87 | + write_set(tmp_path, clean_patches(), series=["0002-beta.patch", "0001-alpha.patch"]) |
| 88 | + assert cp.run_checks(tmp_path).failures == ["series-sync"] |
| 89 | + |
| 90 | + |
| 91 | +# --------------------------------------------------------------------------- numbering |
| 92 | +def test_numbering_gap(tmp_path): |
| 93 | + patches = {"0001-alpha.patch": make_patch("core/alpha.cc"), |
| 94 | + "0003-gamma.patch": make_patch("core/gamma.cc")} |
| 95 | + write_set(tmp_path, patches) # series auto-syncs, so only numbering should trip |
| 96 | + assert cp.run_checks(tmp_path).failures == ["numbering"] |
| 97 | + |
| 98 | + |
| 99 | +def test_numbering_duplicate_number(tmp_path): |
| 100 | + patches = {"0001-alpha.patch": make_patch("core/alpha.cc"), |
| 101 | + "0002-beta.patch": make_patch("core/beta.cc"), |
| 102 | + "0002-clone.patch": make_patch("core/clone.cc")} |
| 103 | + rep = cp.run_checks(write_set(tmp_path, patches)) |
| 104 | + assert rep.failures == ["numbering"] |
| 105 | + |
| 106 | + |
| 107 | +def test_numbering_non_conforming_name(tmp_path): |
| 108 | + patches = {"0001-alpha.patch": make_patch("core/alpha.cc"), |
| 109 | + "not-a-numbered.patch": make_patch("core/other.cc")} |
| 110 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["numbering"] |
| 111 | + |
| 112 | + |
| 113 | +# --------------------------------------------------------------------------- single-surface |
| 114 | +def test_single_surface_two_files(tmp_path): |
| 115 | + patches = {"0001-alpha.patch": make_patch("core/one.cc", second_file="core/two.cc")} |
| 116 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["single-surface"] |
| 117 | + |
| 118 | + |
| 119 | +# --------------------------------------------------------------------------- well-formed |
| 120 | +def test_well_formed_missing_hunk(tmp_path): |
| 121 | + patches = {"0001-alpha.patch": make_patch("core/alpha.cc", hunk=False)} |
| 122 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["well-formed"] |
| 123 | + |
| 124 | + |
| 125 | +def test_well_formed_missing_headers(tmp_path): |
| 126 | + patches = {"0001-alpha.patch": make_patch("core/alpha.cc", headers=False)} |
| 127 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["well-formed"] |
| 128 | + |
| 129 | + |
| 130 | +# --------------------------------------------------------------------------- uxr-only-switches |
| 131 | +def test_uxr_only_rejects_non_uxr_switch(tmp_path): |
| 132 | + patches = {"0001-alpha.patch": make_patch( |
| 133 | + "core/alpha.cc", added=(' if (cmd.HasSwitch("legacy-mode")) return;',))} |
| 134 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["uxr-only-switches"] |
| 135 | + |
| 136 | + |
| 137 | +# --------------------------------------------------------------------------- no-brand-literals |
| 138 | +def test_no_brand_literal_in_added_code(tmp_path): |
| 139 | + patches = {"0001-alpha.patch": make_patch( |
| 140 | + "core/alpha.cc", added=(' const char* k = "fortress-build";',))} |
| 141 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == ["no-brand-literals"] |
| 142 | + |
| 143 | + |
| 144 | +def test_brand_token_in_comment_is_allowed(tmp_path): |
| 145 | + # A `//` comment mentioning a brand does not ship as a string literal, so it must pass. |
| 146 | + patches = {"0001-alpha.patch": make_patch( |
| 147 | + "core/alpha.cc", added=(" int z = 0; // fortress tweak",))} |
| 148 | + assert cp.run_checks(write_set(tmp_path, patches)).failures == [] |
0 commit comments