okami.mom/strata

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

Niko1221Merge f-022-refusals: environmental refusals repair or warn instead of blocking (audit items 1-4, 6, 8-10, 12-15)bcf88b738

main
2.4 KiB54 linesraw
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):
18    def test_no_bare_os_replace_is_left_in_the_state_file_writers(self):
19        for rel in SITES:
20            text = (ROOT / rel).read_text(encoding="utf-8")
21            bare = [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
23            bare = [l for l in bare if not (rel.endswith("updater.py") and "source, destination" in l)]
24            self.assertEqual(bare, [], rel)
25            self.assertIn("_atomic_mod.replace(", text, rel)
26
27
28class Retry(unittest.TestCase):
29    def test_a_held_file_is_waited_for_on_windows(self):
30        calls = []
31
32        def flaky(src, dst):
33            calls.append(1)
34            if len(calls) < 3:
35                raise PermissionError(13, "in use")
36        with mock.patch.object(atomic.os, "replace", flaky), mock.patch.object(atomic.os, "name", "nt"):
37            atomic.replace("a", "b", sleep=lambda s: None)
38        self.assertEqual(len(calls), 3)
39
40
41class Message(unittest.TestCase):
42    def test_sharing_violation_is_told_from_a_refusal(self):
43        in_use = PermissionError(13, "The process cannot access the file because it is being used by another process")
44        in_use.winerror = 32
45        denied = PermissionError(13, "Access is denied")
46        denied.winerror = 5
47        self.assertTrue(private_files.is_sharing_violation(in_use))
48        self.assertFalse(private_files.is_sharing_violation(denied))
49        self.assertNotIn("as yourself", private_files.ACCESS_MESSAGE)
50        self.assertIn("another program", private_files.IN_USE_MESSAGE)
51
52
53if __name__ == "__main__":
54    unittest.main()