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 | |
Gennadiy Civil | 5eb2635 | 2018-08-09 15:24:43 -0400 | [diff] [blame] | 32 | r"""Tests the text output of Google C++ Mocking Framework. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 33 | |
Gennadiy Civil | ec7faa9 | 2018-02-09 10:41:09 -0500 | [diff] [blame] | 34 | To update the golden file: |
| 35 | gmock_output_test.py --build_dir=BUILD/DIR --gengolden |
Gennadiy Civil | 063a90b | 2018-08-09 10:51:49 -0400 | [diff] [blame] | 36 | where BUILD/DIR contains the built gmock_output_test_ file. |
Gennadiy Civil | ec7faa9 | 2018-02-09 10:41:09 -0500 | [diff] [blame] | 37 | gmock_output_test.py --gengolden |
| 38 | gmock_output_test.py |
Gennadiy Civil | 063a90b | 2018-08-09 10:51:49 -0400 | [diff] [blame] | 39 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 40 | """ |
| 41 | |
Tom Hughes | d1ad27e | 2023-01-25 09:13:35 -0800 | [diff] [blame] | 42 | from io import open # pylint: disable=redefined-builtin, g-importing-member |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 43 | import os |
| 44 | import re |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 45 | import sys |
Derek Mauro | c58f562 | 2021-12-22 13:00:44 -0800 | [diff] [blame] | 46 | from googlemock.test import gmock_test_utils |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 47 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 48 | |
| 49 | # The flag for generating the golden file |
| 50 | GENGOLDEN_FLAG = '--gengolden' |
| 51 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 52 | PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_') |
| 53 | COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0'] |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 54 | GOLDEN_NAME = 'gmock_output_test_golden.txt' |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 55 | GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 56 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 57 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 58 | def ToUnixLineEnding(s): |
| 59 | """Changes all Windows/Mac line endings in s to UNIX line endings.""" |
| 60 | |
| 61 | return s.replace('\r\n', '\n').replace('\r', '\n') |
| 62 | |
| 63 | |
| 64 | def RemoveReportHeaderAndFooter(output): |
| 65 | """Removes Google Test result report's header and footer from the output.""" |
| 66 | |
| 67 | output = re.sub(r'.*gtest_main.*\n', '', output) |
| 68 | output = re.sub(r'\[.*\d+ tests.*\n', '', output) |
| 69 | output = re.sub(r'\[.* test environment .*\n', '', output) |
| 70 | output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output) |
| 71 | output = re.sub(r'.* FAILED TESTS\n', '', output) |
| 72 | return output |
| 73 | |
| 74 | |
| 75 | def RemoveLocations(output): |
| 76 | """Removes all file location info from a Google Test program's output. |
| 77 | |
| 78 | Args: |
| 79 | output: the output of a Google Test program. |
| 80 | |
| 81 | Returns: |
| 82 | output with all file location info (in the form of |
| 83 | 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or |
| 84 | 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by |
| 85 | 'FILE:#: '. |
| 86 | """ |
| 87 | |
| 88 | return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output) |
| 89 | |
| 90 | |
| 91 | def NormalizeErrorMarker(output): |
| 92 | """Normalizes the error marker, which is different on Windows vs on Linux.""" |
| 93 | |
| 94 | return re.sub(r' error: ', ' Failure\n', output) |
| 95 | |
| 96 | |
| 97 | def RemoveMemoryAddresses(output): |
| 98 | """Removes memory addresses from the test output.""" |
| 99 | |
| 100 | return re.sub(r'@\w+', '@0x#', output) |
| 101 | |
| 102 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 103 | def RemoveTestNamesOfLeakedMocks(output): |
| 104 | """Removes the test names of leaked mock objects from the test output.""" |
| 105 | |
| 106 | return re.sub(r'\(used in test .+\) ', '', output) |
| 107 | |
| 108 | |
| 109 | def GetLeakyTests(output): |
| 110 | """Returns a list of test names that leak mock objects.""" |
| 111 | |
| 112 | # findall() returns a list of all matches of the regex in output. |
| 113 | # For example, if '(used in test FooTest.Bar)' is in output, the |
| 114 | # list will contain 'FooTest.Bar'. |
| 115 | return re.findall(r'\(used in test (.+)\)', output) |
| 116 | |
| 117 | |
| 118 | def GetNormalizedOutputAndLeakyTests(output): |
| 119 | """Normalizes the output of gmock_output_test_. |
| 120 | |
| 121 | Args: |
| 122 | output: The test output. |
| 123 | |
| 124 | Returns: |
| 125 | A tuple (the normalized test output, the list of test names that have |
| 126 | leaked mocks). |
| 127 | """ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 128 | |
| 129 | output = ToUnixLineEnding(output) |
| 130 | output = RemoveReportHeaderAndFooter(output) |
| 131 | output = NormalizeErrorMarker(output) |
| 132 | output = RemoveLocations(output) |
| 133 | output = RemoveMemoryAddresses(output) |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 134 | return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output)) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 135 | |
| 136 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 137 | def GetShellCommandOutput(cmd): |
| 138 | """Runs a command in a sub-process, and returns its STDOUT in a string.""" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 139 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 140 | return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 141 | |
| 142 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 143 | def GetNormalizedCommandOutputAndLeakyTests(cmd): |
| 144 | """Runs a command and returns its normalized output and a list of leaky tests. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 145 | |
| 146 | Args: |
| 147 | cmd: the shell command. |
| 148 | """ |
| 149 | |
| 150 | # Disables exception pop-ups on Windows. |
| 151 | os.environ['GTEST_CATCH_EXCEPTIONS'] = '1' |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 152 | return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd)) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 153 | |
| 154 | |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 155 | class GMockOutputTest(gmock_test_utils.TestCase): |
Abseil Team | 0ef404e | 2019-07-17 12:56:41 -0400 | [diff] [blame] | 156 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 157 | def testOutput(self): |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 158 | (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 159 | golden_file = open(GOLDEN_PATH, 'rb') |
Abseil Team | 0ef404e | 2019-07-17 12:56:41 -0400 | [diff] [blame] | 160 | golden = golden_file.read().decode('utf-8') |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 161 | golden_file.close() |
Abseil Team | ac7a126 | 2023-01-17 07:12:03 -0800 | [diff] [blame] | 162 | # On Windows the repository might have been checked out with \r\n line |
| 163 | # endings, so normalize it here. |
| 164 | golden = ToUnixLineEnding(golden) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 165 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 166 | # The normalized output should match the golden file. |
Abseil Team | af29db7 | 2022-03-23 15:07:35 -0700 | [diff] [blame] | 167 | self.assertEqual(golden, output) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 168 | |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 169 | # The raw output should contain 2 leaked mock object errors for |
| 170 | # test GMockOutputTest.CatchesLeakedMocks. |
Tom Hughes | d1ad27e | 2023-01-25 09:13:35 -0800 | [diff] [blame] | 171 | self.assertEqual( |
| 172 | [ |
| 173 | 'GMockOutputTest.CatchesLeakedMocks', |
| 174 | 'GMockOutputTest.CatchesLeakedMocks', |
| 175 | ], |
| 176 | leaky_tests, |
| 177 | ) |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 178 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 179 | |
| 180 | if __name__ == '__main__': |
| 181 | if sys.argv[1:] == [GENGOLDEN_FLAG]: |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 182 | (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 183 | golden_file = open(GOLDEN_PATH, 'wb') |
| 184 | golden_file.write(output) |
| 185 | golden_file.close() |
Gennadiy Civil | ec7faa9 | 2018-02-09 10:41:09 -0500 | [diff] [blame] | 186 | # Suppress the error "googletest was imported but a call to its main() |
| 187 | # was never detected." |
| 188 | os._exit(0) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 189 | else: |
| 190 | gmock_test_utils.Main() |