[U-Boot] [PATCH 2/3] test/py: Allow to pass u_boot_log instead of console for run_and_log

The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de --- test/py/u_boot_utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py index 2ba4bae..c80cf07 100644 --- a/test/py/u_boot_utils.py +++ b/test/py/u_boot_utils.py @@ -153,7 +153,7 @@ def wait_until_file_open_fails(fn, ignore_errors): return raise Exception('File can still be opened')
-def run_and_log(u_boot_console, cmd, ignore_errors=False): +def run_and_log(u_boot_console_or_log, cmd, ignore_errors=False): """Run a command and log its output.
Args: @@ -171,7 +171,10 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False): """ if isinstance(cmd, str): cmd = cmd.split() - runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) + try: + runner = u_boot_console_or_log.get_runner(cmd[0], sys.stdout) + except: + runner = u_boot_console_or_log.log.get_runner(cmd[0], sys.stdout) output = runner.run(cmd, ignore_errors=ignore_errors) runner.close() return output @@ -189,7 +192,10 @@ def run_and_log_expect_exception(u_boot_console, cmd, retcode, msg): msg: String that should be contained within the command's output. """ try: + runner = u_boot_console.get_runner(cmd[0], sys.stdout) + except: runner = u_boot_console.log.get_runner(cmd[0], sys.stdout) + try: runner.run(cmd) except Exception as e: assert(retcode == runner.exit_status)

On 11/05/2016 10:45 AM, Stefan Brüns wrote:
The runner actually has no console dependency, only on the log provided by the console. Accept both u_boot_console or a multiplexed_log.
diff --git a/test/py/u_boot_utils.py b/test/py/u_boot_utils.py
-def run_and_log(u_boot_console, cmd, ignore_errors=False): +def run_and_log(u_boot_console_or_log, cmd, ignore_errors=False): """Run a command and log its output.
Args:
I expect you also need to update the documentation for the function parameter in the "Args" section of the docs too.
@@ -171,7 +171,10 @@ def run_and_log(u_boot_console, cmd, ignore_errors=False): """ if isinstance(cmd, str): cmd = cmd.split()
- runner = u_boot_console.log.get_runner(cmd[0], sys.stdout)
- try:
runner = u_boot_console_or_log.get_runner(cmd[0], sys.stdout)
- except:
runner = u_boot_console_or_log.log.get_runner(cmd[0], sys.stdout)
I don't like this because:
a) It duplicates the call to get_runner(), even though both calls are logically the same thing, just with different parameter values.
b) It can catch exceptions that occur inside get_runner(), and then potentially repeat that call.
Better would be:
if hasattr(u_boot_console_or_log, 'get_runner'): get_runner = u_boot_console_or_log.get_runner else: get_runner = u_boot_console_or_log.log.get_runner runner = get_runner(cmd[0], sys.stdout)
Same comment for the similar change in run_and_log_expect_exception(). You could perhaps even create a helper function get_get_runner(u_boot_console_or_log) to share the code.
participants (2)
-
Stefan Brüns
-
Stephen Warren