Bug 1529077 - [marionette] Encode Unicode error messages as UTF-8. r=ato

Differential Revision: https://phabricator.services.mozilla.com/D20434

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2019-02-20 09:59:25 +00:00
Родитель d316a3043b
Коммит 9304ea6d30
2 изменённых файлов: 37 добавлений и 14 удалений

Просмотреть файл

@ -33,22 +33,26 @@ class MarionetteException(Exception):
super(MarionetteException, self).__init__(message)
def __str__(self):
msg = str(self.message)
return unicode(self).encode("utf-8")
def __unicode__(self):
msg = unicode(self.message)
tb = None
if self.cause:
if type(self.cause) is tuple:
msg += ", caused by {0!r}".format(self.cause[0])
msg += u", caused by {0!r}".format(self.cause[0])
tb = self.cause[2]
else:
msg += ", caused by {}".format(self.cause)
msg += u", caused by {}".format(self.cause)
if self.stacktrace:
st = "".join(["\t{}\n".format(x)
for x in self.stacktrace.splitlines()])
msg += "\nstacktrace:\n{}".format(st)
st = u"".join(["\t{}\n".format(x)
for x in self.stacktrace.splitlines()])
msg += u"\nstacktrace:\n{}".format(st)
if tb:
msg += ': ' + "".join(traceback.format_tb(tb))
msg += u": " + u"".join(traceback.format_tb(tb))
return msg

Просмотреть файл

@ -14,14 +14,17 @@ from marionette_harness import marionette_test
def fake_cause():
try:
raise ValueError("bar")
except ValueError as e:
except ValueError:
return sys.exc_info()
message = "foo"
unicode_message = u"\u201Cfoo"
cause = fake_cause()
stacktrace = "first\nsecond"
class TestErrors(marionette_test.MarionetteTestCase):
def test_defaults(self):
exc = errors.MarionetteException()
self.assertIsNone(exc.message)
@ -35,7 +38,7 @@ class TestErrors(marionette_test.MarionetteTestCase):
self.assertEquals(exc.cause, cause)
self.assertEquals(exc.stacktrace, stacktrace)
def test_str(self):
def test_str_message(self):
exc = errors.MarionetteException(
message=message, cause=cause, stacktrace=stacktrace)
r = str(exc)
@ -43,6 +46,22 @@ class TestErrors(marionette_test.MarionetteTestCase):
self.assertIn(", caused by {0!r}".format(cause[0]), r)
self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r)
def test_unicode_message(self):
exc = errors.MarionetteException(
message=unicode_message, cause=cause, stacktrace=stacktrace)
r = unicode(exc)
self.assertIn(unicode_message, r)
self.assertIn(", caused by {0!r}".format(cause[0]), r)
self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r)
def test_unicode_message_as_str(self):
exc = errors.MarionetteException(
message=unicode_message, cause=cause, stacktrace=stacktrace)
r = str(exc)
self.assertIn(unicode_message.encode("utf-8"), r)
self.assertIn(", caused by {0!r}".format(cause[0]), r)
self.assertIn("\nstacktrace:\n\tfirst\n\tsecond", r)
def test_cause_string(self):
exc = errors.MarionetteException(cause="foo")
self.assertEqual(exc.cause, "foo")
@ -57,23 +76,23 @@ class TestErrors(marionette_test.MarionetteTestCase):
class TestLookup(marionette_test.MarionetteTestCase):
def test_by_unknown_number(self):
self.assertEqual(errors.MarionetteException, errors.lookup(123456))
def test_by_known_string(self):
self.assertEqual(errors.NoSuchElementException,
errors.lookup("no such element"))
self.assertEqual(errors.NoSuchElementException, errors.lookup("no such element"))
def test_by_unknown_string(self):
self.assertEqual(errors.MarionetteException, errors.lookup("barbera"))
def test_by_known_unicode_string(self):
self.assertEqual(errors.NoSuchElementException,
errors.lookup(u"no such element"))
self.assertEqual(errors.NoSuchElementException, errors.lookup(u"no such element"))
class TestAllErrors(marionette_test.MarionetteTestCase):
def test_properties(self):
for exc in errors.es_:
self.assertTrue(hasattr(exc, "status"),
"expected exception to have attribute `status'")
"expected exception to have attribute `status'")