|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +import yaml |
| 11 | + |
| 12 | + |
| 13 | +BASH = "bash" |
| 14 | +if os.name == "nt": |
| 15 | + git_path = shutil.which("git") |
| 16 | + if git_path is None: |
| 17 | + raise RuntimeError("Git for Windows is required to exercise the checkout workflow.") |
| 18 | + # The WSL bash launcher does not inherit these per-test environment values. |
| 19 | + candidates = ( |
| 20 | + pathlib.Path(git_path).resolve().parents[1] / "bin" / "bash.exe", |
| 21 | + pathlib.Path(os.environ["ProgramFiles"]) / "Git" / "bin" / "bash.exe", |
| 22 | + ) |
| 23 | + git_bash = next((candidate for candidate in candidates if candidate.is_file()), None) |
| 24 | + if git_bash is None: |
| 25 | + raise RuntimeError("Git for Windows Bash is required to exercise the checkout workflow.") |
| 26 | + BASH = str(git_bash) |
| 27 | + |
| 28 | + |
| 29 | +ROOT = pathlib.Path(__file__).resolve().parents[2] |
| 30 | +WORKFLOW_PATH = pathlib.Path( |
| 31 | + os.environ.get( |
| 32 | + "BUILD_ANALYSIS_WORKFLOW", |
| 33 | + ROOT / ".github/workflows/build-failure-analysis.md", |
| 34 | + ) |
| 35 | +) |
| 36 | +WORKFLOW = yaml.safe_load(WORKFLOW_PATH.read_text(encoding="utf-8").split("---", 2)[1]) |
| 37 | +ATTACH_SCRIPT = next( |
| 38 | + ( |
| 39 | + step["run"] |
| 40 | + for step in WORKFLOW["pre-agent-steps"] |
| 41 | + if step["name"] == "Attach the verified analysis revision" |
| 42 | + ), |
| 43 | + "", |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +class BuildFailureAnalysisCheckoutTests(unittest.TestCase): |
| 48 | + def setUp(self) -> None: |
| 49 | + self.temporary = tempfile.TemporaryDirectory() |
| 50 | + self.addCleanup(self.temporary.cleanup) |
| 51 | + self.root = pathlib.Path(self.temporary.name) |
| 52 | + self.origin = self.root / "origin" |
| 53 | + self.workspace = self.root / "workspace" |
| 54 | + self.environment = { |
| 55 | + **os.environ, |
| 56 | + "GIT_CONFIG_COUNT": "2", |
| 57 | + "GIT_CONFIG_KEY_0": "user.name", |
| 58 | + "GIT_CONFIG_VALUE_0": "Checkout Test", |
| 59 | + "GIT_CONFIG_KEY_1": "user.email", |
| 60 | + "GIT_CONFIG_VALUE_1": "checkout-test@example.invalid", |
| 61 | + } |
| 62 | + self.git("init", "--quiet", "--initial-branch=main", str(self.origin)) |
| 63 | + self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "Base") |
| 64 | + self.git("-C", str(self.origin), "switch", "--quiet", "--create", "analysis-target") |
| 65 | + self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "PR head") |
| 66 | + self.head = self.git("-C", str(self.origin), "rev-parse", "HEAD") |
| 67 | + self.git("-C", str(self.origin), "update-ref", "refs/pull/42/head", self.head) |
| 68 | + self.git("-C", str(self.origin), "switch", "--quiet", "main") |
| 69 | + self.git("init", "--quiet", str(self.workspace)) |
| 70 | + self.git("-C", str(self.workspace), "remote", "add", "origin", self.origin.as_uri()) |
| 71 | + |
| 72 | + def git(self, *arguments: str) -> str: |
| 73 | + result = subprocess.run( |
| 74 | + ["git", *arguments], |
| 75 | + env=self.environment, |
| 76 | + text=True, |
| 77 | + capture_output=True, |
| 78 | + timeout=30, |
| 79 | + check=True, |
| 80 | + ) |
| 81 | + return result.stdout.strip() |
| 82 | + |
| 83 | + def checkout(self, checkout_ref: str = "analysis-target") -> None: |
| 84 | + values = { |
| 85 | + "${{ needs.fetch-binlog.outputs.pr-head-sha }}": self.head, |
| 86 | + "${{ needs.fetch-binlog.outputs.pr-checkout-ref }}": checkout_ref, |
| 87 | + } |
| 88 | + ref = values[WORKFLOW["checkout"][0]["ref"]] |
| 89 | + result = subprocess.run( |
| 90 | + ["git", "-C", str(self.workspace), "fetch", "--quiet", "--depth=1", "origin", ref], |
| 91 | + env=self.environment, |
| 92 | + text=True, |
| 93 | + capture_output=True, |
| 94 | + timeout=30, |
| 95 | + ) |
| 96 | + self.assertEqual(0, result.returncode, result.stderr) |
| 97 | + if ref == self.head or ref.startswith("refs/pull/"): |
| 98 | + self.git("-C", str(self.workspace), "checkout", "--quiet", "--detach", "FETCH_HEAD") |
| 99 | + else: |
| 100 | + self.git("-C", str(self.workspace), "checkout", "--quiet", "-b", ref, "FETCH_HEAD") |
| 101 | + |
| 102 | + def attach(self, checkout_ref: str = "analysis-target", expected_sha: str | None = None) -> subprocess.CompletedProcess: |
| 103 | + return subprocess.run( |
| 104 | + [BASH, "-c", ATTACH_SCRIPT], |
| 105 | + cwd=self.workspace, |
| 106 | + env={ |
| 107 | + **self.environment, |
| 108 | + "EXPECTED_HEAD_SHA": self.head if expected_sha is None else expected_sha, |
| 109 | + "ANALYSIS_CHECKOUT_REF": checkout_ref, |
| 110 | + }, |
| 111 | + text=True, |
| 112 | + capture_output=True, |
| 113 | + timeout=30, |
| 114 | + ) |
| 115 | + |
| 116 | + def assert_attached_revision(self) -> None: |
| 117 | + result = self.attach() |
| 118 | + self.assertEqual(0, result.returncode, result.stdout + result.stderr) |
| 119 | + self.assertEqual(self.head, self.git("-C", str(self.workspace), "rev-parse", "HEAD")) |
| 120 | + self.assertEqual( |
| 121 | + "analysis-target", |
| 122 | + self.git("-C", str(self.workspace), "branch", "--show-current"), |
| 123 | + ) |
| 124 | + self.assertEqual( |
| 125 | + self.head, |
| 126 | + self.git("-C", str(self.workspace), "rev-parse", "refs/remotes/origin/analysis-target"), |
| 127 | + ) |
| 128 | + |
| 129 | + def test_deleted_branch_checks_out_validated_commit(self) -> None: |
| 130 | + self.git("-C", str(self.origin), "branch", "--delete", "--force", "analysis-target") |
| 131 | + |
| 132 | + self.checkout() |
| 133 | + |
| 134 | + self.assert_attached_revision() |
| 135 | + branches = self.git("-C", str(self.origin), "for-each-ref", "--format=%(refname)", "refs/heads") |
| 136 | + self.assertEqual("refs/heads/main", branches) |
| 137 | + |
| 138 | + def test_moving_branch_does_not_change_analysis_revision(self) -> None: |
| 139 | + self.git("-C", str(self.origin), "switch", "--quiet", "analysis-target") |
| 140 | + self.git("-C", str(self.origin), "commit", "--quiet", "--allow-empty", "-m", "Newer head") |
| 141 | + |
| 142 | + self.checkout() |
| 143 | + |
| 144 | + self.assert_attached_revision() |
| 145 | + |
| 146 | + def test_same_repo_patch_baseline_is_original_head(self) -> None: |
| 147 | + self.checkout() |
| 148 | + self.assert_attached_revision() |
| 149 | + |
| 150 | + self.git("-C", str(self.workspace), "commit", "--quiet", "--allow-empty", "-m", "Agent fix") |
| 151 | + |
| 152 | + self.assertEqual( |
| 153 | + "1", |
| 154 | + self.git("-C", str(self.workspace), "rev-list", "--count", "origin/analysis-target..HEAD"), |
| 155 | + ) |
| 156 | + |
| 157 | + def test_fork_remains_detached_without_same_named_origin_branch(self) -> None: |
| 158 | + self.checkout("refs/pull/42/head") |
| 159 | + |
| 160 | + result = self.attach("refs/pull/42/head") |
| 161 | + |
| 162 | + self.assertEqual(0, result.returncode, result.stdout + result.stderr) |
| 163 | + self.assertEqual(self.head, self.git("-C", str(self.workspace), "rev-parse", "HEAD")) |
| 164 | + self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current")) |
| 165 | + self.assertEqual( |
| 166 | + "", |
| 167 | + self.git("-C", str(self.workspace), "for-each-ref", "--format=%(refname)", "refs/remotes"), |
| 168 | + ) |
| 169 | + |
| 170 | + def test_mismatched_checkout_is_rejected_before_creating_refs(self) -> None: |
| 171 | + self.checkout() |
| 172 | + |
| 173 | + result = self.attach(expected_sha="0" * 40) |
| 174 | + |
| 175 | + self.assertNotEqual(0, result.returncode) |
| 176 | + self.assertIn("The checkout does not match the revision", result.stdout) |
| 177 | + self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current")) |
| 178 | + |
| 179 | + def test_invalid_branch_is_rejected_before_creating_refs(self) -> None: |
| 180 | + self.checkout() |
| 181 | + |
| 182 | + result = self.attach("--orphan") |
| 183 | + |
| 184 | + self.assertNotEqual(0, result.returncode) |
| 185 | + self.assertEqual("", self.git("-C", str(self.workspace), "branch", "--show-current")) |
| 186 | + self.assertEqual( |
| 187 | + "", |
| 188 | + self.git("-C", str(self.workspace), "for-each-ref", "--format=%(refname)", "refs/remotes"), |
| 189 | + ) |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + unittest.main() |
0 commit comments