forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_check_main_actor_xctest_hooks.py
More file actions
executable file
·78 lines (61 loc) · 2.85 KB
/
Copy pathtest_check_main_actor_xctest_hooks.py
File metadata and controls
executable file
·78 lines (61 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""Fixture coverage for the fast @MainActor XCTest lifecycle guard."""
from __future__ import annotations
import importlib.util
import sys
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
GUARD_PATH = REPO_ROOT / "desktop/macos/scripts/check-main-actor-xctest-hooks.py"
def _load_guard():
spec = importlib.util.spec_from_file_location("main_actor_xctest_hooks", GUARD_PATH)
assert spec and spec.loader
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
class MainActorXCTestHooksTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.guard = _load_guard()
def test_rejects_async_super_hook_in_main_actor_xctest_case(self):
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "UnsafeTests.swift"
path.write_text(
"import XCTest\n\n@MainActor\nfinal class UnsafeTests: XCTestCase {\n"
" override func setUp() async throws {\n try await super.setUp()\n }\n}\n",
encoding="utf-8",
)
findings = self.guard.find_unsafe_hooks(path)
self.assertEqual(len(findings), 1)
self.assertEqual(findings[0].class_name, "UnsafeTests")
self.assertEqual(findings[0].hook, "setUp")
def test_rejects_inline_main_actor_annotation(self):
with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir) / "UnsafeTests.swift"
path.write_text(
"import XCTest\n\n@MainActor final class UnsafeTests: XCTestCase {\n"
" override func tearDown() async throws {\n try await super.tearDown()\n }\n}\n",
encoding="utf-8",
)
findings = self.guard.find_unsafe_hooks(path)
self.assertEqual(
[(finding.class_name, finding.hook) for finding in findings], [("UnsafeTests", "tearDown")]
)
def test_allows_nonisolated_or_safe_main_actor_lifecycle(self):
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
(root / "SafeMainActorTests.swift").write_text(
"import XCTest\n\n@MainActor\nfinal class SafeMainActorTests: XCTestCase {\n"
" override func setUp() async throws { }\n}\n",
encoding="utf-8",
)
(root / "NonisolatedTests.swift").write_text(
"import XCTest\n\nfinal class NonisolatedTests: XCTestCase {\n"
" override func setUp() async throws {\n try await super.setUp()\n }\n}\n",
encoding="utf-8",
)
self.assertEqual(self.guard.find_all_unsafe_hooks(root), [])
if __name__ == "__main__":
unittest.main()