提交 fccf2b20 编写于 作者: J Junxiang Li 提交者: Lukáš Doktor

Provide "__str__" for "CmdError"

Let's provide the "__str__" method for "CmdError" to be able to see the
actual content on print(error).
Signed-off-by: NJunxiang Li <junli@redhat.com>
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 d0fb1f15
......@@ -82,6 +82,10 @@ class CmdError(Exception):
self.result = result
self.additional_text = additional_text
def __str__(self):
return ("Command '%s' failed.\nstdout: %r\nstderr: %r\nadditional_info: %s" %
(self.command, self.result.stdout, self.result.stderr, self.additional_text))
def can_sudo(cmd=None):
"""
......
......@@ -15,7 +15,7 @@ from avocado.utils import gdb
from avocado.utils import process
from avocado.utils import path
from six import string_types
from six import string_types, PY2
def probe_binary(binary):
......@@ -306,6 +306,23 @@ class CmdResultTests(unittest.TestCase):
self.assertRaises(TypeError, lambda x: result.stderr_text)
class CmdErrorTests(unittest.TestCase):
def test_nasty_str(self):
result = process.CmdResult("ls", b"unicode_follows: \xc5\xa1",
b"cp1250 follows: \xfd", 1, 2, 3,
"wrong_encoding")
err = process.CmdError("ls", result, "please don't crash")
if PY2:
prefix = ''
else:
prefix = 'b'
self.assertEqual(str(err), "Command 'ls' failed.\nstdout: "
"%s'unicode_follows: \\xc5\\xa1'\nstderr: "
"%s'cp1250 follows: \\xfd'\nadditional_info: "
"please don't crash" % (prefix, prefix))
class FDDrainerTests(unittest.TestCase):
def test_drain_from_pipe_fd(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册