Files
phronesis/test/test_publish.py
nvaldez c0d756c4e7 phronesis v0.4.0-dev: publish.py - guarded PyPI release, ported from daedalus
- publish.py at repo root: guards (clean tree, v<version> tag on HEAD,
  build+twine present, version free on PyPI), chained sdist->wheel build,
  twine check, exact-version twine upload; exit codes 2-9 per guard
- tag guard accepts roller tags: uses git tag --points-at HEAD so
  stable/latest beside v<version> do not trip it
- MANIFEST.in ships skill/ in the sdist
- test/test_publish.py: 12 tests via injected runner + fetch, no network
- requirements.txt: build, twine

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-19 10:35:59 +01:00

194 lines
7.0 KiB
Python

"""Tests for publish.py: guarded PyPI release (ported from daedalus).
No network, no git state, no real build: every guard and step runs
through an injected runner and an injectable PyPI status fetch.
"""
import importlib.util
import pathlib
import re
REPO_ROOT = pathlib.Path(__file__).resolve().parent.parent
_SPEC = importlib.util.spec_from_file_location(
"publish", REPO_ROOT / "publish.py")
publish = importlib.util.module_from_spec(_SPEC)
_SPEC.loader.exec_module(publish)
def make_runner(script, calls=None):
"""Build an injectable run_cmd(cmd, cwd) from {prefix: (code, out)}."""
def run_cmd(cmd, cwd=None):
if calls is not None:
calls.append((list(cmd), cwd))
for prefix, reply in script.items():
if list(cmd)[:len(prefix)] == list(prefix):
return reply
raise AssertionError(f"unexpected command: {cmd}")
return run_cmd
def repo_version():
"""The version the checkout's pyproject carries."""
_name, version = publish.read_project(
publish.find_repo_root() / "pyproject.toml")
return version
def clean_runner(version, calls=None):
"""A runner with every guard green for `version` (plus roller tags)."""
py = publish.PY
return make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"stable\nv{version}\nlatest\n"),
(py, "-m", "build", "--version"): (0, "build 1.6.1\n"),
(py, "-m", "twine", "--version"): (0, "twine 7.0.0\n"),
(py, "-m", "build"): (0, "Successfully built\n"),
(py, "-m", "twine", "check"): (0, "PASSED\n"),
(py, "-m", "twine", "upload"): (0, "Uploaded\n"),
}, calls)
def test_read_project_from_this_repo():
"""The script's own repo resolves: name phronesis, semver version."""
root = publish.find_repo_root()
assert root is not None
name, version = publish.read_project(root / "pyproject.toml")
assert name == "phronesis"
assert re.fullmatch(r"\d+\.\d+\.\d+", version)
def test_dirty_tree_stops_before_building():
"""A dirty tree is exit 3; no build runs."""
calls = []
runner = make_runner({("git", "status"): (0, " M file\n")}, calls)
args = publish.parse_args([])
assert publish.cmd_publish(args, runner,
lambda _u: 404) == 3
assert not any(cmd[:2] == ["git", "tag"]
or cmd[1:3] == ["-m", "build"]
for cmd, _cwd in calls)
def test_tag_missing_stops():
"""A HEAD without v<version> among its tags is exit 4."""
calls = []
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, "stable\nlatest\n"),
}, calls)
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 404) == 4
def test_tag_probe_failure_stops():
"""A failing tag probe is exit 4."""
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (128, "no tags\n"),
})
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 404) == 4
def test_roller_tags_do_not_confuse_the_guard():
"""v<version> among stable/latest on the same HEAD passes the guard."""
assert publish.cmd_publish(publish.parse_args([]),
clean_runner(repo_version()),
lambda _u: 404) == 0
def test_tooling_missing_stops():
"""No build/twine in the interpreter is exit 5."""
py = publish.PY
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"),
(py, "-m", "build", "--version"): (127, "no module"),
(py, "-m", "twine", "--version"): (0, "twine\n"),
})
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 404) == 5
def test_already_on_pypi_stops():
"""A published version is exit 6 before any artifact is built."""
calls = []
py = publish.PY
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"),
(py, "-m", "build", "--version"): (0, "build\n"),
(py, "-m", "twine", "--version"): (0, "twine\n"),
}, calls)
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 200) == 6
assert not any(cmd[1:3] == ["-m", "build"] and len(cmd) == 3
for cmd, _cwd in calls)
def test_unreachable_pypi_stops():
"""An unreachable PyPI is exit 6 (never publish blind)."""
py = publish.PY
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"),
(py, "-m", "build", "--version"): (0, "build\n"),
(py, "-m", "twine", "--version"): (0, "twine\n"),
})
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: None) == 6
def test_build_failure_is_seven():
"""A failed build is exit 7."""
py = publish.PY
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"),
(py, "-m", "build", "--version"): (0, "build\n"),
(py, "-m", "twine", "--version"): (0, "twine\n"),
(py, "-m", "build"): (1, "boom"),
})
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 404) == 7
def test_twine_check_failure_is_eight():
"""A failed twine check is exit 8."""
py = publish.PY
runner = make_runner({
("git", "status"): (0, ""),
("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"),
(py, "-m", "build", "--version"): (0, "build\n"),
(py, "-m", "twine", "--version"): (0, "twine\n"),
(py, "-m", "build"): (0, "built\n"),
(py, "-m", "twine", "check"): (1, "README failed"),
})
assert publish.cmd_publish(publish.parse_args([]), runner,
lambda _u: 404) == 8
def test_check_without_upload_never_uploads():
"""Without --upload the flow stops after the check (exit 0)."""
calls = []
assert publish.cmd_publish(publish.parse_args([]),
clean_runner(repo_version(), calls),
lambda _u: 404) == 0
assert not any(cmd[1:4] == ["-m", "twine", "upload"]
for cmd, _cwd in calls)
def test_upload_selects_exactly_this_version():
"""--upload twine-ups exactly the version's sdist and wheel."""
calls = []
version = repo_version()
assert publish.cmd_publish(publish.parse_args(["--upload"]),
clean_runner(version, calls),
lambda _u: 404) == 0
upload = [cmd for cmd, _cwd in calls
if cmd[1:4] == ["-m", "twine", "upload"]]
assert len(upload) == 1
sdist, wheel = publish.artifact_names("phronesis", version)
assert upload[0][-2:] == [f"dist/{sdist}", f"dist/{wheel}"]