返回 last30days-skill
test_changelog_guard_history.py
根目录 / tests / test_changelog_guard_history.py
1 """Exercise the workflow shell against real, disposable Git histories."""
2
3 from __future__ import annotations
4
5 import os
6 import shutil
7 import subprocess
8 import sys
9 from pathlib import Path
10
11 import pytest
12 import yaml
13
14 ROOT = Path(__file__).resolve().parents[1]
15
16
17 @pytest.fixture(scope="module")
18 def workflow_bash():
19 bash = shutil.which("bash")
20 if bash is None:
21 pytest.skip("Changelog workflow requires Bash with the mapfile builtin")
22 probe = subprocess.run(
23 [bash, "-c", "type -t mapfile"], text=True, capture_output=True,
24 )
25 if probe.returncode != 0 or probe.stdout.strip() != "builtin":
26 pytest.skip(f"Changelog workflow requires mapfile; unavailable in {bash}")
27 return bash
28
29
30 @pytest.fixture
31 def history(tmp_path, workflow_bash):
32 env = {key: value for key, value in os.environ.items() if not key.startswith("GIT_")}
33 env.update(
34 GIT_CONFIG_NOSYSTEM="1",
35 GIT_CONFIG_GLOBAL=os.devnull,
36 GIT_AUTHOR_NAME="Test",
37 GIT_COMMITTER_NAME="Test",
38 GIT_AUTHOR_EMAIL="test@example.invalid",
39 GIT_COMMITTER_EMAIL="test@example.invalid",
40 )
41
42 def git(*args, input=None):
43 return subprocess.run(
44 ["git", *args], cwd=tmp_path, env=env, input=input,
45 text=True, capture_output=True, check=True,
46 ).stdout.strip()
47
48 git("init", "--quiet")
49
50 def commit(changes, *parents):
51 git("read-tree", parents[0] if parents else "--empty")
52 for path, content in changes.items():
53 blob = git("hash-object", "-w", "--stdin", input=content)
54 git("update-index", "--add", "--cacheinfo", f"100644,{blob},{path}")
55 tree = git("write-tree")
56 return git(
57 "commit-tree", tree,
58 *[arg for parent in parents for arg in ("-p", parent)],
59 input="Fixture commit\n",
60 )
61
62 old = commit({
63 "pyproject.toml": 'version = "1.0.0"\n',
64 "CHANGELOG.md": "# Release 1.0.0\n",
65 "skills/last30days/scripts/engine.py": "old\n",
66 })
67 release_changes = {
68 "pyproject.toml": 'version = "2.0.0"\n',
69 "CHANGELOG.md": "# Release 2.0.0\n",
70 "changelog.d/base-only.fixed.md": "An unrelated base change.\n",
71 }
72 release = commit(release_changes, old)
73 parser = tmp_path / ".github/scripts/read_manifest_version.py"
74 parser.parent.mkdir(parents=True)
75 shutil.copyfile(ROOT / ".github/scripts/read_manifest_version.py", parser)
76 workflow = yaml.safe_load((ROOT / ".github/workflows/changelog-guard.yml").read_text())
77 step = next(step for step in workflow["jobs"]["guard"]["steps"] if "run" in step)
78 script = step["run"].replace("${{ github.repository }}", "example/repository")
79
80 def run(head, *, base_ref="main", labels="", author="contributor", event_base=None):
81 git("update-ref", f"refs/remotes/origin/{base_ref}", release)
82 return subprocess.run(
83 [workflow_bash, "-c", 'gh() { printf "%s\\n" "$TEST_LABELS"; }\n' + script],
84 cwd=tmp_path, text=True, capture_output=True,
85 env={
86 **env,
87 "PATH": f"{Path(sys.executable).parent}{os.pathsep}{env['PATH']}",
88 "TEST_LABELS": labels,
89 "PR_NUMBER": "1",
90 "PR_AUTHOR": author,
91 "BASE_REF": base_ref,
92 "BASE_SHA": event_base or release,
93 "HEAD_SHA": head,
94 },
95 )
96
97 return old, release, release_changes, commit, run
98
99
100 @pytest.mark.parametrize("shape", ["behind", "merged", "rebased"])
101 @pytest.mark.parametrize("base_ref", ["main", "releases/stable"])
102 def test_base_release_is_not_attributed_to_feature(history, shape, base_ref):
103 old, release, release_changes, commit, run = history
104 changes = {
105 "skills/last30days/scripts/engine.py": "feature\n",
106 "changelog.d/feature.fixed.md": "Feature fix.\n",
107 }
108 head = commit(changes, release if shape == "rebased" else old)
109 if shape == "merged":
110 head = commit(release_changes, head, release)
111 result = run(head, base_ref=base_ref, event_base=release if shape == "behind" else old)
112 assert result.returncode == 0, result.stdout + result.stderr
113 assert "Changelog guard passed." in result.stdout
114
115
116 @pytest.mark.parametrize("labels", ["", "skip-changelog"])
117 def test_contributor_version_bump_is_rejected(history, labels):
118 _, release, _, commit, run = history
119 head = commit({"pyproject.toml": 'version = "3.0.0"\n'}, release)
120 result = run(head, labels=labels)
121 assert result.returncode != 0
122 assert "Non-release PRs must not bump lockstep version strings." in result.stdout
123
124
125 def test_base_fragment_does_not_satisfy_feature_requirement(history):
126 old, release, release_changes, commit, run = history
127 feature = commit({"skills/last30days/scripts/engine.py": "feature\n"}, old)
128 head = commit(release_changes, feature, release)
129 result = run(head, event_base=old)
130 assert result.returncode != 0
131 assert "Engine/skill changes need a changelog.d fragment" in result.stdout
132
133
134 def test_contributor_changelog_edit_is_rejected(history):
135 _, release, _, commit, run = history
136 head = commit({"CHANGELOG.md": "# Contributor release notes\n"}, release)
137 result = run(head)
138 assert result.returncode != 0
139 assert "Do not edit CHANGELOG.md in feature PRs." in result.stdout
140
141
142 @pytest.mark.parametrize(
143 ("labels", "author"), [("skip-changelog", "contributor"), ("", "dependabot[bot]")]
144 )
145 def test_fragment_exemptions_still_allow_engine_changes(history, labels, author):
146 _, release, _, commit, run = history
147 head = commit({"skills/last30days/scripts/engine.py": "feature\n"}, release)
148 result = run(head, labels=labels, author=author)
149 assert result.returncode == 0, result.stdout + result.stderr
150
151
152 def test_release_label_still_allows_version_and_changelog_changes(history):
153 _, release, _, commit, run = history
154 head = commit({
155 "pyproject.toml": 'version = "3.0.0"\n',
156 "CHANGELOG.md": "# Release 3.0.0\n",
157 }, release)
158 result = run(head, labels="release")
159 assert result.returncode == 0, result.stdout + result.stderr
160 assert "version/CHANGELOG edits allowed." in result.stdout
161
161 lines PYTHON