tests/run-natmodtests.py: Don't allow imports from the cwd.

Make tests run in an isolated environment (i.e. `import io` would
otherwise get the `tests/io` directory).

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2023-06-02 17:58:27 +10:00
parent 339f02a594
commit 9d7eac0713
1 changed files with 4 additions and 3 deletions

View File

@ -46,14 +46,14 @@ class __FS:
def chdir(self, path):
pass
def stat(self, path):
if path == '__injected.mpy':
if path == '/__injected.mpy':
return tuple(0 for _ in range(10))
else:
raise OSError(-2) # ENOENT
def open(self, path, mode):
return __File()
os.mount(__FS(), '/__remote')
os.chdir('/__remote')
sys.path.insert(0, '/__remote')
sys.modules['{}'] = __import__('__injected')
"""
@ -111,9 +111,10 @@ def run_tests(target_truth, target, args, stats):
test_file_data = f.read()
# Create full test with embedded .mpy
test_script = b"import sys\nsys.path.remove('')\n\n"
try:
with open(NATMOD_EXAMPLE_DIR + test_mpy, "rb") as f:
test_script = b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"
test_script += b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"
except OSError:
print("---- {} - mpy file not compiled".format(test_file))
continue