okami.mom/strata

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

Niko1221test_home_hermetic: raw docstring837909fdd

main
2.1 KiB47 linesraw
1r"""The Home() helper of the launch tests must keep every per-user file the code reads or writes inside its temp folder.
2
3A test that read the person's real %APPDATA%\Strata\settings.json (its remembered Strata folders) or ~/.config/strata saw the files of whoever ran it: the
4DeepSeek plan tests said "tight" / "spills" for UD-Q8_K_XL on a PC that had a Strata install, and "fits" everywhere else."""
5import os
6import unittest
7from pathlib import Path
8from unittest import mock
9
10from tests.terminal.launch_support import Home
11
12
13def inside(path: Path, base: Path) -> bool:
14    try:
15        Path(path).resolve().relative_to(base.resolve())
16        return True
17    except ValueError:
18        return False
19
20
21class HomeIsHermetic(unittest.TestCase):
22    def test_every_per_user_location_is_inside_the_temp_folder(self):
23        from strata_control import discovery, models, profiles
24        import setup
25        from strata_cli import connections
26        with Home() as h:
27            places = {
28                "discovery.own_settings_file (Windows rule)": discovery.own_settings_file(windows=True),
29                "discovery.own_settings_file (Linux/macOS rule)": discovery.own_settings_file(windows=False),
30                "models.settings_path": models.settings_path(),
31                "profiles.profile_dir": profiles.profile_dir(),
32                "setup.settings_path": setup.settings_path(),
33                "connections.connection_dir": connections.connection_dir(create=False),
34            }
35            for name, path in places.items():
36                self.assertTrue(inside(path, h.base), f"{name} = {path} is outside {h.base}")
37
38    def test_a_developers_own_settings_variable_does_not_reach_a_test(self):
39        with mock.patch.dict(os.environ, {"STRATA_SETTINGS_FILE": "X:/mine.json", "STRATA_CONNECTION_DIR": "X:/conn"}):
40            with Home():
41                self.assertNotIn("STRATA_SETTINGS_FILE", os.environ)
42                self.assertNotIn("STRATA_CONNECTION_DIR", os.environ)
43            self.assertEqual(os.environ["STRATA_SETTINGS_FILE"], "X:/mine.json")
44
45
46if __name__ == "__main__":
47    unittest.main()