summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-06-02 15:40:03 +1000
committerJim Mussared <jim.mussared@gmail.com>2023-06-08 17:54:24 +1000
commit339f02a5947e79347014b4d34adbf8a120b184bc (patch)
tree4113da0673a1652b3b8a04d8e43c09ebee87d145
parent109717457edbf49421af3848ea26e26fff567289 (diff)
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 <jim.mussared@gmail.com>
-rw-r--r--tests/perf_bench/core_import_mpy_multi.py4
-rw-r--r--tests/perf_bench/core_import_mpy_single.py6
-rwxr-xr-xtests/run-perfbench.py3
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 ce6830667..364c32504 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 1b411fc3f..5757c3eaf 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 578f975bb..81d873c45 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)