diff mbox

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

Message ID 52fc650813aa4b498604a816e879f67e@rwthex-w2-b.rwth-ad.de
State Changes Requested
Delegated to: Tom Rini
Headers show

Commit Message

Stefan Brüns Nov. 5, 2016, 4:45 p.m. UTC
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(-)

Comments

Stephen Warren Nov. 6, 2016, 3:17 a.m. UTC | #1
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.
diff mbox

Patch

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)