okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1"""Windows 'file in use': every swap of a state file waits and retries (atomic.replace), and the message does not blame permissions.""" 2 3import re 4import unittest 5from pathlib import Path 6from unittest import mock 7 8from strata_control import atomic ,private_files 9 10ROOT = Path (__file__ ).resolve ().parents [2 ] 11SITES = ["strata_control/calibration.py" ,"strata_control/components.py" ,"strata_control/presets.py" , 12"strata_control/preset_marks.py" ,"strata_control/procs.py" ,"strata_control/profiles.py" , 13"strata_control/tune_apply.py" ,"strata_control/updater.py" ,"strata_cli/bench.py" ,"strata_cli/connections.py" , 14"strata_cli/launch/runner.py" ,"strata_cli/launch/store.py" ,"strata_cli/tune_commands.py" ,"serve/runconfig.py" ] 15 16 17class Sites (unittest .TestCase ): 18def test_no_bare_os_replace_is_left_in_the_state_file_writers (self ): 19for rel in SITES : 20text = (ROOT / rel ).read_text (encoding = "utf-8" ) 21bare = [l for l in text .splitlines ()if re .search (r"(?<![_\w.])os\.replace\(" ,l )or re .search (r"[^_]os\.replace\(" ,l )] 22# updater._move_dir keeps its own longer retry loop 23bare = [l for l in bare if not (rel .endswith ("updater.py" )and "source, destination" in l )] 24self .assertEqual (bare , [],rel ) 25self .assertIn ("_atomic_mod.replace(" ,text ,rel ) 26 27 28class Retry (unittest .TestCase ): 29def test_a_held_file_is_waited_for_on_windows (self ): 30calls = [] 31 32def flaky (src ,dst ): 33calls .append (1 ) 34if len (calls )< 3 : 35raise PermissionError (13 ,"in use" ) 36with mock .patch .object (atomic .os ,"replace" ,flaky ),mock .patch .object (atomic .os ,"name" ,"nt" ): 37atomic .replace ("a" ,"b" ,sleep = lambda s :None ) 38self .assertEqual (len (calls ),3 ) 39 40 41class Message (unittest .TestCase ): 42def test_sharing_violation_is_told_from_a_refusal (self ): 43in_use = PermissionError (13 ,"The process cannot access the file because it is being used by another process" ) 44in_use .winerror = 32 45denied = PermissionError (13 ,"Access is denied" ) 46denied .winerror = 5 47self .assertTrue (private_files .is_sharing_violation (in_use )) 48self .assertFalse (private_files .is_sharing_violation (denied )) 49self .assertNotIn ("as yourself" ,private_files .ACCESS_MESSAGE ) 50self .assertIn ("another program" ,private_files .IN_USE_MESSAGE ) 51 52 53if __name__ == "__main__" : 54unittest .main ()