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")