"""Tests for the publish script: guarded PyPI release (publish spec). No network, no git state, no real build: every guard and step runs through an injected runner and an injectable PyPI status fetch (daedalus agent rule 5.6, publish spec item 6.1). """ import re from daedalus.publish import 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 clean_runner(version, calls=None): """A runner with all guards green for `version`.""" py = publish.PY return make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{version}\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) # --- pyproject discovery (publish spec items 3.1-3.2) def test_read_project_from_this_repo(): """The script's own repo resolves: name daedalus, semver version.""" root = publish.find_repo_root() assert root is not None name, version = publish.read_project(root / "pyproject.toml") assert name == "daedalus" assert re.fullmatch(r"\d+\.\d+\.\d+", version) # --- guards (publish spec item 3.3) def test_dirty_tree_stops_before_building(): """A dirty tree is exit 3; no build runs.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") 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_mismatch_stops(): """HEAD tagged with another version is exit 4.""" calls = [] runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, "v0.0.1\n"), }, calls) args = publish.parse_args([]) assert publish.cmd_publish(args, runner, lambda _u: 404) == 4 def test_tag_probe_failure_stops(): """An untagged HEAD (no tag points at it) is exit 4.""" runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (1, ""), }) assert publish.cmd_publish(publish.parse_args([]), runner, lambda _u: 404) == 4 def test_moving_tags_on_the_same_commit_do_not_confuse_the_guard(): """`latest` sharing the release commit is fine (item 3.3.2). git's own name picking is not the contract; v among the tags that point at HEAD is. Live case: git prints `latest` for a commit tagged both latest and v. """ calls = [] runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"latest\nv{repo_version()}\n"), (publish.PY, "-m", "build", "--version"): (0, "build\n"), (publish.PY, "-m", "twine", "--version"): (0, "twine\n"), (publish.PY, "-m", "build"): (0, "ok\n"), (publish.PY, "-m", "twine", "check"): (0, "PASSED\n"), }, calls) assert publish.cmd_publish(publish.parse_args([]), runner, lambda _u: 404) == 0 tag_calls = [cmd for cmd, _ in calls if cmd[:2] == ["git", "tag"]] assert tag_calls == [["git", "tag", "--points-at", "HEAD"]] def repo_version(): """The version the checkout's pyproject carries.""" _name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") return version def test_tooling_missing_stops(): """No build/twine in the interpreter is exit 5.""" runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), (publish.PY, "-m", "build", "--version"): (127, "no module"), (publish.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 = [] runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), (publish.PY, "-m", "build", "--version"): (0, "build\n"), (publish.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, _ in calls) def test_pypi_unreachable_stops(): """A blind publish is refused: unreachable PyPI is exit 6.""" runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{repo_version()}\n"), (publish.PY, "-m", "build", "--version"): (0, "build\n"), (publish.PY, "-m", "twine", "--version"): (0, "twine\n"), }) assert publish.cmd_publish(publish.parse_args([]), runner, lambda _u: None) == 6 # --- build, check, upload (publish spec items 3.4-3.7) def test_check_only_flow_builds_and_stops(tmp_path, capsys): """Without --upload: chained build, twine check, exit 0.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") calls = [] assert publish.cmd_publish(publish.parse_args([]), clean_runner(version, calls), lambda _u: 404) == 0 build = [cmd for cmd, _ in calls if cmd[1:3] == ["-m", "build"] and len(cmd) == 3] assert len(build) == 1 # chained, not two runs assert not any(cmd[3:4] == ["upload"] for cmd, _ in calls) out = capsys.readouterr().out assert f"{name}-{version}.tar.gz" in out assert "--upload" in out # the next-step hint def test_upload_sends_exactly_this_version(): """--upload passes only this version's sdist and wheel.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") calls = [] assert publish.cmd_publish(publish.parse_args(["--upload"]), clean_runner(version, calls), lambda _u: 404) == 0 upload = [cmd for cmd, _ in calls if cmd[3:4] == ["upload"]][0] assert upload[-2] == f"dist/{name}-{version}.tar.gz" assert upload[-1].startswith(f"dist/{name}-{version}-") assert upload[-1].endswith(".whl") def test_build_failure_is_7(): """A failed chained build is exit 7.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") py = publish.PY runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{version}\n"), (py, "-m", "build", "--version"): (0, "build\n"), (py, "-m", "twine", "--version"): (0, "twine\n"), (py, "-m", "build"): (1, "error\n"), }) assert publish.cmd_publish(publish.parse_args([]), runner, lambda _u: 404) == 7 def test_check_failure_is_8(): """twine check rejecting the artifacts is exit 8.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") py = publish.PY runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{version}\n"), (py, "-m", "build", "--version"): (0, "build\n"), (py, "-m", "twine", "--version"): (0, "twine\n"), (py, "-m", "build"): (0, "ok\n"), (py, "-m", "twine", "check"): (1, "Failed\n"), }) assert publish.cmd_publish(publish.parse_args([]), runner, lambda _u: 404) == 8 def test_upload_failure_is_9(): """twine upload failing is exit 9.""" name, version = publish.read_project( publish.find_repo_root() / "pyproject.toml") py = publish.PY runner = make_runner({ ("git", "status"): (0, ""), ("git", "tag", "--points-at"): (0, f"v{version}\n"), (py, "-m", "build", "--version"): (0, "build\n"), (py, "-m", "twine", "--version"): (0, "twine\n"), (py, "-m", "build"): (0, "ok\n"), (py, "-m", "twine", "check"): (0, "PASSED\n"), (py, "-m", "twine", "upload"): (1, "403\n"), }) assert publish.cmd_publish(publish.parse_args(["--upload"]), runner, lambda _u: 404) == 9 def test_cli_wires_flow(monkeypatch): """The CLI hands cmd_publish the real runner and fetch.""" seen = {} def fake_publish(args, run_cmd, fetch_status): seen["args"] = args seen["run_cmd"] = run_cmd seen["fetch"] = fetch_status return 0 monkeypatch.setattr(publish, "cmd_publish", fake_publish) assert publish.main(["--upload"]) == 0 assert seen["args"].upload is True assert seen["run_cmd"] is publish.run_in_cwd assert seen["fetch"] is publish.pypi_status