| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # Copyright 2008, Google Inc. | 3 # Copyright 2008, Google Inc. |
| 4 # All rights reserved. | 4 # All rights reserved. |
| 5 # | 5 # |
| 6 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
| 7 # modification, are permitted provided that the following conditions are | 7 # modification, are permitted provided that the following conditions are |
| 8 # met: | 8 # met: |
| 9 # | 9 # |
| 10 # * Redistributions of source code must retain the above copyright | 10 # * Redistributions of source code must retain the above copyright |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 SYNOPSIS | 34 SYNOPSIS |
| 35 gmock_output_test.py --gmock_build_dir=BUILD/DIR --gengolden | 35 gmock_output_test.py --gmock_build_dir=BUILD/DIR --gengolden |
| 36 # where BUILD/DIR contains the built gmock_output_test_ file. | 36 # where BUILD/DIR contains the built gmock_output_test_ file. |
| 37 gmock_output_test.py --gengolden | 37 gmock_output_test.py --gengolden |
| 38 gmock_output_test.py | 38 gmock_output_test.py |
| 39 """ | 39 """ |
| 40 | 40 |
| 41 __author__ = 'wan@google.com (Zhanyong Wan)' | 41 __author__ = 'wan@google.com (Zhanyong Wan)' |
| 42 | 42 |
| 43 import gmock_test_utils | |
| 44 import os | 43 import os |
| 45 import re | 44 import re |
| 46 import string | |
| 47 import sys | 45 import sys |
| 48 import unittest | 46 |
| 47 import gmock_test_utils |
| 49 | 48 |
| 50 | 49 |
| 51 # The flag for generating the golden file | 50 # The flag for generating the golden file |
| 52 GENGOLDEN_FLAG = '--gengolden' | 51 GENGOLDEN_FLAG = '--gengolden' |
| 53 | 52 |
| 54 IS_WINDOWS = os.name == 'nt' | 53 PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_') |
| 55 | 54 COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0'] |
| 56 if IS_WINDOWS: | |
| 57 PROGRAM = r'..\build.dbg\gmock_output_test_.exe' | |
| 58 else: | |
| 59 PROGRAM = 'gmock_output_test_' | |
| 60 | |
| 61 PROGRAM_PATH = os.path.join(gmock_test_utils.GetBuildDir(), PROGRAM) | |
| 62 COMMAND = PROGRAM_PATH + ' --gtest_stack_trace_depth=0 --gtest_print_time=0' | |
| 63 GOLDEN_NAME = 'gmock_output_test_golden.txt' | 55 GOLDEN_NAME = 'gmock_output_test_golden.txt' |
| 64 GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), | 56 GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME) |
| 65 GOLDEN_NAME) | |
| 66 | 57 |
| 67 | 58 |
| 68 def ToUnixLineEnding(s): | 59 def ToUnixLineEnding(s): |
| 69 """Changes all Windows/Mac line endings in s to UNIX line endings.""" | 60 """Changes all Windows/Mac line endings in s to UNIX line endings.""" |
| 70 | 61 |
| 71 return s.replace('\r\n', '\n').replace('\r', '\n') | 62 return s.replace('\r\n', '\n').replace('\r', '\n') |
| 72 | 63 |
| 73 | 64 |
| 74 def RemoveReportHeaderAndFooter(output): | 65 def RemoveReportHeaderAndFooter(output): |
| 75 """Removes Google Test result report's header and footer from the output.""" | 66 """Removes Google Test result report's header and footer from the output.""" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 """ | 128 """ |
| 138 | 129 |
| 139 output = ToUnixLineEnding(output) | 130 output = ToUnixLineEnding(output) |
| 140 output = RemoveReportHeaderAndFooter(output) | 131 output = RemoveReportHeaderAndFooter(output) |
| 141 output = NormalizeErrorMarker(output) | 132 output = NormalizeErrorMarker(output) |
| 142 output = RemoveLocations(output) | 133 output = RemoveLocations(output) |
| 143 output = RemoveMemoryAddresses(output) | 134 output = RemoveMemoryAddresses(output) |
| 144 return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output)) | 135 return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output)) |
| 145 | 136 |
| 146 | 137 |
| 147 def IterShellCommandOutput(cmd, stdin_string=None): | 138 def GetShellCommandOutput(cmd): |
| 148 """Runs a command in a sub-process, and iterates the lines in its STDOUT. | 139 """Runs a command in a sub-process, and returns its STDOUT in a string.""" |
| 149 | 140 |
| 150 Args: | 141 return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output |
| 151 | |
| 152 cmd: The shell command. | |
| 153 stdin_string: The string to be fed to the STDIN of the sub-process; | |
| 154 If None, the sub-process will inherit the STDIN | |
| 155 from the parent process. | |
| 156 """ | |
| 157 | |
| 158 # Spawns cmd in a sub-process, and gets its standard I/O file objects. | |
| 159 stdin_file, stdout_file = os.popen2(cmd, 'b') | |
| 160 | |
| 161 # If the caller didn't specify a string for STDIN, gets it from the | |
| 162 # parent process. | |
| 163 if stdin_string is None: | |
| 164 stdin_string = sys.stdin.read() | |
| 165 | |
| 166 # Feeds the STDIN string to the sub-process. | |
| 167 stdin_file.write(stdin_string) | |
| 168 stdin_file.close() | |
| 169 | |
| 170 while True: | |
| 171 line = stdout_file.readline() | |
| 172 if not line: # EOF | |
| 173 stdout_file.close() | |
| 174 break | |
| 175 | |
| 176 yield line | |
| 177 | |
| 178 | |
| 179 def GetShellCommandOutput(cmd, stdin_string=None): | |
| 180 """Runs a command in a sub-process, and returns its STDOUT in a string. | |
| 181 | |
| 182 Args: | |
| 183 | |
| 184 cmd: The shell command. | |
| 185 stdin_string: The string to be fed to the STDIN of the sub-process; | |
| 186 If None, the sub-process will inherit the STDIN | |
| 187 from the parent process. | |
| 188 """ | |
| 189 | |
| 190 lines = list(IterShellCommandOutput(cmd, stdin_string)) | |
| 191 return string.join(lines, '') | |
| 192 | 142 |
| 193 | 143 |
| 194 def GetNormalizedCommandOutputAndLeakyTests(cmd): | 144 def GetNormalizedCommandOutputAndLeakyTests(cmd): |
| 195 """Runs a command and returns its normalized output and a list of leaky tests. | 145 """Runs a command and returns its normalized output and a list of leaky tests. |
| 196 | 146 |
| 197 Args: | 147 Args: |
| 198 cmd: the shell command. | 148 cmd: the shell command. |
| 199 """ | 149 """ |
| 200 | 150 |
| 201 # Disables exception pop-ups on Windows. | 151 # Disables exception pop-ups on Windows. |
| 202 os.environ['GTEST_CATCH_EXCEPTIONS'] = '1' | 152 os.environ['GTEST_CATCH_EXCEPTIONS'] = '1' |
| 203 return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd, '')) | 153 return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd)) |
| 204 | 154 |
| 205 | 155 |
| 206 class GMockOutputTest(unittest.TestCase): | 156 class GMockOutputTest(gmock_test_utils.TestCase): |
| 207 def testOutput(self): | 157 def testOutput(self): |
| 208 (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) | 158 (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) |
| 209 golden_file = open(GOLDEN_PATH, 'rb') | 159 golden_file = open(GOLDEN_PATH, 'rb') |
| 210 golden = golden_file.read() | 160 golden = golden_file.read() |
| 211 golden_file.close() | 161 golden_file.close() |
| 212 | 162 |
| 213 # The normalized output should match the golden file. | 163 # The normalized output should match the golden file. |
| 214 self.assertEquals(golden, output) | 164 self.assertEquals(golden, output) |
| 215 | 165 |
| 216 # The raw output should contain 2 leaked mock object errors for | 166 # The raw output should contain 2 leaked mock object errors for |
| 217 # test GMockOutputTest.CatchesLeakedMocks. | 167 # test GMockOutputTest.CatchesLeakedMocks. |
| 218 self.assertEquals(['GMockOutputTest.CatchesLeakedMocks', | 168 self.assertEquals(['GMockOutputTest.CatchesLeakedMocks', |
| 219 'GMockOutputTest.CatchesLeakedMocks'], | 169 'GMockOutputTest.CatchesLeakedMocks'], |
| 220 leaky_tests) | 170 leaky_tests) |
| 221 | 171 |
| 222 | 172 |
| 223 if __name__ == '__main__': | 173 if __name__ == '__main__': |
| 224 if sys.argv[1:] == [GENGOLDEN_FLAG]: | 174 if sys.argv[1:] == [GENGOLDEN_FLAG]: |
| 225 (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) | 175 (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) |
| 226 golden_file = open(GOLDEN_PATH, 'wb') | 176 golden_file = open(GOLDEN_PATH, 'wb') |
| 227 golden_file.write(output) | 177 golden_file.write(output) |
| 228 golden_file.close() | 178 golden_file.close() |
| 229 else: | 179 else: |
| 230 gmock_test_utils.Main() | 180 gmock_test_utils.Main() |
| OLD | NEW |