|
| 1 | +"""The checkout axes clone narrowly and fail closed on the merge base. |
| 2 | +
|
| 3 | +Both PR-head checkouts in cursor-axis-base.yml (the Cursor `axis` job and |
| 4 | +`axis-direct`) are depth 1, followed by the shared fetch-history.sh, which |
| 5 | +fetches back to the merge base of BASE_SHA and commit_sha or fails the job. |
| 6 | +A full-history clone took minutes per axis on a large monorepo. |
| 7 | +
|
| 8 | +Run: python3 -m unittest discover -s .github/cursor-approve/tests -p 'test_*.py' |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import re |
| 13 | +import unittest |
| 14 | + |
| 15 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 16 | +ROOT = os.path.normpath(os.path.join(HERE, "..", "..", "..")) |
| 17 | +with open(os.path.join(ROOT, ".github", "workflows", "cursor-axis-base.yml"), encoding="utf-8") as f: |
| 18 | + BASE = f.read() |
| 19 | + |
| 20 | +CHECKOUT = "Check out the PR head (read-only)" |
| 21 | +FETCH = "Fetch the merge-base history" |
| 22 | +RENDER = "Render prompt" |
| 23 | +SCRIPT = ('run: bash "$GITHUB_WORKSPACE/_cursor_approve_assets/' |
| 24 | + 'scripts/check-pr-size/fetch-history.sh"') |
| 25 | + |
| 26 | + |
| 27 | +def job(name): |
| 28 | + start = BASE.index(f"\n {name}:\n") |
| 29 | + nxt = re.search(r"\n [A-Za-z0-9_-]+:\n", BASE[start + 1:]) |
| 30 | + return BASE[start:start + 1 + nxt.start()] if nxt else BASE[start:] |
| 31 | + |
| 32 | + |
| 33 | +def steps(text): |
| 34 | + """[(name, text)] in order.""" |
| 35 | + parts = re.split(r"\n - name: ", text) |
| 36 | + return [(p.split("\n", 1)[0].strip(), p) for p in parts[1:]] |
| 37 | + |
| 38 | + |
| 39 | +class NarrowCheckout(unittest.TestCase): |
| 40 | + def test_no_full_history_clone(self): |
| 41 | + self.assertNotRegex(BASE, r"fetch-depth:\s*0\b") |
| 42 | + |
| 43 | + def test_each_checkout_job_fetches_the_merge_base_before_rendering(self): |
| 44 | + for name, gate in (("axis", "if: inputs.checkout"), ("axis-direct", None)): |
| 45 | + with self.subTest(job=name): |
| 46 | + st = steps(job(name)) |
| 47 | + names = [n for n, _ in st] |
| 48 | + self.assertLess(names.index(CHECKOUT), names.index(FETCH)) |
| 49 | + self.assertLess(names.index(FETCH), names.index(RENDER)) |
| 50 | + checkout, fetch = dict(st)[CHECKOUT], dict(st)[FETCH] |
| 51 | + self.assertIn("fetch-depth: 1\n", checkout) |
| 52 | + self.assertIn("ref: ${{ inputs.commit_sha }}", checkout) |
| 53 | + self.assertIn(SCRIPT, fetch) |
| 54 | + self.assertIn("working-directory: pr\n", fetch) |
| 55 | + self.assertIn("HEAD_SHA: ${{ inputs.commit_sha }}", fetch) |
| 56 | + self.assertIn("BASE_SHA: ${{ github.event.pull_request.base.sha }}", fetch) |
| 57 | + self.assertIn("FETCH_TOKEN: ${{ github.token }}", fetch) |
| 58 | + if gate: |
| 59 | + self.assertIn(gate + "\n", fetch) |
| 60 | + |
| 61 | + def test_render_reads_the_vouched_merge_base_with_no_fallback(self): |
| 62 | + # A silent fallback to BASE_SHA would show every base-branch commit |
| 63 | + # since the fork point as reverted by the PR. |
| 64 | + for name in ("axis", "axis-direct"): |
| 65 | + with self.subTest(job=name): |
| 66 | + render = dict(steps(job(name)))[RENDER] |
| 67 | + self.assertIn('merge_base="$(git -C pr merge-base "$BASE_SHA" "$COMMIT_SHA")"', render) |
| 68 | + self.assertNotIn("origin/$BASE_REF", render) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + unittest.main() |
0 commit comments