Files
pkgmgr/tests/e2e/test_tools_commands.py
Kevin Veen-Birkenbach b4e0594901 style: modernise typing and clean up lint findings
Repository-wide mechanical cleanup so `ruff check src tests` has a chance
of passing; no behavioural changes.

- Add `from __future__ import annotations` where PEP 604 unions are used.
  This has to come first: pyproject declares requires-python >= 3.9, where
  `X | None` is not evaluable at runtime unless annotations are stringified.
- Replace typing.List/Dict/Tuple/Set with the builtin generics and
  Optional[X] with X | None, then drop the imports that became unused.
  The four actions/*/__init__.py files needed this by hand because ruff
  leaves unused imports in __init__.py alone (possible re-exports).
- Strip shebangs from 72 importable modules. None of them are executable
  or invoked directly; the entry points are console_scripts and runpy.
- Flatten nested `with` blocks, collapse needless-bool returns, and apply
  the remaining mechanical ruff fixes (PIE810, FLY002, PERF102, FURB192,
  RUF059, I001).
- Pass check=False explicitly to the four subprocess.run() calls that
  inspect returncode themselves. That is the existing default.

Two rewrites are visible to mocks, so their tests move with them:
subprocess.run(stdout=PIPE, stderr=PIPE) became capture_output=True, and
open(path, "r", ...) lost the redundant mode.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-27 16:47:13 +02:00

93 lines
2.7 KiB
Python

"""
Integration tests for the "tools" commands:
- pkgmgr code
- pkgmgr explore
- pkgmgr terminal
These commands spawn external GUI tools (VS Code, Nautilus,
GNOME Terminal) which are usually not available in a headless CI
container. Therefore, the entire test class is skipped by default.
If you run the tests on a local desktop environment where these
commands exist and can be spawned, you can remove or modify the
@skip decorator.
"""
from __future__ import annotations
import os
import runpy
import sys
import unittest
from test_version_commands import _load_pkgmgr_repo_dir
@unittest.skip(
"Requires GUI tools (code, nautilus, gnome-terminal) inside the "
"test environment; skipped by default in CI."
)
class TestIntegrationToolsCommands(unittest.TestCase):
"""
E2E tests for pkgmgr 'code', 'explore', and 'terminal' commands.
"""
@classmethod
def setUpClass(cls) -> None:
cls.pkgmgr_repo_dir = _load_pkgmgr_repo_dir()
def _run_pkgmgr_tools_command(self, extra_args: list[str]) -> None:
"""
Run a 'tools' style command (code/explore/terminal) for pkgmgr.
Any non-zero exit code is treated as a test failure.
"""
cmd_repr = "pkgmgr " + " ".join(extra_args)
original_argv = list(sys.argv)
original_cwd = os.getcwd()
try:
os.chdir(self.pkgmgr_repo_dir)
sys.argv = ["pkgmgr"] + extra_args
try:
runpy.run_module("pkgmgr", run_name="__main__")
except SystemExit as exc:
code = exc.code if isinstance(exc.code, int) else str(exc.code)
if code != 0:
print("[TEST] SystemExit caught while running", cmd_repr)
print(f"[TEST] Working directory: {os.getcwd()}")
print(f"[TEST] Exit code: {code}")
raise AssertionError(
f"{cmd_repr!r} failed with exit code {code}. "
"Scroll up to inspect the output printed before failure."
) from exc
# exit code 0 is success
finally:
os.chdir(original_cwd)
sys.argv = original_argv
def test_code_workspace_for_pkgmgr(self) -> None:
"""
Run: pkgmgr code pkgmgr
"""
self._run_pkgmgr_tools_command(["code", "pkgmgr"])
def test_explore_pkgmgr(self) -> None:
"""
Run: pkgmgr explore pkgmgr
"""
self._run_pkgmgr_tools_command(["explore", "pkgmgr"])
def test_terminal_pkgmgr(self) -> None:
"""
Run: pkgmgr terminal pkgmgr
"""
self._run_pkgmgr_tools_command(["terminal", "pkgmgr"])
if __name__ == "__main__":
unittest.main()