okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-gh2-app (0dfa2d9b1)2e8fb221c

main
3.5 KiB81 linesraw
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)
17    return root
18
19
20class MoveDir(unittest.TestCase):
21    def setUp(self):
22        self.tmp = tempfile.TemporaryDirectory()
23        self.addCleanup(self.tmp.cleanup)
24        self.root = Path(self.tmp.name)
25        self.source = tree(self.root / "stage" / "engine")
26        self.destination = self.root / "live" / "engine"
27        self.destination.parent.mkdir()
28        self.clock = [0.0]
29
30    def sleep(self, seconds):
31        self.clock[0] += seconds
32
33    def move(self, patience=5.0):
34        updater._move_dir(self.source, self.destination, patience=patience, sleep=self.sleep, clock=lambda: self.clock[0])
35
36    def test_a_rename_that_works_is_a_rename(self):
37        self.move()
38        self.assertTrue((self.destination / "strata.exe").is_file())
39        self.assertFalse(self.source.exists())
40
41    def test_a_rename_held_for_a_moment_is_waited_for(self):
42        real, calls = os.replace, []
43
44        def held(a, b):
45            calls.append(1)
46            if len(calls) < 4:
47                raise PermissionError(13, "in use")
48            return real(a, b)
49        with mock.patch.object(updater.os, "replace", held):
50            self.move()
51        self.assertEqual(len(calls), 4)
52        self.assertTrue((self.destination / "lib" / "cublas.dll").is_file())
53
54    def test_a_rename_held_for_good_is_copied(self):
55        with mock.patch.object(updater.os, "replace", side_effect=PermissionError(5, "Access is denied")):
56            self.move(patience=3.0)
57        self.assertGreaterEqual(self.clock[0], 3.0)                       # it waited the whole patience first
58        self.assertEqual((self.destination / "lib" / "cublas.dll").read_bytes(), b"dll" * 1000)
59        self.assertEqual((self.destination / "strata.exe").read_bytes(), b"exe")
60        self.assertFalse(self.source.exists())
61
62    def test_a_copy_that_fails_too_says_both_and_leaves_nothing_behind(self):
63        with mock.patch.object(updater.os, "replace", side_effect=PermissionError(5, "Access is denied")), \
64                mock.patch.object(updater.shutil, "copytree", side_effect=OSError(28, "No space left on device")):
65            with self.assertRaises(updater.UpdateError) as caught:
66                self.move(patience=1.0)
67        self.assertEqual(caught.exception.exit_code, 6)
68        self.assertIn("could not be moved into place", str(caught.exception))
69        self.assertIn("copying it did not work either", str(caught.exception))
70        self.assertFalse(self.destination.exists())
71        self.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")
74    def test_a_real_open_file_blocks_the_rename_and_the_copy_still_works(self):
75        with open(self.source / "lib" / "cublas.dll", "rb"):              # what a scanner does: it holds one file open
76            self.move(patience=0.0)
77        self.assertEqual((self.destination / "lib" / "cublas.dll").read_bytes(), b"dll" * 1000)
78
79
80if __name__ == "__main__":
81    unittest.main()