mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 19:47:12 +01:00
meta/scripts/optimize_vscode.py was 498 lines against a 250-line cap and completely untested, and at that path the repo-wide gate (--cov=python_pkg, fail_under = 100) could not see it. Moved verbatim to python_pkg/vscode_optimizer/_optimize.py - git records a pure rename, and the content is byte-identical to HEAD. The move and the tests are one commit by necessity: any intermediate state with the module under python_pkg/ but uncovered turns every pytest run in the repo red, including ci-mirror on pre-push for unrelated work. Running it by path no longer works after the move (sys.path[0] would be the module's own directory), so __main__.py replaces it, following the existing focus_policy convention rather than a sys.path insert. Nothing invoked the old path: grep over .sh/.desktop/.service/.yml/.py found zero callers. 98 tests reach 100% line and branch coverage. Patching is confined to real boundaries - subprocess.run, the module-level Path, builtins.input, sys.argv - so the forthcoming split has few monkeypatch targets to re-point. Writing the tests surfaced a pre-existing bug, left unfixed here and pinned by a strict xfail: _VENDOR_KW's "ati" key is a substring of "VGA compatible controller", which appears in every lspci display line, so it matches before "intel" is ever tried and Intel iGPUs are misreported as AMD. Verified: python3 -m python_pkg.vscode_optimizer --dry-run reproduces the pre-move 18-change plan and writes nothing; the real settings.json is untouched; repo-wide pytest is 1846 passed at 100%; over-cap files 37 -> 36. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
240 lines
8.0 KiB
Python
240 lines
8.0 KiB
Python
"""Tests for applying a plan to one variant, and for the CLI entry point.
|
|
|
|
Every variant used here points at ``tmp_path``. The suite must never be able
|
|
to resolve a real ``~/.config/*/User/settings.json``; the last test in this
|
|
module asserts that directly.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import builtins
|
|
import json
|
|
import sys
|
|
from typing import TYPE_CHECKING
|
|
|
|
import pytest
|
|
|
|
from python_pkg.vscode_optimizer import _optimize as opt
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
def _variant(tmp_path: Path) -> opt._Variant:
|
|
"""A VS Code variant rooted entirely inside tmp_path."""
|
|
return opt._Variant(
|
|
name="Test Code",
|
|
settings=tmp_path / "User" / "settings.json",
|
|
flags=tmp_path / "code-flags.conf",
|
|
binary="code",
|
|
)
|
|
|
|
|
|
_HW = opt._Hw(cpu_physical_cores=8, ram_total_mb=32000, gpu_vendor="NVIDIA")
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# _apply_variant
|
|
# --------------------------------------------------------------------------- #
|
|
def test_apply_variant_writes_settings_and_flags(tmp_path: Path) -> None:
|
|
"""With --yes, both files are written and the plan is applied."""
|
|
v = _variant(tmp_path)
|
|
|
|
opt._apply_variant(v, _HW, ["--enable-zero-copy"], dry_run=False, auto_yes=True)
|
|
|
|
written = json.loads(v.settings.read_text())
|
|
assert written["search.maxThreads"] == 8
|
|
assert written["files.maxMemoryForLargeFilesMB"] == 4096
|
|
assert v.flags.read_text().strip() == "--enable-zero-copy"
|
|
|
|
|
|
def test_apply_variant_writes_nothing_in_dry_run(
|
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""--dry-run reports the plan and leaves the disk untouched."""
|
|
v = _variant(tmp_path)
|
|
|
|
opt._apply_variant(v, _HW, ["--enable-zero-copy"], dry_run=True, auto_yes=True)
|
|
|
|
assert not v.settings.exists()
|
|
assert not v.flags.exists()
|
|
assert "dry-run" in capsys.readouterr().out
|
|
|
|
|
|
def test_apply_variant_backs_up_an_existing_settings_file(tmp_path: Path) -> None:
|
|
"""An existing file is copied aside before being overwritten."""
|
|
v = _variant(tmp_path)
|
|
v.settings.parent.mkdir(parents=True)
|
|
v.settings.write_text('{"editor.fontSize": 14}')
|
|
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=True)
|
|
|
|
backups = list(v.settings.parent.glob("*.bak"))
|
|
assert len(backups) == 1
|
|
assert json.loads(backups[0].read_text()) == {"editor.fontSize": 14}
|
|
assert json.loads(v.settings.read_text())["editor.fontSize"] == 14
|
|
|
|
|
|
def test_apply_variant_backs_up_an_existing_flags_file(tmp_path: Path) -> None:
|
|
"""The flags file gets the same backup treatment as settings."""
|
|
v = _variant(tmp_path)
|
|
v.flags.write_text("--existing-flag\n")
|
|
|
|
opt._apply_variant(v, _HW, ["--enable-zero-copy"], dry_run=False, auto_yes=True)
|
|
|
|
assert list(tmp_path.glob("*.bak"))
|
|
assert "--existing-flag" in v.flags.read_text()
|
|
assert "--enable-zero-copy" in v.flags.read_text()
|
|
|
|
|
|
def test_apply_variant_does_nothing_when_already_optimized(
|
|
tmp_path: Path, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""A variant needing no changes is reported and skipped."""
|
|
v = _variant(tmp_path)
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=True)
|
|
before = v.settings.read_text()
|
|
capsys.readouterr()
|
|
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=True)
|
|
|
|
assert v.settings.read_text() == before
|
|
assert "Nothing to do" in capsys.readouterr().out
|
|
|
|
|
|
def test_apply_variant_writes_only_flags_when_settings_are_current(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""A flags-only change leaves settings.json untouched, with no backup."""
|
|
v = _variant(tmp_path)
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=True)
|
|
settings_before = v.settings.read_text()
|
|
|
|
opt._apply_variant(v, _HW, ["--ignore-gpu-blocklist"], dry_run=False, auto_yes=True)
|
|
|
|
assert v.settings.read_text() == settings_before
|
|
assert not list(v.settings.parent.glob("*.bak"))
|
|
assert "--ignore-gpu-blocklist" in v.flags.read_text()
|
|
|
|
|
|
@pytest.mark.parametrize("answer", ["y", "yes", "Y", "YES"])
|
|
def test_apply_variant_applies_on_confirmation(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path, answer: str
|
|
) -> None:
|
|
"""Any spelling of yes at the prompt applies the change."""
|
|
v = _variant(tmp_path)
|
|
monkeypatch.setattr(builtins, "input", lambda _p: answer)
|
|
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=False)
|
|
|
|
assert v.settings.exists()
|
|
|
|
|
|
@pytest.mark.parametrize("answer", ["n", "", "no", "maybe"])
|
|
def test_apply_variant_skips_when_declined(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
tmp_path: Path,
|
|
capsys: pytest.CaptureFixture[str],
|
|
answer: str,
|
|
) -> None:
|
|
"""Anything other than yes leaves the disk untouched."""
|
|
v = _variant(tmp_path)
|
|
monkeypatch.setattr(builtins, "input", lambda _p: answer)
|
|
|
|
opt._apply_variant(v, _HW, [], dry_run=False, auto_yes=False)
|
|
|
|
assert not v.settings.exists()
|
|
assert "Skipped" in capsys.readouterr().out
|
|
|
|
|
|
def test_apply_variant_adds_flags_without_duplicating_existing_ones(
|
|
tmp_path: Path,
|
|
) -> None:
|
|
"""Merging flags preserves order and drops duplicates."""
|
|
v = _variant(tmp_path)
|
|
v.flags.write_text("--enable-zero-copy\n--custom\n")
|
|
|
|
opt._apply_variant(
|
|
v,
|
|
_HW,
|
|
["--enable-zero-copy", "--ignore-gpu-blocklist"],
|
|
dry_run=False,
|
|
auto_yes=True,
|
|
)
|
|
|
|
assert v.flags.read_text().split() == [
|
|
"--enable-zero-copy",
|
|
"--custom",
|
|
"--ignore-gpu-blocklist",
|
|
]
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# main
|
|
# --------------------------------------------------------------------------- #
|
|
def test_main_optimizes_every_discovered_variant(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""The entry point detects hardware, then applies to each variant."""
|
|
v = _variant(tmp_path)
|
|
monkeypatch.setattr(sys, "argv", ["optimize_vscode", "--yes"])
|
|
monkeypatch.setattr(opt, "_detect_hardware", lambda: _HW)
|
|
monkeypatch.setattr(opt, "_discover_variants", lambda: [v])
|
|
|
|
opt.main()
|
|
|
|
assert json.loads(v.settings.read_text())["search.maxThreads"] == 8
|
|
|
|
|
|
def test_main_honours_dry_run(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
|
|
"""--dry-run reaches _apply_variant and prevents all writes."""
|
|
v = _variant(tmp_path)
|
|
monkeypatch.setattr(sys, "argv", ["optimize_vscode", "--dry-run"])
|
|
monkeypatch.setattr(opt, "_detect_hardware", lambda: _HW)
|
|
monkeypatch.setattr(opt, "_discover_variants", lambda: [v])
|
|
|
|
opt.main()
|
|
|
|
assert not v.settings.exists()
|
|
|
|
|
|
def test_main_exits_nonzero_without_an_installation(
|
|
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
) -> None:
|
|
"""No VS Code variant is a usage error, not a silent success."""
|
|
monkeypatch.setattr(sys, "argv", ["optimize_vscode", "--dry-run"])
|
|
monkeypatch.setattr(opt, "_detect_hardware", lambda: _HW)
|
|
monkeypatch.setattr(opt, "_discover_variants", list)
|
|
|
|
with pytest.raises(SystemExit) as excinfo:
|
|
opt.main()
|
|
|
|
assert excinfo.value.code == 1
|
|
assert "No VS Code installation found" in capsys.readouterr().out
|
|
|
|
|
|
def test_the_module_entry_point_exposes_main() -> None:
|
|
"""`python3 -m python_pkg.vscode_optimizer` resolves to the same main().
|
|
|
|
The script used to be run by path from meta/scripts/; this import is what
|
|
proves the replacement entry point works after the move.
|
|
"""
|
|
from python_pkg.vscode_optimizer import __main__ as entry
|
|
|
|
assert entry.main is opt.main
|
|
|
|
|
|
def test_the_suite_never_touches_the_real_user_settings(
|
|
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
|
|
) -> None:
|
|
"""Guard rail: with Path.home redirected, discovery stays inside tmp_path.
|
|
|
|
A regression here would mean some test could rewrite the developer's own
|
|
VS Code configuration, which is exactly what --dry-run exists to prevent.
|
|
"""
|
|
monkeypatch.setattr(opt.Path, "home", classmethod(lambda _cls: tmp_path))
|
|
|
|
for variant in opt._discover_variants():
|
|
assert tmp_path in variant.settings.parents
|
|
assert tmp_path in variant.flags.parents
|