diff --git a/avocado/utils/process.py b/avocado/utils/process.py index d68431e969e2a0f31564d596ad328a34f438d2d3..6f38e22e09b70bdf300cd69635d3da614b0bf1f0 100644 --- a/avocado/utils/process.py +++ b/avocado/utils/process.py @@ -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): """ diff --git a/selftests/unit/test_utils_process.py b/selftests/unit/test_utils_process.py index 4577e04e807753e582efb200f61d9f7efe5823fd..7feb02d414833c543b9d68a870bda870a8070e90 100644 --- a/selftests/unit/test_utils_process.py +++ b/selftests/unit/test_utils_process.py @@ -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):