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 18 matching lines...) Expand all Loading... |
29 # Class for unittest support. Used for capturing stderr/stdout. | 29 # Class for unittest support. Used for capturing stderr/stdout. |
30 | 30 |
31 import logging | 31 import logging |
32 import sys | 32 import sys |
33 import unittest | 33 import unittest |
34 | 34 |
35 from StringIO import StringIO | 35 from StringIO import StringIO |
36 | 36 |
37 | 37 |
38 class OutputCapture(object): | 38 class OutputCapture(object): |
39 # By default we capture the output to a stream. Other modules may override | |
40 # this function in order to do things like pass through the output. See | |
41 # webkitpy.test.main for an example. | |
42 @staticmethod | |
43 def stream_wrapper(stream): | |
44 return StringIO() | |
45 | 39 |
46 def __init__(self): | 40 def __init__(self): |
47 self.saved_outputs = dict() | 41 self.saved_outputs = dict() |
48 self._log_level = logging.INFO | 42 self._log_level = logging.INFO |
49 | 43 |
50 def set_log_level(self, log_level): | 44 def set_log_level(self, log_level): |
51 self._log_level = log_level | 45 self._log_level = log_level |
52 if hasattr(self, '_logs_handler'): | 46 if hasattr(self, '_logs_handler'): |
53 self._logs_handler.setLevel(self._log_level) | 47 self._logs_handler.setLevel(self._log_level) |
54 | 48 |
55 def _capture_output_with_name(self, output_name): | 49 def _capture_output_with_name(self, output_name): |
56 stream = getattr(sys, output_name) | 50 stream = getattr(sys, output_name) |
57 captured_output = self.stream_wrapper(stream) | 51 captured_output = StringIO() |
58 self.saved_outputs[output_name] = stream | 52 self.saved_outputs[output_name] = stream |
59 setattr(sys, output_name, captured_output) | 53 setattr(sys, output_name, captured_output) |
60 return captured_output | 54 return captured_output |
61 | 55 |
62 def _restore_output_with_name(self, output_name): | 56 def _restore_output_with_name(self, output_name): |
63 captured_output = getattr(sys, output_name).getvalue() | 57 captured_output = getattr(sys, output_name).getvalue() |
64 setattr(sys, output_name, self.saved_outputs[output_name]) | 58 setattr(sys, output_name, self.saved_outputs[output_name]) |
65 del self.saved_outputs[output_name] | 59 del self.saved_outputs[output_name] |
66 return captured_output | 60 return captured_output |
67 | 61 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 del self.__captured_stdout | 114 del self.__captured_stdout |
121 del self.__captured_stderr | 115 del self.__captured_stderr |
122 self.output_capture.restore_output() | 116 self.output_capture.restore_output() |
123 unittest.TestCase.tearDown(self) | 117 unittest.TestCase.tearDown(self) |
124 | 118 |
125 def assertStdout(self, expected_stdout): | 119 def assertStdout(self, expected_stdout): |
126 self.assertEqual(expected_stdout, self.__captured_stdout.getvalue()) | 120 self.assertEqual(expected_stdout, self.__captured_stdout.getvalue()) |
127 | 121 |
128 def assertStderr(self, expected_stderr): | 122 def assertStderr(self, expected_stderr): |
129 self.assertEqual(expected_stderr, self.__captured_stderr.getvalue()) | 123 self.assertEqual(expected_stderr, self.__captured_stderr.getvalue()) |
OLD | NEW |