tests/run-multitests.py: Allow passing unique env vars to each instance.

For example, to run the BLE multitests entirely with the unix port:

    env MICROPY_MICROPYTHON=../ports/unix/micropython-dev ./run-multitests.py \
        -i micropython,MICROPYBTUSB=01 \
        -i micropython,MICROPYBTUSB=02:02 \
        multi_bluetooth/ble_*.py
This commit is contained in:
Jim Mussared 2020-06-05 15:41:40 +10:00 committed by Damien George
parent 3f77f2c60c
commit 00c3e2156a

View File

@ -92,20 +92,25 @@ class PyInstance:
class PyInstanceSubProcess(PyInstance): class PyInstanceSubProcess(PyInstance):
def __init__(self, cmd): def __init__(self, argv, env=None):
self.cmd = cmd self.argv = argv
self.env = {n: v for n, v in (i.split("=") for i in env)} if env else None
self.popen = None self.popen = None
self.finished = True self.finished = True
def __str__(self): def __str__(self):
return self.cmd[0].rsplit("/")[-1] return self.argv[0].rsplit("/")[-1]
def run_script(self, script): def run_script(self, script):
output = b"" output = b""
err = None err = None
try: try:
p = subprocess.run( p = subprocess.run(
self.cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, input=script self.argv,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
input=script,
env=self.env,
) )
output = p.stdout output = p.stdout
except subprocess.CalledProcessError as er: except subprocess.CalledProcessError as er:
@ -114,7 +119,11 @@ class PyInstanceSubProcess(PyInstance):
def start_script(self, script): def start_script(self, script):
self.popen = subprocess.Popen( self.popen = subprocess.Popen(
self.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT self.argv,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=self.env,
) )
self.popen.stdin.write(script) self.popen.stdin.write(script)
self.popen.stdin.close() self.popen.stdin.close()
@ -404,16 +413,20 @@ def main():
instances_test = [] instances_test = []
for i in cmd_args.instance: for i in cmd_args.instance:
if i.startswith("exec:"): # Each instance arg is <cmd>,ENV=VAR,ENV=VAR...
instances_test.append(PyInstanceSubProcess([i[len("exec:") :]])) i = i.split(",")
elif i == "micropython": cmd = i[0]
instances_test.append(PyInstanceSubProcess([MICROPYTHON])) env = i[1:]
elif i == "cpython": if cmd.startswith("exec:"):
instances_test.append(PyInstanceSubProcess([CPYTHON3])) instances_test.append(PyInstanceSubProcess([cmd[len("exec:") :]], env))
elif i.startswith("pyb:"): elif cmd == "micropython":
instances_test.append(PyInstancePyboard(i[len("pyb:") :])) instances_test.append(PyInstanceSubProcess([MICROPYTHON], env))
elif cmd == "cpython":
instances_test.append(PyInstanceSubProcess([CPYTHON3], env))
elif cmd.startswith("pyb:"):
instances_test.append(PyInstancePyboard(cmd[len("pyb:") :]))
else: else:
print("unknown instance string: {}".format(i), file=sys.stderr) print("unknown instance string: {}".format(cmd), file=sys.stderr)
sys.exit(1) sys.exit(1)
for _ in range(max_instances - len(instances_test)): for _ in range(max_instances - len(instances_test)):