You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.0 KiB

  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. r"""Tests the text output of Google C++ Mocking Framework.
  32. To update the golden file:
  33. gmock_output_test.py --build_dir=BUILD/DIR --gengolden
  34. where BUILD/DIR contains the built gmock_output_test_ file.
  35. gmock_output_test.py --gengolden
  36. gmock_output_test.py
  37. """
  38. from io import open # pylint: disable=redefined-builtin, g-importing-member
  39. import os
  40. import re
  41. import sys
  42. import gmock_test_utils
  43. # The flag for generating the golden file
  44. GENGOLDEN_FLAG = '--gengolden'
  45. PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_output_test_')
  46. COMMAND = [PROGRAM_PATH, '--gtest_stack_trace_depth=0', '--gtest_print_time=0']
  47. GOLDEN_NAME = 'gmock_output_test_golden.txt'
  48. GOLDEN_PATH = os.path.join(gmock_test_utils.GetSourceDir(), GOLDEN_NAME)
  49. def ToUnixLineEnding(s):
  50. """Changes all Windows/Mac line endings in s to UNIX line endings."""
  51. return s.replace('\r\n', '\n').replace('\r', '\n')
  52. def RemoveReportHeaderAndFooter(output):
  53. """Removes Google Test result report's header and footer from the output."""
  54. output = re.sub(r'.*gtest_main.*\n', '', output)
  55. output = re.sub(r'\[.*\d+ tests.*\n', '', output)
  56. output = re.sub(r'\[.* test environment .*\n', '', output)
  57. output = re.sub(r'\[=+\] \d+ tests .* ran.*', '', output)
  58. output = re.sub(r'.* FAILED TESTS\n', '', output)
  59. return output
  60. def RemoveLocations(output):
  61. """Removes all file location info from a Google Test program's output.
  62. Args:
  63. output: the output of a Google Test program.
  64. Returns:
  65. output with all file location info (in the form of
  66. 'DIRECTORY/FILE_NAME:LINE_NUMBER: 'or
  67. 'DIRECTORY\\FILE_NAME(LINE_NUMBER): ') replaced by
  68. 'FILE:#: '.
  69. """
  70. return re.sub(r'.*[/\\](.+)(\:\d+|\(\d+\))\:', 'FILE:#:', output)
  71. def NormalizeErrorMarker(output):
  72. """Normalizes the error marker, which is different on Windows vs on Linux."""
  73. return re.sub(r' error: ', ' Failure\n', output)
  74. def RemoveMemoryAddresses(output):
  75. """Removes memory addresses from the test output."""
  76. return re.sub(r'@\w+', '@0x#', output)
  77. def RemoveTestNamesOfLeakedMocks(output):
  78. """Removes the test names of leaked mock objects from the test output."""
  79. return re.sub(r'\(used in test .+\) ', '', output)
  80. def GetLeakyTests(output):
  81. """Returns a list of test names that leak mock objects."""
  82. # findall() returns a list of all matches of the regex in output.
  83. # For example, if '(used in test FooTest.Bar)' is in output, the
  84. # list will contain 'FooTest.Bar'.
  85. return re.findall(r'\(used in test (.+)\)', output)
  86. def GetNormalizedOutputAndLeakyTests(output):
  87. """Normalizes the output of gmock_output_test_.
  88. Args:
  89. output: The test output.
  90. Returns:
  91. A tuple (the normalized test output, the list of test names that have
  92. leaked mocks).
  93. """
  94. output = ToUnixLineEnding(output)
  95. output = RemoveReportHeaderAndFooter(output)
  96. output = NormalizeErrorMarker(output)
  97. output = RemoveLocations(output)
  98. output = RemoveMemoryAddresses(output)
  99. return (RemoveTestNamesOfLeakedMocks(output), GetLeakyTests(output))
  100. def GetShellCommandOutput(cmd):
  101. """Runs a command in a sub-process, and returns its STDOUT in a string."""
  102. return gmock_test_utils.Subprocess(cmd, capture_stderr=False).output
  103. def GetNormalizedCommandOutputAndLeakyTests(cmd):
  104. """Runs a command and returns its normalized output and a list of leaky tests.
  105. Args:
  106. cmd: the shell command.
  107. """
  108. # Disables exception pop-ups on Windows.
  109. os.environ['GTEST_CATCH_EXCEPTIONS'] = '1'
  110. return GetNormalizedOutputAndLeakyTests(GetShellCommandOutput(cmd))
  111. class GMockOutputTest(gmock_test_utils.TestCase):
  112. def testOutput(self):
  113. (output, leaky_tests) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
  114. golden_file = open(GOLDEN_PATH, 'rb')
  115. golden = golden_file.read().decode('utf-8')
  116. golden_file.close()
  117. # The normalized output should match the golden file.
  118. self.assertEquals(golden, output)
  119. # The raw output should contain 2 leaked mock object errors for
  120. # test GMockOutputTest.CatchesLeakedMocks.
  121. self.assertEquals(['GMockOutputTest.CatchesLeakedMocks',
  122. 'GMockOutputTest.CatchesLeakedMocks'],
  123. leaky_tests)
  124. if __name__ == '__main__':
  125. if sys.argv[1:] == [GENGOLDEN_FLAG]:
  126. (output, _) = GetNormalizedCommandOutputAndLeakyTests(COMMAND)
  127. golden_file = open(GOLDEN_PATH, 'wb')
  128. golden_file.write(output)
  129. golden_file.close()
  130. # Suppress the error "googletest was imported but a call to its main()
  131. # was never detected."
  132. os._exit(0)
  133. else:
  134. gmock_test_utils.Main()