Files
strata/tests/terminal/test_setup_failure_words.py
T
Niko1221 2a22daa4f0 Fix #3613 #3467: the Terminal/Control bundle ships tools/calibrate.py; a setup crash now says what it was
setup.py imports tools/calibrate.py after a tune, but INSTALLER_TOOLS did not list it, so every launch after a tune died with
ModuleNotFoundError and only 'The model could not be installed'. The list now names it, and add_installer also reads the
tools/ modules setup.py imports so the list cannot go stale again (stage-control.py uses the same list).

A new test stages the bundle the way package_terminal.py does and imports setup.py and its tools with tools/ limited to the bundle.
strata launch keeps the last 400 lines of setup's output in launch/<name>.setup.log, shows the Python error line in plain words
when setup crashed without an [X] line, and names the file.
2026-10-22 10:08:50 +02:00

73 lines
4.1 KiB
Python

"""When setup.py stops with a Python error, `strata launch` says what it was and where the rest of the output is (#3613, #3467)."""
import tempfile
import unittest
from pathlib import Path
from unittest import mock
from strata_cli.launch import runner
from strata_cli.launch.runner import LaunchError, Runner, missing_file, python_failure, setup_failure
TRACEBACK = [
" [..] Tuning Strata for this PC",
"Traceback (most recent call last):",
' File "C:\\Strata\\setup.py", line 6414, in calibrate_config',
" import calibrate as CAL",
"ModuleNotFoundError: No module named 'calibrate'",
]
class SetupFailureWords(unittest.TestCase):
def test_the_last_line_of_a_traceback_is_found(self):
self.assertEqual(python_failure(TRACEBACK), "ModuleNotFoundError: No module named 'calibrate'")
self.assertIsNone(python_failure(["[X] Not enough disk space", " Free some space"]))
self.assertIsNone(python_failure(["Traceback (most recent call last):", ' File "x.py", line 1, in <module>']))
# the last traceback wins, frames and blank lines are skipped
two = TRACEBACK[:1] + ["Traceback (most recent call last):", " frame", "ValueError: old"] + TRACEBACK[1:]
self.assertEqual(python_failure(two), "ModuleNotFoundError: No module named 'calibrate'")
def test_a_missing_module_is_said_as_a_missing_file_with_the_next_step_and_the_output_path(self):
error = setup_failure(TRACEBACK, "", "", r"C:\Users\x\launch\a.setup.log", False)
self.assertIsInstance(error, LaunchError)
self.assertIn("The model could not be installed.", error.text)
self.assertIn("missing from this Strata install", error.text)
self.assertIn("No module named 'calibrate'", error.text)
self.assertIn("strata update", error.next)
self.assertIn(r"C:\Users\x\launch\a.setup.log", error.next)
def test_another_python_error_keeps_its_line_and_the_path(self):
error = setup_failure(["Traceback (most recent call last):", " f", "KeyError: 'cuda'"], "", "", "/tmp/a.setup.log", True)
self.assertIn("KeyError: 'cuda'", error.text)
self.assertNotIn("missing", error.text)
self.assertIn("/tmp/a.setup.log", error.next)
def test_a_setup_error_line_still_wins_and_gets_the_details_path(self):
error = setup_failure(TRACEBACK, "Not enough disk space", "Free some space.", "/tmp/a.setup.log", False)
self.assertEqual(error.text, "The model could not be installed. Not enough disk space.")
self.assertEqual(error.next, "Free some space. Details: /tmp/a.setup.log")
plain = setup_failure([], "", "", None, False)
self.assertEqual(plain.text, "The model could not be installed.")
self.assertIn("a download continues where it stopped", plain.next)
self.assertNotIn("Details", plain.next)
def test_missing_file_kinds(self):
self.assertTrue(missing_file("ModuleNotFoundError: No module named 'x'"))
self.assertTrue(missing_file("ImportError: cannot import name 'y'"))
self.assertFalse(missing_file("ValueError: bad"))
def test_the_setup_output_is_kept_in_the_launch_folder(self):
with tempfile.TemporaryDirectory() as folder:
run = Runner.__new__(Runner)
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)):
kept = run.keep_setup_output([f"line {i}" for i in range(1000)] + TRACEBACK)
self.assertEqual(Path(kept), Path(folder) / "gyro-s-32k.setup.log")
text = Path(kept).read_text(encoding="utf-8")
self.assertIn("ModuleNotFoundError: No module named 'calibrate'", text)
self.assertEqual(len(text.splitlines()), 400) # the tail, not a runaway file
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")):
self.assertIsNone(run.keep_setup_output(["x"]))
if __name__ == "__main__":
unittest.main()