shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2008, Google Inc. |
| 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are |
| 8 | # met: |
| 9 | # |
| 10 | # * Redistributions of source code must retain the above copyright |
| 11 | # notice, this list of conditions and the following disclaimer. |
| 12 | # * Redistributions in binary form must reproduce the above |
| 13 | # copyright notice, this list of conditions and the following disclaimer |
| 14 | # in the documentation and/or other materials provided with the |
| 15 | # distribution. |
| 16 | # * Neither the name of Google Inc. nor the names of its |
| 17 | # contributors may be used to endorse or promote products derived from |
| 18 | # this software without specific prior written permission. |
| 19 | # |
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | """Tests the text output of Google C++ Mocking Framework. |
| 33 | |
| 34 | SYNOPSIS |
| 35 | gmock_output_test.py --gmock_build_dir=BUILD/DIR --gengolden |
| 36 | # where BUILD/DIR contains the built gmock_output_test_ file. |
| 37 | gmock_output_test.py --gengolden |
| 38 | gmock_output_test.py |
| 39 | """ |
| 40 | |
| 41 | __author__ = 'wan@google.com (Zhanyong Wan)' |
| 42 | |
| 43 | import gmock_test_utils |
| 44 | import os |
| 45 | import re |
| 46 | import string |
| 47 | import sys |
| 48 | import unittest |
| 49 | |
| 50 | |
| 51 | # The flag for generating the golden file |
| 52 | GENGOLDEN_FLAG = '--gengolden' |
| 53 | |
| 54 | IS_WINDOWS = os.name == 'nt' |
| 55 | |
| 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' |
| 63 | GOLDEN_NAME = 'gmock_output_test_golden.txt' |
| 64 | GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), |
| 65 | GOLDEN_NAME) |
| 66 | |
| 67 | def ToUnixLineEnding(s): |
| 68 | """Changes all Windows/Mac line endings in s to UNIX line endings.""" |
| 69 | |
| 70 | return s.replace('\r\n', '\n').replace('\r', '\n') |
| 71 | |
| 72 | |
| 73 | def RemoveReportHeaderAndFooter(output): |
| 74 | """Removes Google Test result report's header and footer from the output.""" |
| 75 | |
| 76 | output = re.sub(r'.*gtest_main.*\n', '', output) |
| 77 | output = re.sub(r'\[.*\d+ tests.*\n', '', output) |
| 78 | output = re.sub(r'\[.* test environment .*\n', '', output) |
| 79 | output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output) |
| 80 | output = re.sub(r'.* FAILED TESTS\n', '', output) |
| 81 | return output |
| 82 | |
| 83 | |
| 84 | def RemoveLocations(output): |
| 85 | """Removes all file location info from a Google Test program's output. |
| 86 | |
| 87 | Args: |
| 88 | output: the output of a Google Test program. |
| 89 | |
| 90 | Returns: |
| 91 | output with all file location info (in the form of |
| 92 | 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or |
| 93 | 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by |
| 94 | 'FILE:#: '. |
| 95 | """ |
| 96 | |
| 97 | return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output) |
| 98 | |
| 99 | |
| 100 | def NormalizeErrorMarker(output): |
| 101 | """Normalizes the error marker, which is different on Windows vs on Linux.""" |
| 102 | |
| 103 | return re.sub(r' error: ', ' Failure\n', output) |
| 104 | |
| 105 | |
| 106 | def RemoveMemoryAddresses(output): |
| 107 | """Removes memory addresses from the test output.""" |
| 108 | |
| 109 | return re.sub(r'@\w+', '@0x#', output) |
| 110 | |
| 111 | |
| 112 | def NormalizeOutput(output): |
| 113 | """Normalizes output (the output of gmock_output_test_.exe).""" |
| 114 | |
| 115 | output = ToUnixLineEnding(output) |
| 116 | output = RemoveReportHeaderAndFooter(output) |
| 117 | output = NormalizeErrorMarker(output) |
| 118 | output = RemoveLocations(output) |
| 119 | output = RemoveMemoryAddresses(output) |
| 120 | return output |
| 121 | |
| 122 | |
| 123 | def IterShellCommandOutput(cmd, stdin_string=None): |
| 124 | """Runs a command in a sub-process, and iterates the lines in its STDOUT. |
| 125 | |
| 126 | Args: |
| 127 | |
| 128 | cmd: The shell command. |
| 129 | stdin_string: The string to be fed to the STDIN of the sub-process; |
| 130 | If None, the sub-process will inherit the STDIN |
| 131 | from the parent process. |
| 132 | """ |
| 133 | |
| 134 | # Spawns cmd in a sub-process, and gets its standard I/O file objects. |
| 135 | stdin_file, stdout_file = os.popen2(cmd, 'b') |
| 136 | |
| 137 | # If the caller didn't specify a string for STDIN, gets it from the |
| 138 | # parent process. |
| 139 | if stdin_string is None: |
| 140 | stdin_string = sys.stdin.read() |
| 141 | |
| 142 | # Feeds the STDIN string to the sub-process. |
| 143 | stdin_file.write(stdin_string) |
| 144 | stdin_file.close() |
| 145 | |
| 146 | while True: |
| 147 | line = stdout_file.readline() |
| 148 | if not line: # EOF |
| 149 | stdout_file.close() |
| 150 | break |
| 151 | |
| 152 | yield line |
| 153 | |
| 154 | |
| 155 | def GetShellCommandOutput(cmd, stdin_string=None): |
| 156 | """Runs a command in a sub-process, and returns its STDOUT in a string. |
| 157 | |
| 158 | Args: |
| 159 | |
| 160 | cmd: The shell command. |
| 161 | stdin_string: The string to be fed to the STDIN of the sub-process; |
| 162 | If None, the sub-process will inherit the STDIN |
| 163 | from the parent process. |
| 164 | """ |
| 165 | |
| 166 | lines = list(IterShellCommandOutput(cmd, stdin_string)) |
| 167 | return string.join(lines, '') |
| 168 | |
| 169 | |
| 170 | def GetCommandOutput(cmd): |
| 171 | """Runs a command and returns its output with all file location |
| 172 | info stripped off. |
| 173 | |
| 174 | Args: |
| 175 | cmd: the shell command. |
| 176 | """ |
| 177 | |
| 178 | # Disables exception pop-ups on Windows. |
| 179 | os.environ['GTEST_CATCH_EXCEPTIONS'] = '1' |
| 180 | return NormalizeOutput(GetShellCommandOutput(cmd, '')) |
| 181 | |
| 182 | |
| 183 | class GMockOutputTest(unittest.TestCase): |
| 184 | def testOutput(self): |
| 185 | output = GetCommandOutput(COMMAND) |
| 186 | golden_file = open(GOLDEN_PATH, 'rb') |
| 187 | golden = golden_file.read() |
| 188 | golden_file.close() |
| 189 | |
| 190 | self.assertEquals(golden, output) |
| 191 | |
| 192 | |
| 193 | if __name__ == '__main__': |
| 194 | if sys.argv[1:] == [GENGOLDEN_FLAG]: |
| 195 | output = GetCommandOutput(COMMAND) |
| 196 | golden_file = open(GOLDEN_PATH, 'wb') |
| 197 | golden_file.write(output) |
| 198 | golden_file.close() |
| 199 | else: |
| 200 | gmock_test_utils.Main() |