|
10 | 10 |
|
11 | 11 | from __future__ import annotations |
12 | 12 |
|
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +import textwrap |
| 16 | +from pathlib import Path |
| 17 | + |
13 | 18 | import alert_maintainer_change as alert |
| 19 | +import yaml |
14 | 20 |
|
15 | 21 | MARKER = "<!-- maintainer-approval-delta -->" |
16 | 22 |
|
@@ -85,3 +91,108 @@ def test_login_pattern_matches_the_gate() -> None: |
85 | 91 | import check_maintainer_approval as gate |
86 | 92 |
|
87 | 93 | assert alert.MAINTAINER_RE.pattern == gate.MAINTAINER_RE.pattern |
| 94 | + |
| 95 | + |
| 96 | +ROOT = Path(__file__).resolve().parents[2] |
| 97 | +WORKFLOW = ROOT / ".github/workflows/maintainers-change-alert.yml" |
| 98 | +STEP_NAME = "Post the maintainer delta" |
| 99 | + |
| 100 | + |
| 101 | +def workflow_step() -> dict: |
| 102 | + steps = yaml.safe_load(WORKFLOW.read_text())["jobs"]["describe-change"]["steps"] |
| 103 | + return next(step for step in steps if step.get("name") == STEP_NAME) |
| 104 | + |
| 105 | + |
| 106 | +def run_alert_step(tmp_path, merge_responses): |
| 107 | + """Run the actual workflow step against a controlled GitHub API fixture. |
| 108 | +
|
| 109 | + `merge_responses` lists the parents the merge ref reports on each |
| 110 | + successive call; the last entry repeats once the list is exhausted. |
| 111 | + """ |
| 112 | + import json |
| 113 | + |
| 114 | + step = workflow_step() |
| 115 | + env_values = { |
| 116 | + "GH_REPO": "NVIDIA/OpenShell", |
| 117 | + "PR_NUMBER": "42", |
| 118 | + "BASE_SHA": "base", |
| 119 | + "HEAD_SHA": "head", |
| 120 | + "COMMENT_MARKER": MARKER, |
| 121 | + "GH_TOKEN": "token", |
| 122 | + } |
| 123 | + # A renamed or added variable must fail here, not pass silently. |
| 124 | + assert set(step["env"]) == set(env_values) |
| 125 | + for index, parents in enumerate(merge_responses): |
| 126 | + (tmp_path / f"merge{index}.json").write_text( |
| 127 | + json.dumps({"sha": "merge", "parents": [{"sha": p} for p in parents]}) |
| 128 | + ) |
| 129 | + (tmp_path / "merge-count").write_text(str(len(merge_responses))) |
| 130 | + (tmp_path / "base.md").write_text(TABLE) |
| 131 | + (tmp_path / "merged.md").write_text( |
| 132 | + TABLE + "| Jim | [@purp](https://github.com/purp) | NVIDIA |\n" |
| 133 | + ) |
| 134 | + gh = tmp_path / "gh" |
| 135 | + gh.write_text( |
| 136 | + textwrap.dedent("""\ |
| 137 | + #!/bin/sh |
| 138 | + case "$*" in |
| 139 | + *commits/refs/pull/42/merge*) |
| 140 | + n=$(cat calls 2>/dev/null || echo 0) |
| 141 | + echo $((n + 1)) > calls |
| 142 | + last=$(($(cat merge-count) - 1)) |
| 143 | + [ "$n" -gt "$last" ] && n=$last |
| 144 | + cat "merge$n.json" ;; |
| 145 | + *contents/MAINTAINERS.md?ref=base*) cat base.md ;; |
| 146 | + *contents/MAINTAINERS.md?ref=merge*) cat merged.md ;; |
| 147 | + *issues/42/comments*--jq*) printf '1\\n' ;; |
| 148 | + *issues/comments/1*) cp body.md posted.md ;; |
| 149 | + *) echo "unexpected gh call: $*" >&2; exit 1 ;; |
| 150 | + esac |
| 151 | + """) |
| 152 | + ) |
| 153 | + gh.chmod(0o755) |
| 154 | + # No real waiting between retries. |
| 155 | + sleep = tmp_path / "sleep" |
| 156 | + sleep.write_text("#!/bin/sh\nexit 0\n") |
| 157 | + sleep.chmod(0o755) |
| 158 | + env = ( |
| 159 | + os.environ |
| 160 | + | env_values |
| 161 | + | { |
| 162 | + "PATH": f"{tmp_path}:{os.environ['PATH']}", |
| 163 | + "GITHUB_STEP_SUMMARY": str(tmp_path / "summary"), |
| 164 | + } |
| 165 | + ) |
| 166 | + (tmp_path / "tasks/scripts").mkdir(parents=True) |
| 167 | + (tmp_path / "tasks/scripts/alert_maintainer_change.py").symlink_to( |
| 168 | + ROOT / "tasks/scripts/alert_maintainer_change.py" |
| 169 | + ) |
| 170 | + return subprocess.run( |
| 171 | + ["bash", "-c", step["run"]], |
| 172 | + cwd=tmp_path, |
| 173 | + env=env, |
| 174 | + capture_output=True, |
| 175 | + text=True, |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +def test_workflow_reports_delta_from_verified_merge(tmp_path): |
| 180 | + result = run_alert_step(tmp_path, [["base", "head"]]) |
| 181 | + assert result.returncode == 0, result.stderr |
| 182 | + body = (tmp_path / "posted.md").read_text() |
| 183 | + assert "@purp" in body |
| 184 | + assert "Loses approval rights" not in body |
| 185 | + |
| 186 | + |
| 187 | +def test_workflow_rejects_merge_for_different_head(tmp_path): |
| 188 | + result = run_alert_step(tmp_path, [["base", "other-head"]]) |
| 189 | + assert result.returncode != 0 |
| 190 | + assert not (tmp_path / "posted.md").exists() |
| 191 | + |
| 192 | + |
| 193 | +def test_workflow_retries_until_the_merge_ref_catches_up(tmp_path): |
| 194 | + # Right after a push the ref still points at the previous merge. |
| 195 | + result = run_alert_step(tmp_path, [["base", "old-head"], ["base", "head"]]) |
| 196 | + assert result.returncode == 0, result.stderr |
| 197 | + assert int((tmp_path / "calls").read_text()) == 2 |
| 198 | + assert "@purp" in (tmp_path / "posted.md").read_text() |
0 commit comments