diff --git a/pyproject.toml b/pyproject.toml index 3de364896..38d763fc2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -187,7 +187,12 @@ itest = [ [tool.semantic_release] version_toml = ["pyproject.toml:project.version"] -build_command = "uv build" +# uv.lock also records the project's version: relock and stage it so it lands in the +# release commit. Otherwise the lock stays stale until the next push, and backport +# regeneration rejects the relock (#521). Recipe from the python-semantic-release docs. +# Chained with && so a failed relock or stage stops the release instead of being masked +# by a later successful step. +build_command = 'uv lock --upgrade-package "$PACKAGE_NAME" && git add uv.lock && uv build' commit_message = "chore(release): {version} [skip ci]" upload_to_pypi = false upload_to_release = true diff --git a/tests/acceptance/test_release_relocks.py b/tests/acceptance/test_release_relocks.py new file mode 100644 index 000000000..a9f592d0c --- /dev/null +++ b/tests/acceptance/test_release_relocks.py @@ -0,0 +1,127 @@ +"""The release commit must carry every file that records the project's version. + +python-semantic-release bumps the files in ``version_toml`` and commits them. ``uv.lock`` +also records the project's own version, so a release that leaves it out lands a stale +lock. The next ``uv sync`` then rewrites it, which the backport workflow rejects as a +change outside its generated paths (#521). + +This drives the configured ``build_command`` the way a release does: bump every +``version_toml`` target in a scratch git repository, run the command (minus the +``uv build`` step, which needs the generated package), and check that ``uv.lock`` now +records the new version and is staged for the release commit. +""" + +from __future__ import annotations + +import os +import shutil +import subprocess +import sys +from pathlib import Path + +import pytest +from packaging.version import Version + +if sys.version_info >= (3, 11): + import tomllib +else: # Python 3.10 has no tomllib; tomli is its backport. + tomllib = pytest.importorskip("tomli") + +REPO_ROOT = Path(__file__).resolve().parents[2] +PROJECT_FILES = ("pyproject.toml", "uv.lock", "README.md") + + +def _release_config() -> dict[str, object]: + with (REPO_ROOT / "pyproject.toml").open("rb") as fh: + return tomllib.load(fh)["tool"]["semantic_release"] + + +def _locked_project_version(lock: Path, name: str) -> Version: + with lock.open("rb") as fh: + packages = tomllib.load(fh)["package"] + (entry,) = [p for p in packages if p["name"] == name] + return Version(entry["version"]) + + +def _git(cwd: Path, *args: str) -> str: + return subprocess.run( + ["git", *args], cwd=cwd, check=True, capture_output=True, text=True + ).stdout + + +@pytest.mark.skipif(shutil.which("uv") is None, reason="needs uv on PATH") +def test_release_build_command_relocks_and_stages_uv_lock(tmp_path: Path) -> None: + config = _release_config() + for name in PROJECT_FILES: + shutil.copy2(REPO_ROOT / name, tmp_path / name) + _git(tmp_path, "init", "-q") + _git(tmp_path, "add", ".") + _git( + tmp_path, + "-c", + "user.name=t", + "-c", + "user.email=t@example.com", + "commit", + "-qm", + "base", + ) + + with (tmp_path / "pyproject.toml").open("rb") as fh: + project = tomllib.load(fh)["project"] + old = project["version"] + new = f"{Version(old).major + 1}.0.0" + + # Bump what python-semantic-release bumps: every version_toml target. + version_toml = config["version_toml"] + assert isinstance(version_toml, list) + for target in version_toml: + assert isinstance(target, str) + file_name, key = target.split(":", 1) + assert key == "project.version", f"unexpected version_toml target {target}" + path = tmp_path / file_name + text = path.read_text(encoding="utf-8") + needle = f'version = "{old}"' + assert text.count(needle) == 1, f"cannot find {needle} in {file_name}" + path.write_text(text.replace(needle, f'version = "{new}"'), encoding="utf-8") + + build_command = config["build_command"] + assert isinstance(build_command, str) + assert "uv build" in build_command + env = { + **os.environ, + "NEW_VERSION": new, + "PACKAGE_NAME": project["name"], + } + env.pop("VIRTUAL_ENV", None) + # Run the command as one shell script, the way the release does, with the + # ``uv build`` step stubbed out (it needs the generated package). + script = build_command.replace("uv build", "true") + subprocess.run(["sh", "-c", script], cwd=tmp_path, env=env, check=True) + + assert _locked_project_version(tmp_path / "uv.lock", project["name"]) == Version( + new + ), "build_command must relock so uv.lock records the released version" + staged = _git(tmp_path, "diff", "--cached", "--name-only").split() + assert "uv.lock" in staged, ( + "build_command must stage uv.lock so it lands in the release commit" + ) + + +@pytest.mark.parametrize("failing", ["uv lock", "git add"]) +def test_release_build_command_fails_when_a_step_fails( + tmp_path: Path, failing: str +) -> None: + """A failed relock or stage must fail the whole command, even if ``uv build`` passes.""" + build_command = _release_config()["build_command"] + assert isinstance(build_command, str) + assert failing in build_command + # Make the selected step fail (``false && : ``) and stub every other step + # to succeed, so only the selected failure can make the command fail. + script = build_command.replace("uv build", "true") + for step in ("uv lock", "git add"): + assert step in build_command + stub = "false && : " if step == failing else "true && : " + script = script.replace(f"{step} ", stub, 1) + result = subprocess.run(["sh", "-c", script], cwd=tmp_path) + assert result.returncode != 0, f"build_command must stop when {failing} fails"