mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 19:47:12 +01:00
The last python file over the 250-line cap. Now that it sits at 100% line
and branch coverage, split along its natural seams:
_types.py 48 the three dataclasses, so nothing imports a sibling
merely to name a type (which would have made a cycle)
_render.py 69 the ANSI palette and the printers
_config_io.py 104 variant discovery, JSONC parsing, reads and writes
_hardware.py 106 _run and the four probes
_plan.py 156 the pure hardware -> proposed-changes functions
_optimize.py 100 the CLI entry: _apply_variant and main
Definitions were moved by line range rather than retyped. A throwaway
ast.dump identity check against HEAD confirms all 26 top-level definitions
are unchanged - formatting-blind, logic-sensitive.
The first attempt raised NameError: name '_BO' is not defined at runtime;
_BO was used by main() but missing from the _render import list. Rather than
fix that one failure alone, an AST scan over all six modules for names used
but not bound confirmed nothing else was missing.
Test imports now name the owning module; no re-export barrels. One patch
target genuinely moved - the guard-rail test patches _config_io.Path, since
that is the namespace resolving the name when _discover_variants is called
directly rather than through main().
Verified: same 98 tests pass with every module at 100% line and branch;
python3 -m python_pkg.vscode_optimizer --dry-run reproduces the same
18-change plan and writes nothing; repo-wide pytest 1846 passed at 100%;
over-cap files 36 -> 35.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
134 lines
4.2 KiB
Python
134 lines
4.2 KiB
Python
"""Tests for the command runner and for CPU and RAM detection.
|
|
|
|
``_run`` shells out, so ``subprocess.run`` is patched at the boundary, and
|
|
``_detect_ram``'s hardcoded ``/proc/meminfo`` is redirected by patching the
|
|
module-level ``Path`` name. Nothing here reads the machine's real hardware.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from python_pkg.vscode_optimizer import _hardware as opt
|
|
from python_pkg.vscode_optimizer._types import _Hw
|
|
from python_pkg.vscode_optimizer.tests.conftest import FakeProc as _Proc
|
|
from python_pkg.vscode_optimizer.tests.conftest import fake_run as _fake_run
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
_LSCPU_OUT = """\
|
|
Architecture: x86_64
|
|
Model name: AMD Ryzen 9 7900X3D 12-Core Processor
|
|
CPU(s): 24
|
|
Core(s) per socket: 12
|
|
CPU max MHz: 5662.5000
|
|
"""
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# _run
|
|
# --------------------------------------------------------------------------- #
|
|
def test_run_returns_stripped_stdout(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""A successful command hands back its stdout without surrounding space."""
|
|
monkeypatch.setattr(subprocess, "run", lambda *_a, **_k: _Proc(" out \n"))
|
|
|
|
assert opt._run(["anything"]) == "out"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"error",
|
|
[subprocess.TimeoutExpired(cmd="x", timeout=10), FileNotFoundError()],
|
|
)
|
|
def test_run_returns_empty_when_the_command_fails(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
error: Exception,
|
|
) -> None:
|
|
"""A missing binary or a timeout degrades to an empty string, not a crash."""
|
|
|
|
def _boom(*_a: object, **_k: object) -> None:
|
|
raise error
|
|
|
|
monkeypatch.setattr(subprocess, "run", _boom)
|
|
|
|
assert opt._run(["anything"]) == ""
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# _detect_cpu
|
|
# --------------------------------------------------------------------------- #
|
|
def test_detect_cpu_reads_model_cores_and_clock(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Each recognised lscpu key lands on its typed _Hw attribute."""
|
|
monkeypatch.setattr(subprocess, "run", _fake_run({"lscpu": _LSCPU_OUT}))
|
|
hw = _Hw()
|
|
|
|
opt._detect_cpu(hw)
|
|
|
|
assert hw.cpu_model == "AMD Ryzen 9 7900X3D 12-Core Processor"
|
|
assert hw.cpu_logical_cores == 24
|
|
assert hw.cpu_physical_cores == 12
|
|
assert hw.cpu_max_mhz == pytest.approx(5662.5)
|
|
|
|
|
|
def test_detect_cpu_ignores_unknown_keys(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""Lines outside the _LSCPU map leave the defaults alone."""
|
|
monkeypatch.setattr(
|
|
subprocess,
|
|
"run",
|
|
_fake_run({"lscpu": "Architecture: x86_64\nBogoMIPS: 9000\n"}),
|
|
)
|
|
hw = _Hw()
|
|
|
|
opt._detect_cpu(hw)
|
|
|
|
assert hw.cpu_model == "Unknown"
|
|
assert hw.cpu_physical_cores == 1
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# _detect_ram
|
|
# --------------------------------------------------------------------------- #
|
|
def test_detect_ram_converts_kb_to_mb(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""MemTotal in kB becomes whole MB."""
|
|
meminfo = tmp_path / "meminfo"
|
|
meminfo.write_text("MemTotal: 32768000 kB\nMemFree: 100 kB\n")
|
|
monkeypatch.setattr(opt, "Path", lambda _p: meminfo)
|
|
hw = _Hw()
|
|
|
|
opt._detect_ram(hw)
|
|
|
|
assert hw.ram_total_mb == 32000
|
|
|
|
|
|
def test_detect_ram_leaves_zero_when_memtotal_is_absent(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""A procfs file without MemTotal is not an error, just no reading."""
|
|
meminfo = tmp_path / "meminfo"
|
|
meminfo.write_text("SwapTotal: 4096 kB\n")
|
|
monkeypatch.setattr(opt, "Path", lambda _p: meminfo)
|
|
hw = _Hw()
|
|
|
|
opt._detect_ram(hw)
|
|
|
|
assert hw.ram_total_mb == 0
|
|
|
|
|
|
def test_detect_ram_survives_an_unreadable_procfs(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""An OSError reading /proc/meminfo leaves the default in place."""
|
|
monkeypatch.setattr(opt, "Path", lambda _p: tmp_path / "does-not-exist")
|
|
hw = _Hw()
|
|
|
|
opt._detect_ram(hw)
|
|
|
|
assert hw.ram_total_mb == 0
|