From 9d7eac07138b6e02f4c0775dc70f36fdd69432a2 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 2 Jun 2023 17:58:27 +1000 Subject: [PATCH] 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 --- tests/run-natmodtests.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/run-natmodtests.py b/tests/run-natmodtests.py index 9fe970a6d0..16f8fde967 100755 --- a/tests/run-natmodtests.py +++ b/tests/run-natmodtests.py @@ -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