| OLD | NEW |
| 1 # Copyright (c) 2009, Google Inc. All rights reserved. | 1 # Copyright (c) 2009, Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 import sys | 32 import sys |
| 33 | 33 |
| 34 from StringIO import StringIO | 34 from StringIO import StringIO |
| 35 | 35 |
| 36 | 36 |
| 37 class OutputCapture(object): | 37 class OutputCapture(object): |
| 38 | 38 |
| 39 def __init__(self): | 39 def __init__(self): |
| 40 self.saved_outputs = dict() | 40 self.saved_outputs = dict() |
| 41 self._log_level = logging.INFO | 41 self._log_level = logging.INFO |
| 42 self._logs = None |
| 43 self._logs_handler = None |
| 44 self._logger = None |
| 45 self._orig_log_level = None |
| 42 | 46 |
| 43 def set_log_level(self, log_level): | 47 def set_log_level(self, log_level): |
| 44 self._log_level = log_level | 48 self._log_level = log_level |
| 45 if hasattr(self, '_logs_handler'): | 49 if hasattr(self, '_logs_handler'): |
| 46 self._logs_handler.setLevel(self._log_level) | 50 self._logs_handler.setLevel(self._log_level) |
| 47 | 51 |
| 48 def _capture_output_with_name(self, output_name): | 52 def _capture_output_with_name(self, output_name): |
| 49 stream = getattr(sys, output_name) | 53 stream = getattr(sys, output_name) |
| 50 captured_output = StringIO() | 54 captured_output = StringIO() |
| 51 self.saved_outputs[output_name] = stream | 55 self.saved_outputs[output_name] = stream |
| (...skipping 19 matching lines...) Expand all Loading... |
| 71 def restore_output(self): | 75 def restore_output(self): |
| 72 self._logger.removeHandler(self._logs_handler) | 76 self._logger.removeHandler(self._logs_handler) |
| 73 self._logger.setLevel(self._orig_log_level) | 77 self._logger.setLevel(self._orig_log_level) |
| 74 self._logs_handler.flush() | 78 self._logs_handler.flush() |
| 75 self._logs.flush() | 79 self._logs.flush() |
| 76 logs_string = self._logs.getvalue() | 80 logs_string = self._logs.getvalue() |
| 77 delattr(self, '_logs_handler') | 81 delattr(self, '_logs_handler') |
| 78 delattr(self, '_logs') | 82 delattr(self, '_logs') |
| 79 return (self._restore_output_with_name("stdout"), self._restore_output_w
ith_name("stderr"), logs_string) | 83 return (self._restore_output_with_name("stdout"), self._restore_output_w
ith_name("stderr"), logs_string) |
| 80 | 84 |
| 81 def assert_outputs(self, testcase, function, args=[], kwargs={}, expected_st
dout="", | 85 def assert_outputs(self, testcase, function, args=None, kwargs=None, expecte
d_stdout="", |
| 82 expected_stderr="", expected_exception=None, expected_log
s=None): | 86 expected_stderr="", expected_exception=None, expected_log
s=None): |
| 87 args = args or [] |
| 88 kwargs = kwargs or {} |
| 83 self.capture_output() | 89 self.capture_output() |
| 84 try: | 90 try: |
| 85 if expected_exception: | 91 if expected_exception: |
| 86 return_value = testcase.assertRaises(expected_exception, functio
n, *args, **kwargs) | 92 return_value = testcase.assertRaises(expected_exception, functio
n, *args, **kwargs) |
| 87 else: | 93 else: |
| 88 return_value = function(*args, **kwargs) | 94 return_value = function(*args, **kwargs) |
| 89 finally: | 95 finally: |
| 90 (stdout_string, stderr_string, logs_string) = self.restore_output() | 96 (stdout_string, stderr_string, logs_string) = self.restore_output() |
| 91 | 97 |
| 92 if hasattr(testcase, 'assertMultiLineEqual'): | 98 if hasattr(testcase, 'assertMultiLineEqual'): |
| 93 testassert = testcase.assertMultiLineEqual | 99 testassert = testcase.assertMultiLineEqual |
| 94 else: | 100 else: |
| 95 testassert = testcase.assertEqual | 101 testassert = testcase.assertEqual |
| 96 | 102 |
| 97 testassert(stdout_string, expected_stdout) | 103 testassert(stdout_string, expected_stdout) |
| 98 testassert(stderr_string, expected_stderr) | 104 testassert(stderr_string, expected_stderr) |
| 99 if expected_logs is not None: | 105 if expected_logs is not None: |
| 100 testassert(logs_string, expected_logs) | 106 testassert(logs_string, expected_logs) |
| 101 # This is a little strange, but I don't know where else to return this i
nformation. | 107 # This is a little strange, but I don't know where else to return this i
nformation. |
| 102 return return_value | 108 return return_value |
| OLD | NEW |