okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a7187997
main
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 23def __init__ (self )-> None : 24self .tmp = tempfile .TemporaryDirectory (prefix = "strata-launch-test-" ) 25base = Path (self .tmp .name ) 26self .base = base 27self .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 } 41self .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 ) 43self .patch = mock .patch .dict (os .environ ,self .env ) 44 45def __enter__ (self )-> "Home" : 46self .patch .start () 47for k in self .env_clear :# (patch.dict puts the original environment back on exit) 48os .environ .pop (k ,None ) 49return self 50 51def __exit__ (self ,* exc )-> None : 52self .patch .stop () 53self .tmp .cleanup () 54 55 56def catalog (hw :dict | None = None ,root :Path | None = None ): 57from strata_cli .launch .catalog import Catalog 58return 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).""" 63strips = app .screen ._compositor .render_strips () 64return "\n" .join (s .text .rstrip ()for s in strips ) 65 66 67def run_cli (argv :list [str ])-> tuple [int ,str ,str ]: 68from strata_cli .cli import main 69out ,err = io .StringIO (),io .StringIO () 70with contextlib .redirect_stdout (out ),contextlib .redirect_stderr (err ): 71try : 72code = main (argv ) 73except SystemExit as e : 74code = int (e .code or 0 ) 75return 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 80Intel Arc (p1006) the launch became an Intel one and the plan words changed. Patched for the length of the test.""" 81from strata_cli .launch .runner import Runner 82p = mock .patch .object (Runner ,"intel_chosen" ,lambda self :False ) 83p .start () 84testcase .addCleanup (p .stop )