okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bb7e399c3
main
1"""Windows MAX_PATH (260) and the engine installer. 2 3A fresh Windows PC has long paths off. `strata launch` on an AMD PC failed with "local file operation failed 4(FileNotFoundError)" because the staging folder (components\\.updates\\staging\\<12 hex>\\engine\\<folder>) plus the longest file in 5the hip packages (149 characters) went past 259 under an ordinary user name. This pins the path arithmetic for the real 0.2.1 6package file lists (tests/terminal/data/windows_package_paths.json, made from the release tarballs) and the behaviour of the fix.""" 7 8import io 9import json 10import os 11import tarfile 12import tempfile 13import unittest 14from pathlib import Path ,PureWindowsPath 15from unittest import mock 16 17from strata_control import engine_component as ec ,longpath ,updater 18 19DATA = json .loads ((Path (__file__ ).parent / "data" / "windows_package_paths.json" ).read_text (encoding = "utf-8" )) 20LIMIT = longpath .PLAIN_LIMIT 21USER = "u" * 20 22COMPONENTS = PureWindowsPath (rf"C:\Users\ { USER } \AppData\Local\Strata\components" )# what components_dir() gives on Windows 23LEGACY_STAGE = COMPONENTS / ".updates" / "staging" / ("0123456789ab" )# what 0.2.1 used 24NEW_STAGE = COMPONENTS / updater .STAGE_DIR / "0123" 25WINDOWS = os .name == "nt" 26 27 28def longest (entry )-> str : 29return max (entry ["longest" ],key = len ) 30 31 32class PathArithmetic (unittest .TestCase ): 33def test_the_old_staging_folder_was_past_the_limit (self ): 34"""The reproduction: with the 0.2.1 layout the AMD packages do not fit under a 20-character user name.""" 35too_long = {} 36for e in DATA ["engines" ]: 37n = len (str (LEGACY_STAGE / "engine" / e ["folder" ]/ longest (e ))) 38if n > LIMIT : 39too_long [e ["folder" ]]= n 40self .assertEqual (sorted (too_long ), ["0.1.46-windows-x86_64-hip-rdna3" ,"0.1.46-windows-x86_64-hip-rx9070" ],too_long ) 41 42def test_every_staged_path_fits_for_a_20_character_user_name (self ): 43for e in DATA ["engines" ]: 44n = len (str (NEW_STAGE / "engine" / e ["folder" ]/ longest (e ))) 45self .assertLessEqual (n ,LIMIT ,f" { e [ 'folder' ] } : staged path is { n } characters" ) 46 47def test_every_final_path_fits_for_a_20_character_user_name (self ): 48for e in DATA ["engines" ]: 49n = len (str (COMPONENTS / "engine" / e ["folder" ]/ longest (e ))) 50self .assertLessEqual (n ,LIMIT ,f" { e [ 'folder' ] } : installed path is { n } characters" ) 51for c in DATA ["control" ]: 52for base in (COMPONENTS / "control" / c ["version" ],COMPONENTS / updater .STAGE_DIR / "c0123" ): 53n = len (str (base / longest (c ))) 54self .assertLessEqual (n ,LIMIT ,f"control { c [ 'version' ] } : path is { n } characters" ) 55 56def test_staging_is_no_deeper_than_nine_characters_over_the_install (self ): 57live = COMPONENTS / "engine" / "x" 58staged = NEW_STAGE / "engine" / "x" 59self .assertLessEqual (len (str (staged ))- len (str (live )),9 ) 60 61def test_the_hip_estimate_covers_the_real_packages (self ): 62for e in DATA ["engines" ]: 63if "-hip-" in e ["folder" ]: 64self .assertGreaterEqual (longpath .LONGEST_IN_PACKAGE ["hip" ],len (longest (e ))) 65 66 67class Messages (unittest .TestCase ): 68def test_a_missing_file_is_named (self ): 69words = longpath .file_words (FileNotFoundError (2 ,"No such file or directory" ,r"C:\x\y\z.dat" )) 70self .assertIn (r"C:\x\y\z.dat" ,words ) 71self .assertIn ("FileNotFoundError" ,words ) 72 73def test_something_without_a_name_still_says_what_kind (self ): 74self .assertIn ("(OSError)" ,longpath .file_words (OSError ("boom" ))) 75 76@ unittest . skipUnless ( WINDOWS , "the limit is a Windows one" ) 77def test_a_path_that_is_too_long_says_so (self ): 78name = "C:\\" + "\\" .join (["d" * 60 ]* 5 )+ "\\file.dat" 79words = longpath .file_words (FileNotFoundError (2 ,"No such file or directory" ,name )) 80self .assertIn ("longer than Windows allows" ,words ) 81self .assertIn ("STRATA_COMPONENTS_DIR" ,words ) 82self .assertIn (name ,words ) 83 84@ unittest . skipUnless ( WINDOWS , "the limit is a Windows one" ) 85def test_no_warning_for_a_short_folder_and_one_for_a_deep_one (self ): 86self .assertIsNone (longpath .warning_for (Path (r"C:\Strata\engine\0.1.46-windows-x86_64-hip-rdna3" ),150 )) 87deep = Path ("C:/Users/" + USER + "/AppData/Local/Strata/components/engine/0.1.46-windows-x86_64-hip-rx9070/" + "q" * 30 ) 88words = longpath .warning_for (deep ,150 ) 89self .assertIn ("long paths" ,words ) 90self .assertIn ("carries on" ,words ) 91 92 93@ unittest . skipUnless ( WINDOWS , "extended-length paths are a Windows thing" ) 94class DeepFolders (unittest .TestCase ): 95def setUp (self ): 96self .temp = tempfile .TemporaryDirectory (prefix = "strata-lp-" ) 97self .addCleanup (lambda :__import__ ("shutil" ).rmtree (longpath .long_path (self .temp .name ),ignore_errors = True )) 98self .deep = Path (self .temp .name )/ ("p" * 120 )/ ("q" * 120 ) 99 100def archive (self ,member ,data = b"engine bytes" ): 101raw = io .BytesIO () 102with tarfile .open (fileobj = raw ,mode = "w:gz" )as tar : 103info = tarfile .TarInfo (member ) 104info .size = len (data ) 105tar .addfile (info ,io .BytesIO (data )) 106path = Path (self .temp .name )/ "pkg.tar.gz" 107path .write_bytes (raw .getvalue ()) 108return path 109 110def test_long_path_adds_the_prefix_once (self ): 111p = longpath .long_path (r"C:\a\b" ) 112self .assertTrue (str (p ).startswith ("\\\\?\\C:\\a" )) 113self .assertEqual (longpath .long_path (p ),p ) 114self .assertEqual (longpath .plain (p ),r"C:\a\b" ) 115 116def test_extract_goes_through_the_extended_form (self ): 117member = "rocm/bin/hipblaslt/library/gfx1151/" + "T" * 137 + ".dat.zlib" 118archive = self .archive (member ) 119seen = [] 120real = updater .components .safe_extract 121 122def spy (archive_ ,destination ,** kw ): 123seen .append (str (destination )) 124return real (archive_ ,destination ,** kw ) 125 126with mock .patch .object (updater .components ,"safe_extract" ,spy ): 127updater .extract (archive ,self .deep / "engine" / "x" ,"tar.gz" ,1 << 20 ) 128self .assertTrue (seen [0 ].startswith ("\\\\?\\" ),seen ) 129target = longpath .long_path (self .deep / "engine" / "x" )/ member 130self .assertGreater (len (longpath .plain (target )),LIMIT ) 131self .assertEqual (target .read_bytes (),b"engine bytes" ) 132 133def test_a_failed_unpack_names_the_file (self ): 134archive = self .archive ("a/b.dat" ) 135with mock .patch .object (updater .components ,"safe_extract" ,side_effect = FileNotFoundError (3 ,"The system cannot find the path" ,r"C:\n\o.dat" )): 136with self .assertRaises (updater .UpdateError )as cm : 137updater .extract (archive ,self .deep ,"tar.gz" ,1 << 20 ) 138self .assertIn (r"C:\n\o.dat" ,str (cm .exception )) 139self .assertEqual (cm .exception .exit_code ,6 ) 140 141def test_verify_reads_files_that_sit_past_the_limit (self ): 142from tests .terminal import update_fixture as fx 143root = Path (self .temp .name ) 144built = fx .build_package (root / "b" ,"0.1.45" ) 145# a folder deep enough that the stub's files are past 259 characters 146base = longpath .long_path (root / ("m" * 110 )/ ("n" * 110 )) 147import shutil 148shutil .copytree (longpath .long_path (built ),base / built .name ) 149component = ec .read_manifest (base / built .name ) 150self .assertGreater (len (longpath .plain (base / built .name / component .entrypoint )),0 ) 151ec .verify (component ,full = True )# raises IntegrityProblem if a file looks missing 152 153def test_the_probe_runs_and_cleans_up (self ): 154longpath .plain_long_paths_work (Path (self .temp .name )) 155self .assertEqual ([p .name for p in Path (self .temp .name ).iterdir ()if p .name .startswith (".lp-" )], []) 156 157 158class StagingFolder (unittest .TestCase ): 159def test_new_stage_is_short_and_sweeps_old_staging (self ): 160with tempfile .TemporaryDirectory (prefix = "strata-stage-" )as tmp : 161root = Path (tmp ) 162old = root / ".updates" / "staging" / "abc" 163old .mkdir (parents = True ) 164stale = root / updater .STAGE_DIR / "dead" 165stale .mkdir (parents = True ) 166stage = updater .new_stage (root ) 167self .assertEqual (stage .parent ,root / updater .STAGE_DIR ) 168self .assertEqual (len (stage .name ),4 ) 169self .assertFalse (old .exists ()or stale .exists ()) 170self .assertTrue (updater .new_stage (root ,"c" ).name .startswith ("c" )) 171 172 173if __name__ == "__main__" : 174unittest .main ()