|
| 1 | +"""Run the actual patch-bullet shell step locally without publication commands.""" |
| 2 | +from pathlib import Path |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +import unittest |
| 6 | + |
| 7 | +ROOT = Path(__file__).resolve().parents[1] |
| 8 | +WORKFLOW = ROOT / ".github/workflows/auto-release.yml" |
| 9 | +SHA = "1234567890abcdef1234567890abcdef12345678ab" |
| 10 | +REPO = "fixture/owned-repository" |
| 11 | + |
| 12 | + |
| 13 | +def compose_script(): |
| 14 | + workflow = WORKFLOW.read_text() |
| 15 | + marker = " - name: Compose patch bullet\n" |
| 16 | + assert workflow.count(marker) == 1 |
| 17 | + step = workflow.split(marker, 1)[1].split(" run: |\n", 1)[1] |
| 18 | + lines = [] |
| 19 | + for line in step.splitlines(): |
| 20 | + if line and not line.startswith(" "): |
| 21 | + break |
| 22 | + lines.append(line[10:]) |
| 23 | + script = "\n".join(lines) |
| 24 | + assert script.startswith("set -euo pipefail\n") |
| 25 | + return script |
| 26 | + |
| 27 | + |
| 28 | +class PatchBullet(unittest.TestCase): |
| 29 | + def check_message(self, message, description): |
| 30 | + with tempfile.TemporaryDirectory(prefix="coven-patch-bullet-") as directory: |
| 31 | + root = Path(directory) |
| 32 | + cargo = root / "src-rust/Cargo.toml" |
| 33 | + cargo.parent.mkdir() |
| 34 | + original = b'[workspace.package]\nversion = "1.2.3"\n' |
| 35 | + cargo.write_bytes(original) |
| 36 | + output = root / "output" |
| 37 | + result = subprocess.run( |
| 38 | + ["/bin/bash", "--noprofile", "--norc", "-c", compose_script()], |
| 39 | + cwd=root, |
| 40 | + env={ |
| 41 | + "PATH": "/usr/bin:/bin", "HOME": directory, |
| 42 | + "MSG": message, "SHA": SHA, "REPO": REPO, |
| 43 | + "GITHUB_OUTPUT": str(output), |
| 44 | + }, |
| 45 | + capture_output=True, text=True, timeout=5, |
| 46 | + ) |
| 47 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 48 | + bullet = f"- {description} ([`{SHA[:7]}`](https://github.com/{REPO}/commit/{SHA}))" |
| 49 | + self.assertEqual(output.read_text(), f"tag=v1.2.3\nbullet<<__EOF__\n{bullet}\n__EOF__\n") |
| 50 | + self.assertEqual(result.stdout, f"Will patch v1.2.3 with:\n {bullet}\n") |
| 51 | + self.assertEqual(result.stderr, "") |
| 52 | + self.assertEqual(cargo.read_bytes(), original) |
| 53 | + self.assertEqual(sorted(path.name for path in root.iterdir()), ["output", "src-rust"]) |
| 54 | + |
| 55 | + def test_large_bodies_do_not_break_subject_extraction(self): |
| 56 | + # Each environment value stays below Linux's per-string exec limit. |
| 57 | + for length in (100_000, 120_000): |
| 58 | + with self.subTest(body_bytes=length): |
| 59 | + self.check_message("fix output --patch\n\n" + ("body\n" * (length // 5)), "fix output") |
| 60 | + |
| 61 | + def test_first_line_and_marker_behavior(self): |
| 62 | + cases = [ |
| 63 | + ("fix output --patch", "fix output"), |
| 64 | + ("fix output --patch\n", "fix output"), |
| 65 | + ("fix output --patch\nbody ignored\n", "fix output"), |
| 66 | + (" --patch fix output --patch \nbody", "fix output"), |
| 67 | + ("subject without marker\n--patch", "subject without marker"), |
| 68 | + ("--patch\nbody", "(patch — see commit)"), |
| 69 | + ("\nbody --patch", "(patch — see commit)"), |
| 70 | + ("", "(patch — see commit)"), |
| 71 | + ] |
| 72 | + for message, expected in cases: |
| 73 | + with self.subTest(message=message): |
| 74 | + self.check_message(message, expected) |
| 75 | + |
| 76 | + def test_subject_is_literal_shell_data(self): |
| 77 | + subject = "fix $(touch injected) `touch injected2` ; & < > 'quoted' --patch" |
| 78 | + self.check_message(subject + "\nbody", subject.removesuffix(" --patch")) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + unittest.main() |
0 commit comments