google / mobly

E2E test framework for tests with complex environment requirements.
https://github.com/google/mobly
Apache License 2.0
634 stars 177 forks source link

Add extra log for debug when UnicodeDecodeError happened #674

Closed williamfzc closed 4 years ago

williamfzc commented 4 years ago

Hi This patch mainly related to some encoding/decoding things. When I run mobly without fastboot or adb installed, mobly will correctly raise an error, everything is fine under English env.

But if we use Windows (gbk) + other languages (Chinese? etc.), because of calling command via subprocess, stdout/stderr may contains some unexpected words in specfic language, not utf8. These words will cause decode error.

File "f:\workspace\github\mobly\mobly\controllers\android_device.py", line 797, in is_rootable
    return not self.is_bootloader and self.build_info['debuggable'] == '1'
  File "f:\workspace\github\mobly\mobly\controllers\android_device.py", line 782, in is_bootloader
    return self.serial in list_fastboot_devices()
  File "f:\workspace\github\mobly\mobly\controllers\android_device.py", line 243, in list_fastboot_devices
    return parse_device_list(out, 'fastboot')
  File "f:\workspace\github\mobly\mobly\controllers\android_device.py", line 197, in parse_device_list
    raise e
  File "f:\workspace\github\mobly\mobly\controllers\android_device.py", line 194, in parse_device_list
    clean_lines = new_str(device_list_str, 'utf-8').strip().split('\n')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 11: invalid start byte
[SampleTestBed] 07-16 16:00:54.935 ERROR Error in HelloWorldTest#setup_class.

It raises an unicode expection but actually it caused by some lost deps. It's a little confused for debug. After 30 mins review finally I realized that I forgot my fastboot.

This patch will simply print out its origin value to stderr before new_str. Looks like:

[SampleTestBed] 07-16 16:00:54.873 INFO ==========> HelloWorldTest <==========
f:\workspace\github\mobly\mobly\controllers\android_device.py:196: UserWarning: unicode decode error, origin str: b"'fastboot' \xb2\xbb\xca\xc7\xc4\xda\xb2\xbf\xbb\xf
2\xcd\xe2\xb2\xbf\xc3\xfc\xc1\xee\xa3\xac\xd2\xb2\xb2\xbb\xca\xc7\xbf\xc9\xd4\xcb\xd0\xd0\xb5\xc4\xb3\xcc\xd0\xf2\r\n\xbb\xf2\xc5\xfa\xb4\xa6\xc0\xed\xce\xc4\xbc\xfe\
xa1\xa3\r\n"
  warnings.warn("unicode decode error, origin str: {}".format(device_list_str))

Maybe not the best design but at least I can know something wrong when I calling fastboot via command line. Just an idea/draft so feel free to edit these.


This change is Reviewable

williamfzc commented 4 years ago

no need to get this as e. directly raise instead of raise e should work

yes but maybe more readable?

isn't logging.warn sufficient?

yes but I did not find any logging usage in this project so use warning instead. I'd replace it if you prefer logging.

logging's built-in string formatting instead

ok

xpconanfan commented 4 years ago

Please reply in Reviewable if you can

williamfzc commented 4 years ago

Please reply in Reviewable if you can

image

ah, unfortunately I can't.

williamfzc commented 4 years ago

Unit test failing in PY2. Please modify so the test works both in PY2 and PY3. https://travis-ci.org/github/google/mobly/jobs/709238422

ok and I have forgotten PY2 for a long time lol

xpconanfan commented 4 years ago

Thanks!