Files
pkgmgr/tests/unit/pkgmgr/cli/commands/test_release.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 208a8c3ace refactor(release): bundle the options and refuse untracked release inputs
quality, 7 findings: _release_impl and release take one frozen ReleaseOptions (new options.py, exported from pkgmgr.actions.release) and split into small private helpers with a frozen _Prepared carrier; the three _release_impl call paths become one plus dataclasses.replace(preview=True) for the interactive preview. cli/commands/release.py and cli/parser/release_cmd.py split into helpers with arguments in the same order, so the CLI surface is unchanged; test_workflow.py uses one with-block instead of five patch decorators. CodeRabbit: with --expect-tree an existing but untracked release input (pyproject, CHANGELOG.md, flake.nix, PKGBUILD, spec, debian/changelog, Cargo files) aborts before any write; the README says the abort comes before any release file is written and the pull is not reverted.

| 🌿 Branch | 📊 Diff stat | 🎯 Fix | 💥 Break | 💸 Tokens | ⏱️ Velocity | 🛡️ Security | 🏆 Score | Gate |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| feature/release-expect-tree | 13 files +457/-342 | 91 % | 4 % | – | – | 100 % | 87 | ✅ 90/5, tokens and velocity <= 100, security 100 % |

Evidence: tests/unit + tests/integration with PYTHONPATH=src 886 passed; 2 known gpg-in-sandbox failures in test_publish_integration. quality check --staged 0 findings, ruff clean, MEDUSA 100 %, critical 0. Hub review e12f7e62ce35b6f5.

Refs https://github.com/kevinveenbirkenbach/package-manager/pull/19

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 23:33:49 +00:00

212 lines
6.5 KiB
Python

"""
Unit tests for pkgmgr.cli.commands.release.
These tests focus on the wiring layer:
- Argument handling for the release command as defined by the
top-level parser (cli.parser.create_parser).
- Correct invocation of pkgmgr.actions.release.release(...) for the
selected repositories.
- Behaviour of --preview, --list, --close, and -f/--force.
"""
from __future__ import annotations
import argparse
import unittest
from types import SimpleNamespace
from unittest.mock import call, patch
from pkgmgr.actions.release import ReleaseOptions
class TestReleaseCommand(unittest.TestCase):
"""
Tests for the `pkgmgr release` CLI wiring.
"""
def _make_ctx(self, all_repos: list[dict]) -> SimpleNamespace:
"""
Create a minimal CLIContext-like object for tests.
Only the attributes that handle_release() uses are provided.
"""
return SimpleNamespace(
config_merged={},
repositories_base_dir="/base/dir",
all_repositories=all_repos,
binaries_dir="/bin",
user_config_path="/tmp/config.yaml",
)
def _parse_release_args(self, argv: list[str]) -> argparse.Namespace:
"""
Build a real top-level parser and parse the given argv list
to obtain the Namespace for the `release` command.
"""
from pkgmgr.cli.parser import create_parser
parser = create_parser("test parser")
args = parser.parse_args(argv)
self.assertEqual(args.command, "release")
return args
@patch("pkgmgr.cli.commands.release.os.path.isdir", return_value=True)
@patch("pkgmgr.cli.commands.release.run_release")
@patch("pkgmgr.cli.commands.release.get_repo_dir")
@patch("pkgmgr.cli.commands.release.get_repo_identifier")
@patch("pkgmgr.cli.commands.release.os.chdir")
@patch("pkgmgr.cli.commands.release.os.getcwd", return_value="/cwd")
def test_release_with_close_and_message(
self,
mock_getcwd,
mock_chdir,
mock_get_repo_identifier,
mock_get_repo_dir,
mock_run_release,
mock_isdir,
) -> None:
"""
The release handler should call pkgmgr.actions.release.release() with:
- release_type (e.g. minor)
- provided message
- preview flag
- force flag
- close flag
It must change into the repository directory and then back.
"""
from pkgmgr.cli.commands.release import handle_release
repo = {"name": "dummy-repo"}
selected = [repo]
ctx = self._make_ctx(selected)
mock_get_repo_identifier.return_value = "dummy-id"
mock_get_repo_dir.return_value = "/repos/dummy"
argv = [
"release",
"minor",
"dummy-id",
"-m",
"Close branch after minor release",
"--close",
"-f",
]
args = self._parse_release_args(argv)
handle_release(args, ctx, selected)
# We should have changed into the repo dir and then back.
mock_chdir.assert_has_calls([call("/repos/dummy"), call("/cwd")])
# And run_release should be invoked once with the expected parameters.
mock_run_release.assert_called_once_with(
ReleaseOptions(
pyproject_path="pyproject.toml",
changelog_path="CHANGELOG.md",
release_type="minor",
message="Close branch after minor release",
preview=False,
force=True,
close=True,
retry=False,
expect_tree=None,
)
)
@patch("pkgmgr.cli.commands.release.os.path.isdir", return_value=True)
@patch("pkgmgr.cli.commands.release.run_release")
@patch("pkgmgr.cli.commands.release.get_repo_dir")
@patch("pkgmgr.cli.commands.release.get_repo_identifier")
@patch("pkgmgr.cli.commands.release.os.chdir")
@patch("pkgmgr.cli.commands.release.os.getcwd", return_value="/cwd")
def test_release_preview_mode(
self,
mock_getcwd,
mock_chdir,
mock_get_repo_identifier,
mock_get_repo_dir,
mock_run_release,
mock_isdir,
) -> None:
"""
In preview mode, the handler should pass preview=True to the
release helper and force=False by default.
"""
from pkgmgr.cli.commands.release import handle_release
repo = {"name": "dummy-repo"}
selected = [repo]
ctx = self._make_ctx(selected)
mock_get_repo_identifier.return_value = "dummy-id"
mock_get_repo_dir.return_value = "/repos/dummy"
argv = [
"release",
"patch",
"dummy-id",
"--preview",
"--expect-tree",
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
]
args = self._parse_release_args(argv)
handle_release(args, ctx, selected)
mock_run_release.assert_called_once_with(
ReleaseOptions(
pyproject_path="pyproject.toml",
changelog_path="CHANGELOG.md",
release_type="patch",
message=None,
preview=True,
force=False,
close=False,
retry=False,
expect_tree="4b825dc642cb6eb9a060e54bf8d69288fbee4904",
)
)
@patch("pkgmgr.cli.commands.release.run_release")
@patch("pkgmgr.cli.commands.release.get_repo_dir")
@patch("pkgmgr.cli.commands.release.get_repo_identifier")
def test_release_list_mode_does_not_invoke_helper(
self,
mock_get_repo_identifier,
mock_get_repo_dir,
mock_run_release,
) -> None:
"""
When --list is provided, the handler should print the list of affected
repositories and must NOT invoke run_release().
"""
from pkgmgr.cli.commands.release import handle_release
repo1 = {"name": "repo-1"}
repo2 = {"name": "repo-2"}
selected = [repo1, repo2]
ctx = self._make_ctx(selected)
mock_get_repo_identifier.side_effect = ["id-1", "id-2"]
argv = [
"release",
"major",
"--list",
]
args = self._parse_release_args(argv)
handle_release(args, ctx, selected)
mock_run_release.assert_not_called()
self.assertEqual(
mock_get_repo_identifier.call_args_list,
[call(repo1, selected), call(repo2, selected)],
)
if __name__ == "__main__":
unittest.main()