okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e8fb221c
main
1"""#4853: a checked engine folder that Windows (a virus scanner, the indexer) will not let Strata rename is copied into place instead of 2ending the install with "the update didn't finish". A rename that works is still a rename.""" 3 4import os 5import tempfile 6import unittest 7from pathlib import Path 8from unittest import mock 9 10from strata_control import updater 11 12 13def tree (root :Path )-> Path : 14 (root / "lib" ).mkdir (parents = True ) 15 (root / "strata.exe" ).write_bytes (b"exe" ) 16 (root / "lib" / "cublas.dll" ).write_bytes (b"dll" * 1000 ) 17return root 18 19 20class MoveDir (unittest .TestCase ): 21def setUp (self ): 22self .tmp = tempfile .TemporaryDirectory () 23self .addCleanup (self .tmp .cleanup ) 24self .root = Path (self .tmp .name ) 25self .source = tree (self .root / "stage" / "engine" ) 26self .destination = self .root / "live" / "engine" 27self .destination .parent .mkdir () 28self .clock = [0.0 ] 29 30def sleep (self ,seconds ): 31self .clock [0 ]+= seconds 32 33def move (self ,patience = 5.0 ): 34updater ._move_dir (self .source ,self .destination ,patience = patience ,sleep = self .sleep ,clock = lambda :self .clock [0 ]) 35 36def test_a_rename_that_works_is_a_rename (self ): 37self .move () 38self .assertTrue ((self .destination / "strata.exe" ).is_file ()) 39self .assertFalse (self .source .exists ()) 40 41def test_a_rename_held_for_a_moment_is_waited_for (self ): 42real ,calls = os .replace , [] 43 44def held (a ,b ): 45calls .append (1 ) 46if len (calls )< 4 : 47raise PermissionError (13 ,"in use" ) 48return real (a ,b ) 49with mock .patch .object (updater .os ,"replace" ,held ): 50self .move () 51self .assertEqual (len (calls ),4 ) 52self .assertTrue ((self .destination / "lib" / "cublas.dll" ).is_file ()) 53 54def test_a_rename_held_for_good_is_copied (self ): 55with mock .patch .object (updater .os ,"replace" ,side_effect = PermissionError (5 ,"Access is denied" )): 56self .move (patience = 3.0 ) 57self .assertGreaterEqual (self .clock [0 ],3.0 )# it waited the whole patience first 58self .assertEqual ((self .destination / "lib" / "cublas.dll" ).read_bytes (),b"dll" * 1000 ) 59self .assertEqual ((self .destination / "strata.exe" ).read_bytes (),b"exe" ) 60self .assertFalse (self .source .exists ()) 61 62def test_a_copy_that_fails_too_says_both_and_leaves_nothing_behind (self ): 63with mock .patch .object (updater .os ,"replace" ,side_effect = PermissionError (5 ,"Access is denied" )), \ 64mock .patch .object (updater .shutil ,"copytree" ,side_effect = OSError (28 ,"No space left on device" )): 65with self .assertRaises (updater .UpdateError )as caught : 66self .move (patience = 1.0 ) 67self .assertEqual (caught .exception .exit_code ,6 ) 68self .assertIn ("could not be moved into place" ,str (caught .exception )) 69self .assertIn ("copying it did not work either" ,str (caught .exception )) 70self .assertFalse (self .destination .exists ()) 71self .assertTrue ((self .source / "strata.exe" ).is_file ())# the staged copy is untouched 72 73@ unittest . skipUnless ( os . name == "nt" , "a program holding a file inside the folder refuses the rename only on Windows" ) 74def test_a_real_open_file_blocks_the_rename_and_the_copy_still_works (self ): 75with open (self .source / "lib" / "cublas.dll" ,"rb" ):# what a scanner does: it holds one file open 76self .move (patience = 0.0 ) 77self .assertEqual ((self .destination / "lib" / "cublas.dll" ).read_bytes (),b"dll" * 1000 ) 78 79 80if __name__ == "__main__" : 81unittest .main ()