okami.mom/strata

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

Niko1221Merge f-0250-tui into f-0243-rel0c53372a0

main
5.5 KiB92 linesraw
1"""When setup.py stops with a Python error, `strata launch` says what it was and where the rest of the output is (#3613, #3467)."""
2
3import tempfile
4import unittest
5from pathlib import Path
6from unittest import mock
7
8from strata_cli.launch import runner
9from strata_cli.launch.runner import LaunchError, Runner, missing_file, python_failure, setup_failure
10
11TRACEBACK = [
12    "  [..] Tuning Strata for this PC",
13    "Traceback (most recent call last):",
14    '  File "C:\\Strata\\setup.py", line 6414, in calibrate_config',
15    "    import calibrate as CAL",
16    "ModuleNotFoundError: No module named 'calibrate'",
17]
18
19
20class SetupFailureWords(unittest.TestCase):
21    def test_an_option_setup_refuses_is_said_with_its_reason(self):                    # #3785
22        lines = ["Preparing unsloth UD-Q6_K_XL from your model files...",
23                 "setup.py: error: argument --kv: invalid choice: 'bf16' (choose from int8, q4_0, k8v4, fp16)"]
24        error = setup_failure(lines, "", "", "/tmp/a.setup.log", True)
25        self.assertIn("The model could not be installed. Argument --kv: invalid choice: 'bf16'", error.text)
26        self.assertIn("/tmp/a.setup.log", error.next)
27        self.assertEqual(setup_failure(["nothing useful"], "", "", None, True).text, "The model could not be installed.")
28
29    def test_setup_accepts_the_kv_format_the_docker_docs_offer(self):
30        import subprocess, sys
31        root = Path(__file__).resolve().parents[2]
32        done = subprocess.run([sys.executable, str(root / "setup.py"), "--kv", "bogus"], capture_output=True, text=True, cwd=str(root), timeout=120)
33        self.assertIn("fp16", done.stderr.splitlines()[-1])                  # the choices line of the argparse error
34
35    def test_the_last_line_of_a_traceback_is_found(self):
36        self.assertEqual(python_failure(TRACEBACK), "ModuleNotFoundError: No module named 'calibrate'")
37        self.assertIsNone(python_failure(["[X] Not enough disk space", "       Free some space"]))
38        self.assertIsNone(python_failure(["Traceback (most recent call last):", '  File "x.py", line 1, in <module>']))
39        # the last traceback wins, frames and blank lines are skipped
40        two = TRACEBACK[:1] + ["Traceback (most recent call last):", "  frame", "ValueError: old"] + TRACEBACK[1:]
41        self.assertEqual(python_failure(two), "ModuleNotFoundError: No module named 'calibrate'")
42
43    def test_a_missing_module_is_said_as_a_missing_file_with_the_next_step_and_the_output_path(self):
44        error = setup_failure(TRACEBACK, "", "", r"C:\Users\x\launch\a.setup.log", False)
45        self.assertIsInstance(error, LaunchError)
46        self.assertIn("The model could not be installed.", error.text)
47        self.assertIn("missing from this Strata install", error.text)
48        self.assertIn("No module named 'calibrate'", error.text)
49        self.assertIn("strata update", error.next)
50        self.assertIn(r"C:\Users\x\launch\a.setup.log", error.next)
51
52    def test_another_python_error_keeps_its_line_and_the_path(self):
53        error = setup_failure(["Traceback (most recent call last):", "  f", "KeyError: 'cuda'"], "", "", "/tmp/a.setup.log", True)
54        self.assertIn("KeyError: 'cuda'", error.text)
55        self.assertNotIn("missing", error.text)
56        self.assertIn("/tmp/a.setup.log", error.next)
57
58    def test_a_setup_error_line_still_wins_and_gets_the_details_path(self):
59        error = setup_failure(TRACEBACK, "Not enough disk space", "Free some space.", "/tmp/a.setup.log", False)
60        self.assertEqual(error.text, "The model could not be installed. Not enough disk space.")
61        self.assertEqual(error.next, "Free some space. Details: /tmp/a.setup.log")
62        plain = setup_failure([], "", "", None, False)
63        self.assertEqual(plain.text, "The model could not be installed.")
64        self.assertIn("a download continues where it stopped", plain.next)
65        self.assertNotIn("Details", plain.next)
66
67    def test_missing_file_kinds(self):
68        self.assertTrue(missing_file("ModuleNotFoundError: No module named 'x'"))
69        self.assertTrue(missing_file("ImportError: cannot import name 'y'"))
70        self.assertFalse(missing_file("ValueError: bad"))
71
72    def test_the_setup_output_is_kept_in_the_launch_folder(self):
73        with tempfile.TemporaryDirectory() as folder:
74            run = Runner.__new__(Runner)
75            with mock.patch.object(Runner, "name", new_callable=mock.PropertyMock, return_value="gyro-s-32k"),                     mock.patch.object(runner, "launch_dir", return_value=Path(folder)):
76                kept = run.keep_setup_output([f"line {i}" for i in range(1000)] + TRACEBACK)
77            self.assertEqual(Path(kept), Path(folder) / "gyro-s-32k.setup.log")
78            text = Path(kept).read_text(encoding="utf-8")
79            self.assertIn("ModuleNotFoundError: No module named 'calibrate'", text)
80            self.assertEqual(len(text.splitlines()), 400)                 # the tail, not a runaway file
81            with mock.patch.object(Runner, "name", new_callable=mock.PropertyMock, return_value="gyro-s-32k"),                     mock.patch.object(runner, "launch_dir", side_effect=OSError("read-only")):
82                self.assertIsNone(run.keep_setup_output(["x"]))
83
84
85    def test_a_message_that_ends_with_a_full_stop_does_not_get_a_second_one(self):
86        error = setup_failure(TRACEBACK, "Strata can't reach GitHub to download the DeepSeek engine.", "Check the internet connection.", "/tmp/a.setup.log", False)
87        self.assertIn("DeepSeek engine. Check", error.text + " " + error.next)
88        self.assertNotIn("engine..", error.text)
89
90
91if __name__ == "__main__":
92    unittest.main()