|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Hold the release workflow's Coven engine pin request to its contract. |
| 3 | +
|
| 4 | +Runs the job's own script against a recording stand-in for `gh`; nothing is |
| 5 | +dispatched. |
| 6 | +""" |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +import re |
| 10 | +import subprocess |
| 11 | +import tempfile |
| 12 | +import textwrap |
| 13 | +import unittest |
| 14 | + |
| 15 | +ROOT = Path(__file__).resolve().parents[1] |
| 16 | +RELEASE = (ROOT / '.github/workflows/release.yml').read_text(encoding='utf-8') |
| 17 | +JOB = RELEASE.split('\n advance-coven-engine:\n', 1)[1] |
| 18 | +STAND_IN = '#!/bin/sh\nprintf \'%s\\n\' "$*" >> "$GH_RECORD"\nexit "${GH_EXIT:-0}"\n' |
| 19 | + |
| 20 | + |
| 21 | +class CovenEnginePinRequestTests(unittest.TestCase): |
| 22 | + def run_step(self, token, gh_exit=0): |
| 23 | + script = textwrap.dedent(JOB.split(' run: |\n', 1)[1]) |
| 24 | + with tempfile.TemporaryDirectory() as directory: |
| 25 | + record = Path(directory) / 'gh-calls' |
| 26 | + stand_in = Path(directory) / 'gh' |
| 27 | + stand_in.write_text(STAND_IN, encoding='utf-8') |
| 28 | + stand_in.chmod(0o700) |
| 29 | + env = {'PATH': f'{directory}:/usr/bin:/bin', 'HOME': directory, 'RELEASE_VERSION': 'v1.2.3', |
| 30 | + 'GH_RECORD': str(record), 'GH_EXIT': str(gh_exit)} |
| 31 | + if token is not None: |
| 32 | + env['GH_TOKEN'] = token |
| 33 | + done = subprocess.run(['bash', '-c', script], cwd=directory, env=env, |
| 34 | + capture_output=True, text=True, timeout=10) |
| 35 | + calls = record.read_text(encoding='utf-8').splitlines() if record.exists() else [] |
| 36 | + return done, calls |
| 37 | + |
| 38 | + def test_requests_the_pin_by_default_and_can_be_turned_off(self): |
| 39 | + declared = RELEASE.split(' advance_coven_engine:\n', 1)[1].split('\n\n', 1)[0] |
| 40 | + self.assertIn(' type: boolean\n', declared) |
| 41 | + self.assertTrue(declared.endswith(' default: true')) |
| 42 | + self.assertIn(' if: ${{ inputs.advance_coven_engine }}\n', JOB) |
| 43 | + |
| 44 | + def test_runs_only_after_publication_and_holds_no_repository_token(self): |
| 45 | + self.assertIn(' needs: release\n', JOB) |
| 46 | + self.assertIn(' permissions: {}\n', JOB) |
| 47 | + self.assertEqual(re.findall(r'secrets\.(\w+)', JOB), ['COVEN_ENGINE_BUMP_TOKEN']) |
| 48 | + self.assertNotIn('actions/checkout', JOB) |
| 49 | + self.assertEqual(RELEASE.count('COVEN_ENGINE_BUMP_TOKEN }}'), 1) |
| 50 | + |
| 51 | + def test_dispatches_the_engine_bump_workflow_on_coven_main(self): |
| 52 | + done, calls = self.run_step('token') |
| 53 | + self.assertEqual(done.returncode, 0, done.stdout + done.stderr) |
| 54 | + self.assertEqual(calls, ['workflow run engine-bump.yml --repo OpenCoven/coven --ref main']) |
| 55 | + self.assertIn('v1.2.3', done.stdout) |
| 56 | + |
| 57 | + def test_missing_token_warns_and_does_not_fail_the_published_release(self): |
| 58 | + for token in (None, ''): |
| 59 | + with self.subTest(token=token): |
| 60 | + done, calls = self.run_step(token) |
| 61 | + self.assertEqual(done.returncode, 0, done.stdout + done.stderr) |
| 62 | + self.assertEqual(calls, []) |
| 63 | + self.assertIn('::warning::COVEN_ENGINE_BUMP_TOKEN is not set', done.stdout) |
| 64 | + self.assertIn('v1.2.3', done.stdout) |
| 65 | + |
| 66 | + def test_refused_dispatch_fails_the_job(self): |
| 67 | + done, calls = self.run_step('token', gh_exit=1) |
| 68 | + self.assertNotEqual(done.returncode, 0) |
| 69 | + self.assertEqual(len(calls), 1) |
| 70 | + self.assertNotIn('Asked OpenCoven/coven', done.stdout) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + unittest.main() |
0 commit comments