Skip to content

Commit dabab54

Browse files
authored
Merge pull request #979 from PolicyEngine/fix/versioning-script-import
Fix publication versioning script imports
2 parents 290f691 + 078b151 commit dabab54

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

.github/bump_version.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import sys
66
from pathlib import Path
77

8-
from policyengine_us_data.utils.run_context import (
8+
_REPO_ROOT = Path(__file__).resolve().parent.parent
9+
if str(_REPO_ROOT) not in sys.path:
10+
sys.path.insert(0, str(_REPO_ROOT))
11+
12+
from policyengine_us_data.utils.run_context import ( # noqa: E402
913
build_candidate_scope,
1014
release_version_from_bump,
1115
)
@@ -63,9 +67,8 @@ def write_publication_scope(path: Path, payload: dict[str, str]) -> None:
6367

6468

6569
def main():
66-
root = Path(__file__).resolve().parent.parent
67-
pyproject = root / "pyproject.toml"
68-
changelog_dir = root / "changelog.d"
70+
pyproject = _REPO_ROOT / "pyproject.toml"
71+
changelog_dir = _REPO_ROOT / "changelog.d"
6972

7073
current = get_current_version(pyproject)
7174
bump = infer_bump(changelog_dir)
@@ -78,7 +81,7 @@ def main():
7881
print(f"Would release as at build time: {would_release_as}")
7982

8083
write_publication_scope(
81-
root / PUBLICATION_SCOPE_PATH,
84+
_REPO_ROOT / PUBLICATION_SCOPE_PATH,
8285
{
8386
"base_release_version": current,
8487
"release_bump": bump,

changelog.d/978.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed publication candidate versioning when the GitHub Actions script is invoked directly from `.github/`.

tests/unit/test_publication_scripts.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import importlib.util
44
import json
5+
import os
6+
import shutil
7+
import subprocess
58
import sys
69
import types
710
from pathlib import Path
@@ -61,6 +64,52 @@ def test_bump_version_computes_candidate_scope_without_mutating_pyproject(
6164
assert module.infer_bump(changelog_dir) == "minor"
6265

6366

67+
def test_bump_version_script_runs_from_github_directory_without_installed_package(
68+
tmp_path,
69+
):
70+
repo = tmp_path / "repo"
71+
script_dir = repo / ".github"
72+
package_utils_dir = repo / "policyengine_us_data" / "utils"
73+
changelog_dir = repo / "changelog.d"
74+
script_dir.mkdir(parents=True)
75+
package_utils_dir.mkdir(parents=True)
76+
changelog_dir.mkdir()
77+
shutil.copyfile(
78+
REPO_ROOT / ".github" / "bump_version.py", script_dir / "bump_version.py"
79+
)
80+
shutil.copyfile(
81+
REPO_ROOT / "policyengine_us_data" / "utils" / "run_context.py",
82+
package_utils_dir / "run_context.py",
83+
)
84+
shutil.copyfile(
85+
REPO_ROOT / "policyengine_us_data" / "utils" / "canonical_json.py",
86+
package_utils_dir / "canonical_json.py",
87+
)
88+
(repo / "policyengine_us_data" / "__init__.py").write_text("")
89+
(package_utils_dir / "__init__.py").write_text("")
90+
_write_pyproject(repo, "1.73.0")
91+
(changelog_dir / "123.added").write_text("Added a thing.\n")
92+
env = os.environ.copy()
93+
env.pop("PYTHONPATH", None)
94+
95+
result = subprocess.run(
96+
[sys.executable, str(script_dir / "bump_version.py")],
97+
cwd=repo,
98+
env=env,
99+
text=True,
100+
capture_output=True,
101+
check=False,
102+
)
103+
104+
assert result.returncode == 0, result.stderr
105+
assert json.loads((script_dir / "publication_scope.json").read_text()) == {
106+
"base_release_version": "1.73.0",
107+
"candidate_scope": "1.73.0-minor",
108+
"release_bump": "minor",
109+
"would_release_as_at_build_time": "1.74.0",
110+
}
111+
112+
64113
def test_fetch_publication_scope_prints_requested_field(
65114
tmp_path,
66115
monkeypatch,

0 commit comments

Comments
 (0)