headroom/tests/test_cli_update.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

178 lines
6.6 KiB
Python
Raw Normal View History

feat(cli): add headroom update command and release banner (#1088) ## Description Adds a `headroom update` self-update command and a passive "update available" banner, so users no longer need to remember the right `pip`/`pipx`/`uv` incantation for their environment, and long-running proxies get nudged when they drift behind a release. Closes #1087 ## Type of Change - [x] New feature (non-breaking change that adds functionality) - [x] Documentation update ## Changes Made - `headroom/cli/update.py` — `headroom update` command. `detect_install_method()` resolves the install (git checkout, editable, Docker, pipx, uv tool, venv/conda, `pip --user`, externally-managed system Python per PEP 668, writable global) and builds the matching upgrade. pip path always uses `sys.executable -m pip` so it can't touch the wrong interpreter. Refuses with guidance where self-update is unsafe. Flags: `--check`, `--yes`, `--pre`, `--extras`. - `headroom/update_check.py` — best-effort PyPI check (stdlib `urllib`, no new dep). Split into a daemon-thread probe that caches to `~/.headroom/update_check.json` (≤ once/day) and a cache-only `format_update_notice()`. Opt-out `HEADROOM_UPDATE_CHECK=off`; skipped in `--stateless`, CI, Docker, checkouts. - `headroom/cli/main.py`, `headroom/cli/__init__.py` — register `update`; fire the background check from the group callback (skipped for `update`). - `headroom/cli/proxy.py` — render the one-line notice after the startup banner (best-effort, never blocks). - `README.md` — "Updating" section + opt-out env var. - Tests: `tests/test_update_check.py`, `tests/test_cli_update.py`. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ pytest tests/test_update_check.py tests/test_cli_update.py -q 42 passed in 1.63s $ ruff check headroom/cli/update.py headroom/update_check.py headroom/cli/main.py All checks passed! $ mypy headroom/update_check.py headroom/cli/update.py Success: no issues found in 2 source files ``` ## Real Behavior Proof - Environment: macOS, Python 3.11, source checkout - Exact command / steps: `python -m headroom.cli update --help`; `detect_install_method()` in the checkout - Observed result: command + flags render; in a checkout `detect_install_method()` returns `kind=checkout, can_self_update=False` ("update with `git pull`") and `format_update_notice()` returns `None` (dev tree not nagged) - Not tested: live PyPI fetch and a real pipx/uv-tool upgrade on this machine (covered by unit tests with mocked `urllib`/`subprocess`) ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] I have updated the CHANGELOG.md if applicable ## Additional Notes - CHANGELOG.md is release-please-managed, so it is intentionally not hand-edited (N/A above). - Update check uses stdlib `urllib` because `httpx` lives only in the `[proxy]` extra — the base CLI must stay dependency-light.
2026-06-18 18:22:20 +02:00
"""Tests for the `headroom update` command + install-method detection."""
from __future__ import annotations
import sys
import pytest
from click.testing import CliRunner
from headroom.cli import update as up
from headroom.cli.main import main
@pytest.fixture(autouse=True)
def _clean_env(tmp_path, monkeypatch):
monkeypatch.setenv("HEADROOM_WORKSPACE_DIR", str(tmp_path))
monkeypatch.delenv("PIPX_HOME", raising=False)
monkeypatch.delenv("UV_TOOL_DIR", raising=False)
monkeypatch.delenv("CONDA_PREFIX", raising=False)
# Default: not a checkout / editable / docker / managed install.
monkeypatch.setattr(up, "_is_source_checkout", lambda: False)
monkeypatch.setattr(up, "_is_editable_install", lambda: False)
monkeypatch.setattr(up, "_in_docker", lambda: False)
monkeypatch.setattr(up, "_is_externally_managed", lambda: False)
# --------------------------------------------------------------------------- #
# detect_install_method
# --------------------------------------------------------------------------- #
def test_detect_checkout(monkeypatch):
monkeypatch.setattr(up, "_is_source_checkout", lambda: True)
m = up.detect_install_method()
assert m.kind == "checkout" and m.can_self_update is False and "git pull" in m.guidance
def test_detect_editable(monkeypatch):
monkeypatch.setattr(up, "_is_editable_install", lambda: True)
m = up.detect_install_method()
assert m.kind == "editable" and m.can_self_update is False
def test_detect_docker(monkeypatch):
monkeypatch.setattr(up, "_in_docker", lambda: True)
m = up.detect_install_method()
assert m.kind == "docker" and m.can_self_update is False
def test_detect_pipx_by_path(monkeypatch):
monkeypatch.setattr(up.sys, "prefix", "/home/u/.local/pipx/venvs/headroom-ai")
m = up.detect_install_method()
assert m.kind == "pipx" and m.argv == ["pipx", "upgrade", "headroom-ai"]
def test_detect_pipx_windows_path(monkeypatch):
monkeypatch.setattr(up.sys, "prefix", r"C:\\Users\\u\\pipx\\venvs\\headroom-ai")
m = up.detect_install_method()
assert m.kind == "pipx"
def test_detect_uv_tool(monkeypatch):
monkeypatch.setattr(up.sys, "prefix", "/home/u/.local/share/uv/tools/headroom-ai")
m = up.detect_install_method()
assert m.kind == "uv-tool" and m.argv == ["uv", "tool", "upgrade", "headroom-ai"]
def test_detect_venv_uses_current_interpreter(monkeypatch):
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
m = up.detect_install_method()
assert m.kind == "pip"
assert m.argv[:4] == [sys.executable, "-m", "pip", "install"]
assert "-U" in m.argv and "headroom-ai" in m.argv
def test_detect_venv_with_extras(monkeypatch):
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
m = up.detect_install_method(extras="all")
assert "headroom-ai[all]" in m.argv
def test_detect_user_site(monkeypatch):
monkeypatch.setattr(up, "_in_virtualenv", lambda: False)
monkeypatch.setattr(up, "_package_location", lambda: "/home/u/.local/site")
monkeypatch.setattr(up, "_is_user_site_install", lambda loc: True)
m = up.detect_install_method()
assert m.kind == "pip-user" and "--user" in m.argv
def test_detect_externally_managed_refuses(monkeypatch):
monkeypatch.setattr(up, "_in_virtualenv", lambda: False)
monkeypatch.setattr(up, "_is_user_site_install", lambda loc: False)
monkeypatch.setattr(up, "_is_externally_managed", lambda: True)
m = up.detect_install_method()
assert m.kind == "system" and m.can_self_update is False
assert "PEP 668" in m.guidance
# --------------------------------------------------------------------------- #
# `headroom update` command
# --------------------------------------------------------------------------- #
def test_update_already_current(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.26.0")
res = CliRunner().invoke(main, ["update"])
assert res.exit_code == 0
assert "up to date" in res.output
def test_update_check_reports_command_without_running(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
def _no_run(*a, **k):
raise AssertionError("subprocess.run must not be called with --check")
monkeypatch.setattr(up.subprocess, "run", _no_run)
res = CliRunner().invoke(main, ["update", "--check"])
assert res.exit_code == 0
assert "Update available: 0.26.0 → 0.27.0" in res.output
assert "pip" in res.output and "install" in res.output
def test_update_runs_upgrade_with_yes(monkeypatch):
calls = {}
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
class _Result:
returncode = 0
def _run(argv, *a, **k):
calls["argv"] = argv
return _Result()
monkeypatch.setattr(up.subprocess, "run", _run)
res = CliRunner().invoke(main, ["update", "--yes"])
assert res.exit_code == 0
assert calls["argv"][:4] == [sys.executable, "-m", "pip", "install"]
assert "upgraded to 0.27.0" in res.output
def test_update_refuses_in_checkout(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_is_source_checkout", lambda: True)
def _no_run(*a, **k):
raise AssertionError("must not upgrade a checkout")
monkeypatch.setattr(up.subprocess, "run", _no_run)
res = CliRunner().invoke(main, ["update", "--yes"])
assert res.exit_code == 0
assert "git pull" in res.output
def test_update_network_failure(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: None)
res = CliRunner().invoke(main, ["update"])
assert res.exit_code != 0
assert "Could not reach PyPI" in res.output
def test_update_upgrade_failure_surfaces_command(monkeypatch):
monkeypatch.setattr(up, "installed_version", lambda: "0.26.0")
monkeypatch.setattr(up, "fetch_latest_version", lambda **k: "0.27.0")
monkeypatch.setattr(up, "_in_virtualenv", lambda: True)
class _Result:
returncode = 1
monkeypatch.setattr(up.subprocess, "run", lambda *a, **k: _Result())
res = CliRunner().invoke(main, ["update", "--yes"])
assert res.exit_code != 0
assert "Upgrade failed" in res.output