-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathtest_syspath_restore.py
More file actions
71 lines (53 loc) · 2.56 KB
/
test_syspath_restore.py
File metadata and controls
71 lines (53 loc) · 2.56 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
import unittest
import sys
import os
class TestSysPathRestore(unittest.TestCase):
"""Test that sys.path is properly restored after execute_script"""
def test_syspath_restored_after_execute_script(self):
"""Test that sys.path is restored to original state after file execution"""
# Import here to ensure we're in the right context
from mpos import AppManager
# Capture original sys.path
original_path = sys.path[:]
original_length = len(sys.path)
# Create a test directory path that would be added
test_cwd = "builtin/apps/com.micropythonos.launcher/assets/"
# Verify the test path is not already in sys.path
self.assertFalse(test_cwd in original_path,
f"Test path {test_cwd} should not be in sys.path initially")
test_script = "builtin/apps/com.micropythonos.launcher/assets/launcher.py"
# Call execute_script with cwd parameter
result = AppManager.execute_script(
test_script,
classname="Launcher",
cwd=test_cwd
)
self.assertTrue(result)
# After execution, sys.path should be restored
current_path = sys.path
current_length = len(sys.path)
# Verify sys.path has been restored to original
self.assertEqual(current_length, original_length,
f"sys.path length should be restored. Original: {original_length}, Current: {current_length}")
# Verify the test directory is not in sys.path anymore
self.assertFalse(test_cwd in current_path,
f"Test path {test_cwd} should not be in sys.path after execution. sys.path={current_path}")
# Verify sys.path matches original
self.assertEqual(current_path, original_path,
f"sys.path should match original.\nOriginal: {original_path}\nCurrent: {current_path}")
def test_syspath_not_affected_when_no_cwd(self):
"""Test that sys.path is unchanged when cwd is None"""
from mpos import AppManager
# Capture original sys.path
original_path = sys.path[:]
test_script = "builtin/apps/com.micropythonos.launcher/assets/launcher.py"
# Call without cwd parameter
result = AppManager.execute_script(
test_script,
classname="Launcher",
cwd=None
)
self.assertFalse(result)
# sys.path should be unchanged
self.assertEqual(sys.path, original_path,
"sys.path should be unchanged when cwd is None")