From 339f02a5947e79347014b4d34adbf8a120b184bc Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Fri, 2 Jun 2023 15:40:03 +1000 Subject: [PATCH] tests/run-perfbench.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/perf_bench/core_import_mpy_multi.py | 4 ++-- tests/perf_bench/core_import_mpy_single.py | 6 +++--- tests/run-perfbench.py | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/perf_bench/core_import_mpy_multi.py b/tests/perf_bench/core_import_mpy_multi.py index ce68306678..364c325042 100644 --- a/tests/perf_bench/core_import_mpy_multi.py +++ b/tests/perf_bench/core_import_mpy_multi.py @@ -47,7 +47,7 @@ class FS: 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 @@ -58,7 +58,7 @@ class FS: def mount(): os.mount(FS(), "/__remote") - os.chdir("/__remote") + sys.path.insert(0, "/__remote") def test(r): diff --git a/tests/perf_bench/core_import_mpy_single.py b/tests/perf_bench/core_import_mpy_single.py index 1b411fc3fb..5757c3eaf1 100644 --- a/tests/perf_bench/core_import_mpy_single.py +++ b/tests/perf_bench/core_import_mpy_single.py @@ -2,7 +2,7 @@ # The first import of a module will intern strings that don't already exist, and # this test should be representative of what happens in a real application. -import io, os +import io, os, sys if not (hasattr(io, "IOBase") and hasattr(os, "mount")): print("SKIP") @@ -102,7 +102,7 @@ class FS: 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 @@ -113,7 +113,7 @@ class FS: def mount(): os.mount(FS(), "/__remote") - os.chdir("/__remote") + sys.path.insert(0, "/__remote") def test(): diff --git a/tests/run-perfbench.py b/tests/run-perfbench.py index 578f975bb8..81d873c459 100755 --- a/tests/run-perfbench.py +++ b/tests/run-perfbench.py @@ -109,8 +109,9 @@ def run_benchmarks(args, target, param_n, param_m, n_average, test_list): continue # Create test script + test_script = b"import sys\nsys.path.remove('')\n\n" with open(test_file, "rb") as f: - test_script = f.read() + test_script += f.read() with open(BENCH_SCRIPT_DIR + "benchrun.py", "rb") as f: test_script += f.read() test_script += b"bm_run(%u, %u)\n" % (param_n, param_m)