|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Regression tests for the agent/stacked-PR workflow contract parser.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import check_agent_branch_triggers as contract |
| 11 | + |
| 12 | + |
| 13 | +class TriggerContractParserTests(unittest.TestCase): |
| 14 | + def fixture(self, text: str) -> Path: |
| 15 | + tmp = tempfile.NamedTemporaryFile("w", suffix=".yml", delete=False, encoding="utf-8") |
| 16 | + tmp.write(text) |
| 17 | + tmp.close() |
| 18 | + self.addCleanup(Path(tmp.name).unlink, missing_ok=True) |
| 19 | + return Path(tmp.name) |
| 20 | + |
| 21 | + def test_comment_cannot_hide_pr_branch_filter(self) -> None: |
| 22 | + path = self.fixture( |
| 23 | + "on:\n pull_request:\n # comment at trigger indentation\n" |
| 24 | + " branches: [main]\n" |
| 25 | + ) |
| 26 | + self.assertFalse(contract._pull_request_is_unfiltered(path)) |
| 27 | + |
| 28 | + def test_path_and_type_filters_are_not_unfiltered(self) -> None: |
| 29 | + for filter_line in ("paths: ['src/**']", "paths-ignore: ['docs/**']", "types: [opened]"): |
| 30 | + with self.subTest(filter_line=filter_line): |
| 31 | + path = self.fixture(f"on:\n pull_request:\n {filter_line}\n") |
| 32 | + self.assertFalse(contract._pull_request_is_unfiltered(path)) |
| 33 | + |
| 34 | + def test_head_ref_text_outside_checkout_does_not_count(self) -> None: |
| 35 | + path = self.fixture( |
| 36 | + "jobs:\n test:\n runs-on: ubuntu-latest\n steps:\n" |
| 37 | + f" # ref: {contract.PR_HEAD_REF}\n" |
| 38 | + " - uses: actions/checkout@deadbeef\n" |
| 39 | + " with:\n persist-credentials: false\n" |
| 40 | + ) |
| 41 | + self.assertEqual(contract._checkout_ref_values(path), [None]) |
| 42 | + self.assertFalse(contract._checks_out_exact_pr_head(path)) |
| 43 | + |
| 44 | + def test_named_checkout_ref_is_bound_to_checkout_step(self) -> None: |
| 45 | + path = self.fixture( |
| 46 | + "jobs:\n test:\n steps:\n - name: Checkout\n" |
| 47 | + " uses: actions/checkout@deadbeef\n with:\n" |
| 48 | + f" ref: {contract.PR_HEAD_REF}\n" |
| 49 | + ) |
| 50 | + self.assertEqual(contract._checkout_ref_values(path), [contract.PR_HEAD_REF]) |
| 51 | + self.assertTrue(contract._checks_out_exact_pr_head(path)) |
| 52 | + |
| 53 | + def test_job_level_uses_ignores_comments_and_step_payloads(self) -> None: |
| 54 | + path = self.fixture( |
| 55 | + "jobs:\n smoke:\n runs-on: ubuntu-latest\n steps:\n" |
| 56 | + " # uses: ./.github/workflows/ci-python-local.yml\n" |
| 57 | + " - run: echo 'uses: ./.github/workflows/ci-python-local.yml'\n" |
| 58 | + ) |
| 59 | + self.assertEqual(contract._job_level_uses_values(path), []) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + unittest.main() |
0 commit comments