tests/run-tests.py: Allow injected code customisation.

This commit introduces a mechanism to customise the code that is
injected to the board when performing a test file upload and execution.

A new argument, --begin", is added so regular Python code can be
inserted in the injected fragment between the module file creation and
the effective file import.  This is needed for running larger tests
(usually ones that have been pre-compiled with
"--via-mpy --emit native") on ESP8266, as that board does not have
enough memory to fit certain blocks of code unless additional
configuration is performed.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This commit is contained in:
Alessandro Gatti
2025-06-10 22:54:38 +02:00
committed by Damien George
parent 0d435959e0
commit 6fd7422a8e

View File

@@ -95,6 +95,7 @@ class __FS:
return __File()
vfs.mount(__FS(), '/__vfstest')
os.chdir('/__vfstest')
{import_prologue}
__import__('__injected_test')
"""
@@ -1130,6 +1131,8 @@ class append_filter(argparse.Action):
def main():
global injected_import_hook_code
cmd_parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Run and manage tests for MicroPython.
@@ -1239,8 +1242,20 @@ the last matching regex is used:
action="store_true",
help="re-run only the failed tests",
)
cmd_parser.add_argument(
"--begin",
metavar="PROLOGUE",
default=None,
help="prologue python file to execute before module import",
)
args = cmd_parser.parse_args()
prologue = ""
if args.begin:
with open(args.begin, "rt") as source:
prologue = source.read()
injected_import_hook_code = injected_import_hook_code.replace("{import_prologue}", prologue)
if args.print_failures:
for out in glob(os.path.join(args.result_dir, "*.out")):
testbase = out[:-4]