Spaces:
Running
Running
File size: 6,175 Bytes
a9e27c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | """Workflow events, checkout identity and publishing privileges are contracts."""
import shlex
from pathlib import Path
import pytest
import yaml
from scripts.ci_shards import RUNNERS
from tests.scripts.test_version_policy import commit, git, write
ROOT = Path(__file__).resolve().parents[2]
def load(name):
return yaml.safe_load(
(ROOT / ".github/workflows" / name).read_text(encoding="utf-8")
)
def test_post_merge_serializes_and_limits_publish_permissions():
config = load("post-merge.yml")
assert config["concurrency"] == {
"group": "post-merge-main",
"cancel-in-progress": False,
"queue": "max",
}
assert len(config["jobs"]) == 2
cache = config["jobs"]["warm-dependency-cache"]
assert {e["os"] for e in cache["strategy"]["matrix"]["include"]} == {
"Linux",
"Windows",
"macOS",
}
publish = config["jobs"]["publish"]
assert publish["runs-on"] == "macos-latest"
assert {e["runner"] for e in cache["strategy"]["matrix"]["include"]} == set(
RUNNERS.values()
)
setup = next(
step
for step in publish["steps"]
if step.get("uses") == "./.github/actions/ci-environment"
)
assert setup["with"]["runner-label"] == publish["runs-on"]
assert "needs" not in publish
assert publish["environment"] == "pypi"
assert publish["permissions"] == {"contents": "write", "id-token": "write"}
assert cache["permissions"] == {"contents": "read"}
for job in (cache, publish):
assert job["if"] == "github.ref == 'refs/heads/main'"
checkout = next(
s for s in job["steps"] if s.get("uses", "").startswith("actions/checkout@")
)
assert checkout["with"]["ref"] == "${{ github.sha }}"
assert checkout["with"]["fetch-depth"] == 0
step = next(s for s in publish["steps"] if s.get("name") == "Publish release")
assert step["env"]["RELEASE_COMMIT"] == "${{ inputs.release_commit || github.sha }}"
assert "-m scripts.publish_release" in step["run"]
def test_title_edits_only_trigger_the_separate_required_check():
policy = load("version-policy.yml")
tests = load("tests.yml")
assert set(policy[True]) == {"pull_request"}
assert set(policy[True]["pull_request"]["types"]) == {
"opened",
"synchronize",
"reopened",
"edited",
"ready_for_review",
}
assert tests[True]["pull_request"] is None
assert policy["concurrency"]["group"] != tests["concurrency"]["group"]
assert "version-policy" not in tests["jobs"]
job = policy["jobs"]["version-policy"]
assert job["name"] == "Version policy"
assert job["permissions"] == {"contents": "read", "pull-requests": "read"}
checkout = next(
s for s in job["steps"] if s.get("uses", "").startswith("actions/checkout@")
)
assert checkout["with"]["ref"] == "${{ github.event.pull_request.head.sha }}"
assert checkout["with"]["fetch-depth"] == 0
current = next(
s for s in job["steps"] if s.get("name") == "Read current pull request"
)
assert 'gh api "repos/$PR_REPOSITORY/pulls/$PR_NUMBER"' in current["run"]
check = next(s for s in job["steps"] if s.get("name") == "Check version policy")
assert "--pr-json" in check["run"]
assert "title" not in check["env"]
def test_all_project_setup_jobs_have_history_and_fetch_canonical_tags():
for filename in ("tests.yml", "version-policy.yml", "post-merge.yml"):
for job in load(filename)["jobs"].values():
if any(
s.get("uses") == "./.github/actions/ci-environment"
for s in job["steps"]
):
checkout = next(
s
for s in job["steps"]
if s.get("uses", "").startswith("actions/checkout@")
)
assert checkout["with"]["fetch-depth"] == 0
setup = yaml.safe_load(
(ROOT / ".github/actions/ci-environment/action.yml").read_text()
)
fetch = setup["runs"]["steps"][0]
assert fetch["env"]["CI_REPOSITORY"] == "${{ github.repository }}"
def test_ci_checks_out_the_event_merge_commit_from_the_base_repository():
config = load("tests.yml")
assert set(config[True]) == {"pull_request"}
checkouts = [
step["with"]
for job in config["jobs"].values()
for step in job["steps"]
if step.get("uses", "").startswith("actions/checkout@")
]
assert checkouts
for checkout in checkouts:
assert checkout["ref"] == "${{ github.sha }}"
assert checkout.get("repository", "${{ github.repository }}") == (
"${{ github.repository }}"
)
@pytest.mark.parametrize("conflicting_tag", [False, True])
def test_ci_tag_fetch_uses_only_canonical_tags(tmp_path, conflicting_tag):
upstream = tmp_path / "upstream.git"
upstream.mkdir()
git(upstream, "init", "-b", "main")
write(upstream, "file", "release")
released = commit(upstream)
git(upstream, "tag", "-a", "v1.2.3", "-m", "release")
checkout = tmp_path / "fork"
git(tmp_path, "clone", str(upstream), str(checkout))
write(checkout, "file", "pull request")
head = commit(checkout)
git(checkout, "tag", "v99.0.0")
if conflicting_tag:
git(checkout, "tag", "-f", "v1.2.3")
write(upstream, "file", "new release")
latest = commit(upstream)
git(upstream, "tag", "v1.2.4")
setup = yaml.safe_load(
(ROOT / ".github/actions/ci-environment/action.yml").read_text()
)
command = shlex.split(setup["runs"]["steps"][0]["run"])
command = [
arg.replace("$CI_SERVER/$CI_REPOSITORY.git", upstream.as_posix())
for arg in command
]
assert command[0] == "git"
for _ in range(2):
git(checkout, *command[1:])
assert git(checkout, "tag", "--list").splitlines() == ["v1.2.3", "v1.2.4"]
assert git(checkout, "rev-parse", "v1.2.3^{}") == released
assert git(checkout, "rev-parse", "v1.2.4") == latest
assert git(checkout, "rev-parse", "HEAD") == head
assert git(checkout, "status", "--porcelain") == ""
|