okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0c53372a0
main
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 ): 21def test_an_option_setup_refuses_is_said_with_its_reason (self ):# #3785 22lines = ["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)" ] 24error = setup_failure (lines ,"" ,"" ,"/tmp/a.setup.log" ,True ) 25self .assertIn ("The model could not be installed. Argument --kv: invalid choice: 'bf16'" ,error .text ) 26self .assertIn ("/tmp/a.setup.log" ,error .next ) 27self .assertEqual (setup_failure (["nothing useful" ],"" ,"" ,None ,True ).text ,"The model could not be installed." ) 28 29def test_setup_accepts_the_kv_format_the_docker_docs_offer (self ): 30import subprocess ,sys 31root = Path (__file__ ).resolve ().parents [2 ] 32done = subprocess .run ([sys .executable ,str (root / "setup.py" ),"--kv" ,"bogus" ],capture_output = True ,text = True ,cwd = str (root ),timeout = 120 ) 33self .assertIn ("fp16" ,done .stderr .splitlines ()[- 1 ])# the choices line of the argparse error 34 35def test_the_last_line_of_a_traceback_is_found (self ): 36self .assertEqual (python_failure (TRACEBACK ),"ModuleNotFoundError: No module named 'calibrate'" ) 37self .assertIsNone (python_failure (["[X] Not enough disk space" ," Free some space" ])) 38self .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 40two = TRACEBACK [:1 ]+ ["Traceback (most recent call last):" ," frame" ,"ValueError: old" ]+ TRACEBACK [1 :] 41self .assertEqual (python_failure (two ),"ModuleNotFoundError: No module named 'calibrate'" ) 42 43def test_a_missing_module_is_said_as_a_missing_file_with_the_next_step_and_the_output_path (self ): 44error = setup_failure (TRACEBACK ,"" ,"" ,r"C:\Users\x\launch\a.setup.log" ,False ) 45self .assertIsInstance (error ,LaunchError ) 46self .assertIn ("The model could not be installed." ,error .text ) 47self .assertIn ("missing from this Strata install" ,error .text ) 48self .assertIn ("No module named 'calibrate'" ,error .text ) 49self .assertIn ("strata update" ,error .next ) 50self .assertIn (r"C:\Users\x\launch\a.setup.log" ,error .next ) 51 52def test_another_python_error_keeps_its_line_and_the_path (self ): 53error = setup_failure (["Traceback (most recent call last):" ," f" ,"KeyError: 'cuda'" ],"" ,"" ,"/tmp/a.setup.log" ,True ) 54self .assertIn ("KeyError: 'cuda'" ,error .text ) 55self .assertNotIn ("missing" ,error .text ) 56self .assertIn ("/tmp/a.setup.log" ,error .next ) 57 58def test_a_setup_error_line_still_wins_and_gets_the_details_path (self ): 59error = setup_failure (TRACEBACK ,"Not enough disk space" ,"Free some space." ,"/tmp/a.setup.log" ,False ) 60self .assertEqual (error .text ,"The model could not be installed. Not enough disk space." ) 61self .assertEqual (error .next ,"Free some space. Details: /tmp/a.setup.log" ) 62plain = setup_failure ([],"" ,"" ,None ,False ) 63self .assertEqual (plain .text ,"The model could not be installed." ) 64self .assertIn ("a download continues where it stopped" ,plain .next ) 65self .assertNotIn ("Details" ,plain .next ) 66 67def test_missing_file_kinds (self ): 68self .assertTrue (missing_file ("ModuleNotFoundError: No module named 'x'" )) 69self .assertTrue (missing_file ("ImportError: cannot import name 'y'" )) 70self .assertFalse (missing_file ("ValueError: bad" )) 71 72def test_the_setup_output_is_kept_in_the_launch_folder (self ): 73with tempfile .TemporaryDirectory ()as folder : 74run = Runner .__new__ (Runner ) 75with mock .patch .object (Runner ,"name" ,new_callable = mock .PropertyMock ,return_value = "gyro-s-32k" ),mock .patch .object (runner ,"launch_dir" ,return_value = Path (folder )): 76kept = run .keep_setup_output ([f"line { i } " for i in range (1000 )]+ TRACEBACK ) 77self .assertEqual (Path (kept ),Path (folder )/ "gyro-s-32k.setup.log" ) 78text = Path (kept ).read_text (encoding = "utf-8" ) 79self .assertIn ("ModuleNotFoundError: No module named 'calibrate'" ,text ) 80self .assertEqual (len (text .splitlines ()),400 )# the tail, not a runaway file 81with 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" )): 82self .assertIsNone (run .keep_setup_output (["x" ])) 83 84 85def test_a_message_that_ends_with_a_full_stop_does_not_get_a_second_one (self ): 86error = setup_failure (TRACEBACK ,"Strata can't reach GitHub to download the DeepSeek engine." ,"Check the internet connection." ,"/tmp/a.setup.log" ,False ) 87self .assertIn ("DeepSeek engine. Check" ,error .text + " " + error .next ) 88self .assertNotIn ("engine.." ,error .text ) 89 90 91if __name__ == "__main__" : 92unittest .main ()