mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-08 03:38:23 +00:00
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>
79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
"""Unit tests for :mod:`utils.install.primitives`."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
from utils.install import primitives
|
|
|
|
|
|
class TestRunPrivileged(unittest.TestCase):
|
|
def test_runs_directly_as_root(self) -> None:
|
|
with (
|
|
mock.patch.object(primitives.os, "geteuid", return_value=0),
|
|
mock.patch.object(primitives.subprocess, "run") as run,
|
|
):
|
|
primitives.run_privileged(["apt-get", "update"])
|
|
run.assert_called_once_with(["apt-get", "update"], check=True)
|
|
|
|
def test_prepends_sudo_for_non_root(self) -> None:
|
|
with (
|
|
mock.patch.object(primitives.os, "geteuid", return_value=1000),
|
|
mock.patch.object(primitives.shutil, "which", return_value="/usr/bin/sudo"),
|
|
mock.patch.object(primitives.subprocess, "run") as run,
|
|
):
|
|
primitives.run_privileged(["apt-get", "update"])
|
|
run.assert_called_once_with(["sudo", "apt-get", "update"], check=True)
|
|
|
|
def test_no_sudo_when_unavailable(self) -> None:
|
|
with (
|
|
mock.patch.object(primitives.os, "geteuid", return_value=1000),
|
|
mock.patch.object(primitives.shutil, "which", return_value=None),
|
|
mock.patch.object(primitives.subprocess, "run") as run,
|
|
):
|
|
primitives.run_privileged(["echo", "hi"])
|
|
run.assert_called_once_with(["echo", "hi"], check=True)
|
|
|
|
|
|
class TestEnsureDirOnPath(unittest.TestCase):
|
|
def test_prepends_when_absent(self) -> None:
|
|
with mock.patch.dict(os.environ, {"PATH": "/usr/bin:/bin"}, clear=False):
|
|
primitives.ensure_dir_on_path("/opt/bin")
|
|
self.assertTrue(os.environ["PATH"].startswith("/opt/bin" + os.pathsep))
|
|
|
|
def test_idempotent_when_present(self) -> None:
|
|
with mock.patch.dict(os.environ, {"PATH": "/opt/bin:/usr/bin"}, clear=False):
|
|
primitives.ensure_dir_on_path("/opt/bin")
|
|
self.assertEqual(os.environ["PATH"], "/opt/bin:/usr/bin")
|
|
|
|
def test_empty_directory_noop(self) -> None:
|
|
original = "/usr/bin"
|
|
with mock.patch.dict(os.environ, {"PATH": original}, clear=False):
|
|
primitives.ensure_dir_on_path("")
|
|
self.assertEqual(os.environ["PATH"], original)
|
|
|
|
|
|
class TestInstallWithOptionalSudo(unittest.TestCase):
|
|
def test_succeeds_without_sudo(self) -> None:
|
|
with mock.patch.object(primitives.subprocess, "run") as run:
|
|
primitives.install_with_optional_sudo(["install", "-d", "/tmp/x"])
|
|
run.assert_called_once_with(["install", "-d", "/tmp/x"], check=True)
|
|
|
|
def test_retries_with_sudo_on_failure(self) -> None:
|
|
err = primitives.subprocess.CalledProcessError(returncode=1, cmd=["install"])
|
|
with (
|
|
mock.patch.object(
|
|
primitives.subprocess, "run", side_effect=err
|
|
) as plain_run,
|
|
mock.patch.object(primitives, "run_privileged") as privileged,
|
|
):
|
|
primitives.install_with_optional_sudo(["install", "-d", "/usr/local/bin"])
|
|
plain_run.assert_called_once()
|
|
privileged.assert_called_once_with(["install", "-d", "/usr/local/bin"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|