diff options
author | Per Larsen <perlarsen@google.com> | 2022-11-02 23:57:53 +0000 |
---|---|---|
committer | Per Larsen <perlarsen@google.com> | 2022-11-04 21:49:08 +0000 |
commit | 35a55691dd222bd131fb4509c8b2b223ec493e1b (patch) | |
tree | ec4da68508de4fa29a07be7eeae9c9739ca868d7 | |
parent | 8c14214df0f061b2bb6a5aa2302a0fedf2da17a0 (diff) | |
download | generic-arm64-35a55691dd222bd131fb4509c8b2b223ec493e1b.tar.gz |
project/qemu: Fix default arguments in Runner.run
The qemu.Runner.run(...) method uses empty lists but the argparser
uses None as the default value for --shell-command and --boot-test.
Since use of empty lists as default values is discouraged, change
the default values to None. This change also simplifies the logic
inside the run method by raising exceptions early and using if
statements instead of one big match statement.
Bug: None
Test: build a project and call the run script from the build output
folder while passing `--shell-command ls` to the run script.
Change-Id: I854ca4eec741d5882c98119a1ec772e55a440acc
-rw-r--r-- | project/qemu/qemu.py | 78 |
1 files changed, 40 insertions, 38 deletions
diff --git a/project/qemu/qemu.py b/project/qemu/qemu.py index 76a1d8b..23be8a2 100644 --- a/project/qemu/qemu.py +++ b/project/qemu/qemu.py @@ -6,6 +6,8 @@ import fcntl import json import os from textwrap import dedent +from typing import Optional, List + import qemu_options import re import select @@ -934,7 +936,9 @@ class Runner(object): raise - def run(self, boot_tests=[], android_tests=[], timeout=None): + def run(self, boot_tests: Optional[List] = None, + android_tests: Optional[List] = None, + timeout: Optional[int] = None): """Run boot or android tests. Runs boot_tests through test_runner, android_tests through ADB, @@ -957,47 +961,45 @@ class Runner(object): provided in the config. """ assert self.state == RunnerState.OFF - target_state = (RunnerState.BOOTLOADER if boot_tests else - RunnerState.ANDROID) - android_tests = android_tests if android_tests else [] self.config.check_config(self.interactive, boot_tests, android_tests) + if boot_tests and android_tests: + raise RunnerGenericError( + "Cannot run boot tests and android tests in the same " + "QEMU instance") + + if boot_tests and len(boot_tests) > 1: + raise RunnerGenericError( + "Can only run a single boot test at a time") + timeout = timeout if timeout else self.default_timeout try: - self.launch(target_state) + self.launch(RunnerState.BOOTLOADER if boot_tests else + RunnerState.ANDROID) - match (boot_tests, android_tests): - case ([], []): - pass # nothing to do - case (boot_test, []) if len(boot_test) == 1: - return [self.boottest_run(boot_test, timeout)] - case (boot_tests, []) if len(boot_tests) > 1: - raise RunnerGenericError( - "Can only run a single boot test at a time") - case ([], android_tests): - session: RunnerSession = self.session - test_results = [] - - try: - for android_test in android_tests: - test_result = self.androidtest_run([android_test], - timeout) - test_results.append(test_result) - if test_result: - session.has_error = True - break - return test_results - # Finally is used here to ensure that ADB failures do not - # take away the user's serial console in interactive mode. - finally: - if self.interactive: - # The user is responsible for quitting QEMU - session.qemu_proc.wait() - - case _: - assert boot_tests != [] and android_tests != [] - raise RunnerGenericError( - "Cannot run boot tests and android tests in the same " - "QEMU instance") + if boot_tests: + return [self.boottest_run(boot_tests, timeout)] + + if android_tests: + session: RunnerSession = self.session + test_results = [] + + try: + for android_test in android_tests: + test_result = self.androidtest_run([android_test], + timeout) + test_results.append(test_result) + if test_result: + session.has_error = True + break + return test_results + # Finally is used here to ensure that ADB failures do not + # take away the user's serial console in interactive mode. + finally: + if self.interactive: + # The user is responsible for quitting QEMU + session.qemu_proc.wait() + + return [] # nothing to do finally: self.shutdown() |