Files
computer-playbook/tests/unit/utils/install/test_system_pkg.py
T
kevinveenbirkenbachandClaude Opus 4.7 92f11a6701 feat(install/lint): Python-ize lint install + host/docker dispatcher
Refactor scripts/install/lint.sh (~870 LOC bash) into a Python package
under utils/install/. scripts/install/lint.sh becomes a 7-line wrapper
that delegates to `python3 -m utils.install.lint`.

Layout:
- utils/install/{primitives,pip,npm,github_release,system_pkg}.py:
  channel-agnostic installers
- utils/install/lint/<tool>.py: one ensure() per tool (actionlint,
  shfmt, mbake, ruff, ansible_lint, galaxy_importer, markdownlint_cli2,
  eslint, shellcheck, ansible_commands, ansible_collections)
- utils/install/lint/__main__.py: CLI + group dispatch + stamp logic

INFINITO_LINT_RUNNER dispatch:
- New env var (host | docker, default docker), tracked in env/static.env
  + passthrough handler + drift test.
- scripts/install/wrapper.sh routes `make install-lint` to host or
  container based on the runner.
- scripts/lint/wrapper.sh already dispatched; container branch now uses
  bash (not --login) so /etc/profile does not reset PATH, and forwards
  BASH_ENV so the .env keys land in the lint script's env.

Stamp logic:
- Per-interpreter stamp at build/install-lint-<sha256(sys.executable)>.stamp
  so host venv and container venv track installs independently despite
  bind-mounted build/.
- `--force` drops the stamp; group args bypass the stamp.

Tests + docs:
- 43 unit tests under tests/unit/utils/install/{,lint/} cover primitives,
  pip/npm/github_release/system_pkg fallback chains, and the main CLI
  (stamp logic, group dispatch, error path).
- utils/install/README.md + utils/install/lint/README.md document the
  structure and the per-tool ensure() contract.
- docs/contributing/tools/make.md install-lint row updated to mention
  the runner dispatch + per-env stamp + install-lint-force.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 20:40:34 +02:00

74 lines
2.8 KiB
Python

"""Unit tests for :mod:`utils.install.system_pkg`."""
from __future__ import annotations
import subprocess
import unittest
from unittest import mock
from utils.install import system_pkg
class TestDetectPackageManager(unittest.TestCase):
def test_detects_first_available(self) -> None:
with mock.patch.object(
system_pkg.shutil,
"which",
side_effect=lambda x: "/usr/bin/dnf" if x == "dnf" else None,
):
self.assertEqual(system_pkg.detect_package_manager(), "dnf")
def test_raises_when_none(self) -> None:
with mock.patch.object(system_pkg.shutil, "which", return_value=None):
self.assertRaises(RuntimeError, system_pkg.detect_package_manager)
class TestInstallPackageCandidates(unittest.TestCase):
def test_pacman_succeeds_first_candidate(self) -> None:
with mock.patch.object(system_pkg, "run_privileged") as priv:
system_pkg.install_package_candidates("pacman", ["ansible-core", "ansible"])
# Only one call: the first candidate succeeded.
self.assertEqual(len(priv.call_args_list), 1)
self.assertEqual(priv.call_args_list[0].args[0][0], "pacman")
def test_apt_updates_then_installs(self) -> None:
with mock.patch.object(system_pkg, "run_privileged") as priv:
system_pkg.install_package_candidates("apt-get", ["shfmt"])
commands = [c.args[0] for c in priv.call_args_list]
self.assertEqual(commands[0][:2], ["apt-get", "update"])
self.assertEqual(commands[1][:2], ["apt-get", "install"])
def test_raises_when_all_candidates_fail(self) -> None:
err = subprocess.CalledProcessError(returncode=1, cmd=["pacman"])
with mock.patch.object(system_pkg, "run_privileged", side_effect=err):
self.assertRaises(
RuntimeError,
system_pkg.install_package_candidates,
"pacman",
["ansible-core", "ansible"],
)
class TestInstallCommandViaPkg(unittest.TestCase):
def test_dispatches_ansible_playbook(self) -> None:
with (
mock.patch.object(
system_pkg, "detect_package_manager", return_value="pacman"
),
mock.patch.object(system_pkg, "install_package_candidates") as cand,
):
system_pkg.install_command_via_pkg("ansible-playbook")
cand.assert_called_once_with("pacman", ["ansible-core", "ansible"])
def test_unknown_command_raises(self) -> None:
with mock.patch.object(
system_pkg, "detect_package_manager", return_value="pacman"
):
self.assertRaises(
RuntimeError, system_pkg.install_command_via_pkg, "no-such-tool"
)
if __name__ == "__main__":
unittest.main()