提交 7234c717 编写于 作者: L Lucas Meneghel Rodrigues

Merge pull request #789 from ldoktor/nasty_exc

avocado.core.test: Avoid passing ugly exceptions
......@@ -359,8 +359,9 @@ class Test(unittest.TestCase):
except exceptions.TestNAError, details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
raise exceptions.TestNAError(details)
except Exception, details:
except: # Old-style exceptions are not inherited from Exception()
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
details = sys.exc_info()[1]
raise exceptions.TestSetupFail(details)
try:
testMethod()
......@@ -371,8 +372,11 @@ class Test(unittest.TestCase):
'must fix your test. Original skip exception: '
'%s' % details)
raise exceptions.TestError(skip_illegal_msg)
except Exception, details:
except: # Old-style exceptions are not inherited from Exception()
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
details = sys.exc_info()[1]
if not isinstance(details, Exception): # Avoid passing nasty exc
details = exceptions.TestError("%r: %s" % (details, details))
test_exception = details
finally:
try:
......@@ -384,9 +388,10 @@ class Test(unittest.TestCase):
'you must fix your test. Original skip '
'exception: %s' % details)
raise exceptions.TestError(skip_illegal_msg)
except Exception, details:
except: # avoid old-style exception failures
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
cleanup_exception = details
details = sys.exc_info()[1]
cleanup_exception = exceptions.TestSetupFail(details)
whiteboard_file = os.path.join(self.logdir, 'whiteboard')
genio.write_file(whiteboard_file, self.whiteboard)
......@@ -423,7 +428,7 @@ class Test(unittest.TestCase):
if test_exception is not None:
raise test_exception
elif cleanup_exception is not None:
raise exceptions.TestSetupFail(cleanup_exception)
raise cleanup_exception
elif stdout_check_exception is not None:
raise stdout_check_exception
elif stderr_check_exception is not None:
......
#!/usr/bin/python
from avocado import Test
from avocado import main
class NastyException:
""" Please never use something like this!!! (old-style exception) """
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class FailTest(Test):
"""
This test raises old-style-class exception
"""
def test(self):
"""
Should fail not-that-badly
"""
raise NastyException("Nasty-string-like-exception")
if __name__ == "__main__":
main()
......@@ -62,6 +62,13 @@ class StandaloneTests(unittest.TestCase):
self.assertIn("Exception: Unable to get exception, check the traceback"
" for details.", result.stdout)
def test_failtest_nasty3(self):
cmd_line = './examples/tests/failtest_nasty3.py -r'
expected_rc = 1
result = self.run_and_check(cmd_line, expected_rc, 'failtest_nasty3')
self.assertIn("TestError: <failtest_nasty3.NastyException instance at ",
result.stdout)
def test_errortest(self):
cmd_line = './examples/tests/errortest.py -r'
expected_rc = 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册