From 6323ad5e03d088e585dd8319a1dad9538d055271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Tue, 1 May 2018 12:52:26 +0200 Subject: [PATCH] test: Allow unicode in exception (and tests results in general) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently Avocado crashes on py2 when the exception contains unicode. Let's use the new "astring.to_text" method to safely get the exception details. As this exception is part of the results, this commit also fixes the result plugins to cope with unicodes in them. Signed-off-by: Lukáš Doktor --- avocado/core/test.py | 8 ++++---- avocado/plugins/jsonresult.py | 3 ++- avocado/plugins/xunit.py | 4 ++-- examples/tests/errortest_nasty.py | 2 +- examples/tests/errortest_nasty2.py | 3 +++ optional_plugins/html/avocado_result_html/__init__.py | 3 ++- selftests/functional/test_standalone.py | 11 ++++++----- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/avocado/core/test.py b/avocado/core/test.py index fe802db9..8d14261c 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -997,20 +997,20 @@ class Test(unittest.TestCase, TestData): except exceptions.TestBaseException as detail: self.__status = detail.status self.__fail_class = detail.__class__.__name__ - self.__fail_reason = str(detail) + self.__fail_reason = astring.to_text(detail) self.__traceback = stacktrace.prepare_exc_info(sys.exc_info()) except AssertionError as detail: self.__status = 'FAIL' self.__fail_class = detail.__class__.__name__ - self.__fail_reason = str(detail) + self.__fail_reason = astring.to_text(detail) self.__traceback = stacktrace.prepare_exc_info(sys.exc_info()) except Exception as detail: self.__status = 'ERROR' tb_info = stacktrace.tb_info(sys.exc_info()) self.__traceback = stacktrace.prepare_exc_info(sys.exc_info()) try: - self.__fail_class = str(detail.__class__.__name__) - self.__fail_reason = str(detail) + self.__fail_class = astring.to_text(detail.__class__.__name__) + self.__fail_reason = astring.to_text(detail) except TypeError: self.__fail_class = "Exception" self.__fail_reason = ("Unable to get exception, check the " diff --git a/avocado/plugins/jsonresult.py b/avocado/plugins/jsonresult.py index 5803097a..16fb182f 100644 --- a/avocado/plugins/jsonresult.py +++ b/avocado/plugins/jsonresult.py @@ -23,6 +23,7 @@ import os from avocado.core.output import LOG_UI from avocado.core.parser import FileOrStdoutAction from avocado.core.plugin_interfaces import CLI, Result +from avocado.utils import astring UNKNOWN = '' @@ -44,7 +45,7 @@ class JSONResult(Result): 'whiteboard': test.get('whiteboard', UNKNOWN), 'logdir': test.get('logdir', UNKNOWN), 'logfile': test.get('logfile', UNKNOWN), - 'fail_reason': str(test.get('fail_reason', UNKNOWN))}) + 'fail_reason': astring.to_text(test.get('fail_reason', UNKNOWN))}) content = {'job_id': result.job_unique_id, 'debuglog': result.logfile, 'tests': tests, diff --git a/avocado/plugins/xunit.py b/avocado/plugins/xunit.py index 743d6a2d..e1a58307 100644 --- a/avocado/plugins/xunit.py +++ b/avocado/plugins/xunit.py @@ -23,7 +23,7 @@ from xml.dom.minidom import Document from avocado.core.parser import FileOrStdoutAction from avocado.core.output import LOG_UI from avocado.core.plugin_interfaces import CLI, Result -from avocado.utils import data_structures +from avocado.utils import astring, data_structures class XUnitResult(Result): @@ -36,7 +36,7 @@ class XUnitResult(Result): def _escape_attr(self, attrib): attrib = ''.join(_ if _ in self.PRINTABLE else "\\x%02x" % ord(_) - for _ in str(attrib)) + for _ in astring.to_text(attrib, encoding='utf-8')) return attrib def _escape_cdata(self, cdata): diff --git a/examples/tests/errortest_nasty.py b/examples/tests/errortest_nasty.py index 19878f4c..6834d7c1 100755 --- a/examples/tests/errortest_nasty.py +++ b/examples/tests/errortest_nasty.py @@ -27,7 +27,7 @@ class FailTest(Test): """ Avocado should report this as TestError. """ - raise NastyException("Nasty-string-like-exception") + raise NastyException(u"Nasty-string-like-exception\u017e") if __name__ == "__main__": diff --git a/examples/tests/errortest_nasty2.py b/examples/tests/errortest_nasty2.py index 2743a9c5..970232a6 100755 --- a/examples/tests/errortest_nasty2.py +++ b/examples/tests/errortest_nasty2.py @@ -14,6 +14,9 @@ class NastyException(Exception): def __str__(self): return self.msg + def __unicode__(self): + return self.msg + class FailTest(Test): diff --git a/optional_plugins/html/avocado_result_html/__init__.py b/optional_plugins/html/avocado_result_html/__init__.py index 2eca3eec..e8093115 100644 --- a/optional_plugins/html/avocado_result_html/__init__.py +++ b/optional_plugins/html/avocado_result_html/__init__.py @@ -29,6 +29,7 @@ import pystache from avocado.core import exit_codes from avocado.core.output import LOG_UI from avocado.core.plugin_interfaces import CLI, Result +from avocado.utils import astring class ReportModel(object): @@ -156,7 +157,7 @@ class ReportModel(object): fail_reason = tst.get('fail_reason') if fail_reason is None: fail_reason = '' - fail_reason = str(fail_reason) + fail_reason = astring.to_text(fail_reason) if len(fail_reason) > exhibition_limit: fail_reason = ('