okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-clean-2 (c94ef2375): drafting and plan test fixtures no longer read the box's real Intel GPU (9 failures on the Arc box)6a7187997

main
3.9 KiB84 linesraw
1"""Shared fixtures for the `strata launch` tests: an isolated home, a fake computer, and a text dump of a Textual screen."""
2
3from __future__ import annotations
4
5import contextlib
6import io
7import os
8import tempfile
9from pathlib import Path
10from unittest import mock
11
12HW_12 = {"ram_gb": 64.0, "os": "Test OS", "cpu": {"avx2": True, "name": "test cpu"},
13         "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 5070", "vram_gb": 12.0, "usable": True, "vendor": "nvidia"}]}
14HW_24 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA GeForce RTX 4090", "vram_gb": 24.0, "usable": True, "vendor": "nvidia"}]}
15HW_2X16 = {**HW_12, "gpus": [{"index": 0, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"},
16                             {"index": 1, "name": "NVIDIA RTX A4000", "vram_gb": 16.0, "usable": True, "vendor": "nvidia"}]}
17HW_32GB_RAM = {**HW_12, "ram_gb": 32.0}
18
19
20class Home:
21    """A throw-away per-user home: profiles, presets, launch presets, state, components and install folder."""
22
23    def __init__(self) -> None:
24        self.tmp = tempfile.TemporaryDirectory(prefix="strata-launch-test-")
25        base = Path(self.tmp.name)
26        self.base = base
27        self.env = {
28            "STRATA_PROFILE_DIR": str(base / "config" / "profiles"),
29            "STRATA_PRESET_DIR": str(base / "config" / "presets"),
30            "STRATA_LAUNCH_PRESET_DIR": str(base / "config" / "launch-presets"),
31            "STRATA_COMPONENTS_DIR": str(base / "components"),
32            "STRATA_INSTALL_DIR": str(base / "install"),
33            "STRATA_RUNTIME_DIR": str(base / "runtime"),
34            "XDG_STATE_HOME": str(base / "state"),
35            "LOCALAPPDATA": str(base / "local"),
36            "APPDATA": str(base / "roaming"),            # Windows: %APPDATA%\Strata\settings.json (remembered Strata folders), profiles, connections
37            "XDG_CONFIG_HOME": str(base / "xdg-config"),  # Linux/macOS: ~/.config/strata/settings.json
38            "HOME": str(base / "home"),
39            "USERPROFILE": str(base / "home"),
40        }
41        self.env_clear = ("STRATA_SETTINGS_FILE", "STRATA_CONNECTION_DIR", "CUDA_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES")   # a developer's own variables must not leak into a test either
42        (base / "home").mkdir(parents=True, exist_ok=True)
43        self.patch = mock.patch.dict(os.environ, self.env)
44
45    def __enter__(self) -> "Home":
46        self.patch.start()
47        for k in self.env_clear:                       # (patch.dict puts the original environment back on exit)
48            os.environ.pop(k, None)
49        return self
50
51    def __exit__(self, *exc) -> None:
52        self.patch.stop()
53        self.tmp.cleanup()
54
55
56def catalog(hw: dict | None = None, root: Path | None = None):
57    from strata_cli.launch.catalog import Catalog
58    return Catalog(root=root or Path(os.environ["STRATA_INSTALL_DIR"]), hardware=hw or HW_12)
59
60
61def text_of(app) -> str:
62    """What the current screen shows, one line per terminal row (for assertions and the text previews)."""
63    strips = app.screen._compositor.render_strips()
64    return "\n".join(s.text.rstrip() for s in strips)
65
66
67def run_cli(argv: list[str]) -> tuple[int, str, str]:
68    from strata_cli.cli import main
69    out, err = io.StringIO(), io.StringIO()
70    with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
71        try:
72            code = main(argv)
73        except SystemExit as e:
74            code = int(e.code or 0)
75    return code, out.getvalue(), err.getvalue()
76
77
78def not_intel(testcase) -> None:
79    """These fixtures describe an NVIDIA PC (catalog(HW_12)). Runner.intel_chosen asks the machine that runs the test, so on a box with an
80    Intel Arc (p1006) the launch became an Intel one and the plan words changed. Patched for the length of the test."""
81    from strata_cli.launch.runner import Runner
82    p = mock.patch.object(Runner, "intel_chosen", lambda self: False)
83    p.start()
84    testcase.addCleanup(p.stop)