Skip to content

Commit 9d7eac0

Browse files
committed
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>
1 parent 339f02a commit 9d7eac0

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

tests/run-natmodtests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ def mount(self, readonly, mkfs):
4646
def chdir(self, path):
4747
pass
4848
def stat(self, path):
49-
if path == '__injected.mpy':
49+
if path == '/__injected.mpy':
5050
return tuple(0 for _ in range(10))
5151
else:
5252
raise OSError(-2) # ENOENT
5353
def open(self, path, mode):
5454
return __File()
5555
os.mount(__FS(), '/__remote')
56-
os.chdir('/__remote')
56+
sys.path.insert(0, '/__remote')
5757
sys.modules['{}'] = __import__('__injected')
5858
"""
5959

@@ -111,9 +111,10 @@ def run_tests(target_truth, target, args, stats):
111111
test_file_data = f.read()
112112

113113
# Create full test with embedded .mpy
114+
test_script = b"import sys\nsys.path.remove('')\n\n"
114115
try:
115116
with open(NATMOD_EXAMPLE_DIR + test_mpy, "rb") as f:
116-
test_script = b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"
117+
test_script += b"__buf=" + bytes(repr(f.read()), "ascii") + b"\n"
117118
except OSError:
118119
print("---- {} - mpy file not compiled".format(test_file))
119120
continue

0 commit comments

Comments
 (0)