提交 6323ad5e 编写于 作者: L Lukáš Doktor 提交者: Cleber Rosa

test: Allow unicode in exception (and tests results in general)

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: NLukáš Doktor <ldoktor@redhat.com>
上级 1828ccef
......@@ -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 "
......
......@@ -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 = '<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,
......
......@@ -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):
......
......@@ -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__":
......
......@@ -14,6 +14,9 @@ class NastyException(Exception):
def __str__(self):
return self.msg
def __unicode__(self):
return self.msg
class FailTest(Test):
......
......@@ -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 = '<unknown>'
fail_reason = str(fail_reason)
fail_reason = astring.to_text(fail_reason)
if len(fail_reason) > exhibition_limit:
fail_reason = ('<a data-container="body" '
'data-toggle="popover" '
......
......@@ -3,7 +3,7 @@ import sys
import unittest
from avocado.core import exit_codes
from avocado.utils import process
from avocado.utils import process, astring
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
......@@ -24,7 +24,8 @@ class StandaloneTests(unittest.TestCase):
def run_and_check(self, cmd_line, expected_rc, tstname):
os.chdir(basedir)
result = process.run(cmd_line, ignore_status=True)
result = process.run(cmd_line, ignore_status=True,
encoding=astring.ENCODING)
self.assertEqual(result.exit_status, expected_rc,
"Stand alone %s did not return rc "
"%d:\n%s" % (tstname, expected_rc, result))
......@@ -50,10 +51,10 @@ class StandaloneTests(unittest.TestCase):
expected_rc = exit_codes.AVOCADO_TESTS_FAIL
result = self.run_and_check(cmd_line, expected_rc, 'errortest_nasty')
if sys.version_info[0] == 3:
exc = "errortest_nasty.NastyException: Nasty-string-like-exception"
exc = u"errortest_nasty.NastyException: Nasty-string-like-exception\u017e"
else:
exc = "NastyException: Nasty-string-like-exception"
count = result.stdout_text.count("\n%s" % exc)
exc = u"NastyException: Nasty-string-like-exception\\u017e"
count = result.stdout_text.count(u"\n%s" % exc)
self.assertEqual(count, 2, "Exception \\n%s should be present twice in"
"the log (once from the log, second time when parsing"
"exception details." % (exc))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册