From 964fde2fef16a9b7741f60d88a1781d87005dca7 Mon Sep 17 00:00:00 2001 From: Marc Bonnici Date: Fri, 9 Aug 2019 15:16:25 +0100 Subject: [PATCH] utils/android: Echo the exit code of the actual command When executing a command using `su`, the `echo` command was returning the error code of the invocation of `su` rather than the command itself. Usually `su` should mimic the return code of the command it is executing however this is not always the case which can cause issues. --- devlib/utils/android.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/devlib/utils/android.py b/devlib/utils/android.py index 5a0c6d4..1f7a916 100755 --- a/devlib/utils/android.py +++ b/devlib/utils/android.py @@ -438,6 +438,14 @@ def _ping(device): def adb_shell(device, command, timeout=None, check_exit_code=False, as_root=False, adb_server=None, su_cmd='su -c {}'): # NOQA _check_env() + + # On older combinations of ADB/Android versions, the adb host command always + # exits with 0 if it was able to run the command on the target, even if the + # command failed (https://code.google.com/p/android/issues/detail?id=3254). + # Homogenise this behaviour by running the command then echoing the exit + # code of the executed command itself. + command += r' ; echo "\n$?"' + parts = ['adb'] if adb_server is not None: parts += ['-H', adb_server] @@ -447,12 +455,6 @@ def adb_shell(device, command, timeout=None, check_exit_code=False, command if not as_root else su_cmd.format(quote(command))] logger.debug(' '.join(quote(part) for part in parts)) - # On older combinations of ADB/Android versions, the adb host command always - # exits with 0 if it was able to run the command on the target, even if the - # command failed (https://code.google.com/p/android/issues/detail?id=3254). - # Homogenise this behaviour by running the command then echoing the exit - # code. - parts[-1] += ' ; echo "\n$?"' try: raw_output, _ = check_output(parts, timeout, shell=False, combined_output=True) except subprocess.CalledProcessError as e: