okami.mom/strata

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

Niko1221Merge f-0213-fixux (82d3a0345): user screenshots - a data folder on an external drive under /run/media is no longer refused as a system folder (real system folders still are; the doubled sentence is gone); one fit label everywhere (owner 11-08): 'fits' when the model files without the n-gram/PLE/Engram table fit graphics memory plus RAM, else 'may run slower' (the Unsloth sets were measured against RAM only; DeepSeek Needs shows the whole size; no-card PCs and small Macs say 'may run slower'; NVFP4-QAD sized by its 76.8 GB pack)b7bcbcde9

main
5.5 KiB96 linesraw
1"""0.2.13: a data folder on an external drive is allowed (/run/media/$USER, /media/$USER, /mnt), real system folders are still refused,
2and a refusal says "Nothing was downloaded or changed" once."""
3from __future__ import annotations
4
5import contextlib
6import io
7import os
8import tempfile
9import unittest
10from pathlib import Path
11from unittest import mock
12
13import setup
14from strata_control import folders
15
16SAID = "Nothing was downloaded or changed"
17
18
19def posix_rules():
20    """The POSIX folder rules, whatever system the test runs on."""
21    return mock.patch.object(folders, "_system_roots", lambda: list(folders._POSIX_SYSTEM))
22
23
24class MountPoints(unittest.TestCase):
25    def test_the_drives_a_desktop_mounts_are_not_system_folders(self):
26        with posix_rules(), mock.patch.dict(os.environ, {"HOME": "/home/ann", "USERPROFILE": "/home/ann"}):
27            for p in ("/run/media/ann/Backup/Strata", "/run/media/ann/My Drive/Strata-models", "/run/media/ann/Backup",
28                      "/media/ann/Backup/Strata", "/media/ann/Backup", "/mnt/models/Strata", "/mnt/data"):
29                with self.subTest(p=p):
30                    self.assertFalse(folders._is_system(p), p)
31
32    def test_real_system_folders_are_still_refused(self):
33        with posix_rules(), mock.patch.dict(os.environ, {"HOME": "/home/ann", "USERPROFILE": "/home/ann"}):
34            for p in ("/usr/share/strata", "/etc/strata", "/bin", "/boot/x", "/dev/shm/x", "/proc/1", "/sys/x", "/root/x", "/var/lib/strata",
35                      "/lib/x", "/sbin", "/run", "/run/user/1000/strata", "/run/systemd/x", "/run/lock", "/run/media", "/run/mediax/Strata"):
36                with self.subTest(p=p):
37                    self.assertTrue(folders._is_system(p), p)
38
39    def test_the_run_media_folder_of_the_mounts_is_not_a_place_to_save(self):
40        with tempfile.TemporaryDirectory() as d:
41            run = Path(d) / "run"
42            (run / "media" / "ann" / "Backup").mkdir(parents=True)
43            with mock.patch.object(folders, "_system_roots", lambda: [str(run)]), mock.patch.object(folders, "_POSIX_MOUNTS", (str(run / "media"),)), \
44                    mock.patch.dict(os.environ, {"HOME": str(Path(d) / "home"), "USERPROFILE": str(Path(d) / "home")}):
45                self.assertIsNone(folders.problem(str(run / "media" / "ann" / "Backup" / "Strata")))        # created on the drive: fine
46                self.assertIn("system folder", folders.problem(str(run / "media"))[0])
47                self.assertIn("system folder", folders.problem(str(run / "elsewhere"))[0])
48                self.assertIn("system folder", folders.problem(str(run / "user" / "1000"))[0])
49
50    def test_a_mount_the_user_cannot_write_to_says_so_and_is_not_called_a_system_folder(self):
51        with tempfile.TemporaryDirectory() as d:
52            run = Path(d) / "run"
53            (run / "media" / "bob" / "Drive").mkdir(parents=True)                      # somebody else's drive
54            with mock.patch.object(folders, "_system_roots", lambda: [str(run)]), mock.patch.object(folders, "_POSIX_MOUNTS", (str(run / "media"),)), \
55                    mock.patch.object(folders, "_can_write", return_value=False), \
56                    mock.patch.dict(os.environ, {"HOME": str(Path(d) / "home"), "USERPROFILE": str(Path(d) / "home")}):
57                what, _ = folders.problem(str(run / "media" / "bob" / "Drive" / "Strata"))
58            self.assertIn("cannot be created", what)
59            self.assertNotIn("system folder", what)
60
61    def test_a_writable_folder_outside_home_is_fine(self):                              # /mnt/... and the like, "outside /home"
62        with tempfile.TemporaryDirectory() as d:
63            with posix_rules(), mock.patch.dict(os.environ, {"HOME": "/home/nobody", "USERPROFILE": "/home/nobody"}):
64                self.assertIsNone(folders.problem(str(Path(d) / "Strata")))
65
66
67class OneSentence(unittest.TestCase):
68    def test_the_closing_sentence_is_dropped_only_when_it_is_there(self):
69        both = ("/etc/x is a system folder.", folders.NEXT_POSIX)
70        self.assertEqual(folders.without_nothing_changed(both).count(SAID), 0)
71        self.assertTrue(folders.without_nothing_changed(both).endswith("for example ~/strata-models."))
72        self.assertEqual(folders.without_nothing_changed(("It is bad.", "Do that.")), "It is bad. Do that.")
73
74    def test_a_refused_data_dir_says_nothing_changed_once(self):
75        with tempfile.TemporaryDirectory() as d:
76            f = Path(d) / "afile.txt"
77            f.write_text("x")
78            for target in (f, Path(d) / "afile.txt" / "inside"):
79                out = io.StringIO()
80                with self.subTest(target=target), contextlib.redirect_stdout(out), self.assertRaises(SystemExit):
81                    setup.data_folder(str(target))
82                self.assertEqual(out.getvalue().count(SAID), 1, out.getvalue())
83                self.assertIn("was not saved as your data folder", out.getvalue())
84
85    @unittest.skipIf(os.name == "nt", "POSIX paths")
86    def test_a_system_folder_refusal_says_nothing_changed_once(self):
87        out = io.StringIO()
88        with posix_rules(), mock.patch.dict(os.environ, {"HOME": "/home/nobody", "USERPROFILE": "/home/nobody"}), \
89                mock.patch.object(setup, "load_settings", return_value={}), contextlib.redirect_stdout(out), self.assertRaises(SystemExit):
90            setup.data_folder("/usr/share/Strata")
91        self.assertEqual(out.getvalue().count(SAID), 1, out.getvalue())
92        self.assertIn("is a system folder", out.getvalue())
93
94
95if __name__ == "__main__":
96    unittest.main()