gtest_help_test.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2009, 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. """Tests the --help flag of Google C++ Testing and Mocking Framework.
  32. SYNOPSIS
  33. gtest_help_test.py --build_dir=BUILD/DIR
  34. # where BUILD/DIR contains the built gtest_help_test_ file.
  35. gtest_help_test.py
  36. """
  37. import os
  38. import re
  39. import sys
  40. from googletest.test import gtest_test_utils
  41. IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
  42. IS_GNUHURD = os.name == 'posix' and os.uname()[0] == 'GNU'
  43. IS_GNUKFREEBSD = os.name == 'posix' and os.uname()[0] == 'GNU/kFreeBSD'
  44. IS_OPENBSD = os.name == 'posix' and os.uname()[0] == 'OpenBSD'
  45. IS_WINDOWS = os.name == 'nt'
  46. PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
  47. FLAG_PREFIX = '--gtest_'
  48. DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
  49. STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to'
  50. UNKNOWN_GTEST_PREFIXED_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
  51. LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
  52. INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
  53. SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
  54. [PROGRAM_PATH, LIST_TESTS_FLAG]).output
  55. HAS_ABSL_FLAGS = '--has_absl_flags' in sys.argv
  56. # The help message must match this regex.
  57. HELP_REGEX = re.compile(
  58. FLAG_PREFIX + r'list_tests.*' +
  59. FLAG_PREFIX + r'filter=.*' +
  60. FLAG_PREFIX + r'also_run_disabled_tests.*' +
  61. FLAG_PREFIX + r'repeat=.*' +
  62. FLAG_PREFIX + r'shuffle.*' +
  63. FLAG_PREFIX + r'random_seed=.*' +
  64. FLAG_PREFIX + r'color=.*' +
  65. FLAG_PREFIX + r'brief.*' +
  66. FLAG_PREFIX + r'print_time.*' +
  67. FLAG_PREFIX + r'output=.*' +
  68. FLAG_PREFIX + r'break_on_failure.*' +
  69. FLAG_PREFIX + r'throw_on_failure.*' +
  70. FLAG_PREFIX + r'catch_exceptions=0.*',
  71. re.DOTALL)
  72. def RunWithFlag(flag):
  73. """Runs gtest_help_test_ with the given flag.
  74. Returns:
  75. the exit code and the text output as a tuple.
  76. Args:
  77. flag: the command-line flag to pass to gtest_help_test_, or None.
  78. """
  79. if flag is None:
  80. command = [PROGRAM_PATH]
  81. else:
  82. command = [PROGRAM_PATH, flag]
  83. child = gtest_test_utils.Subprocess(command)
  84. return child.exit_code, child.output
  85. class GTestHelpTest(gtest_test_utils.TestCase):
  86. """Tests the --help flag and its equivalent forms."""
  87. def TestHelpFlag(self, flag):
  88. """Verifies correct behavior when help flag is specified.
  89. The right message must be printed and the tests must
  90. skipped when the given flag is specified.
  91. Args:
  92. flag: A flag to pass to the binary or None.
  93. """
  94. exit_code, output = RunWithFlag(flag)
  95. if HAS_ABSL_FLAGS:
  96. # The Abseil flags library prints the ProgramUsageMessage() with
  97. # --help and returns 1.
  98. self.assertEqual(1, exit_code)
  99. else:
  100. self.assertEqual(0, exit_code)
  101. self.assertTrue(HELP_REGEX.search(output), output)
  102. if IS_LINUX or IS_GNUHURD or IS_GNUKFREEBSD or IS_OPENBSD:
  103. self.assertIn(STREAM_RESULT_TO_FLAG, output)
  104. else:
  105. self.assertNotIn(STREAM_RESULT_TO_FLAG, output)
  106. if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
  107. self.assertIn(DEATH_TEST_STYLE_FLAG, output)
  108. else:
  109. self.assertNotIn(DEATH_TEST_STYLE_FLAG, output)
  110. def TestUnknownFlagWithAbseil(self, flag):
  111. """Verifies correct behavior when an unknown flag is specified.
  112. The right message must be printed and the tests must
  113. skipped when the given flag is specified.
  114. Args:
  115. flag: A flag to pass to the binary or None.
  116. """
  117. exit_code, output = RunWithFlag(flag)
  118. self.assertEqual(1, exit_code)
  119. self.assertIn('ERROR: Unknown command line flag', output)
  120. def TestNonHelpFlag(self, flag):
  121. """Verifies correct behavior when no help flag is specified.
  122. Verifies that when no help flag is specified, the tests are run
  123. and the help message is not printed.
  124. Args:
  125. flag: A flag to pass to the binary or None.
  126. """
  127. exit_code, output = RunWithFlag(flag)
  128. self.assertNotEqual(exit_code, 0)
  129. self.assertFalse(HELP_REGEX.search(output), output)
  130. def testPrintsHelpWithFullFlag(self):
  131. self.TestHelpFlag('--help')
  132. def testPrintsHelpWithUnrecognizedGoogleTestFlag(self):
  133. # The behavior is slightly different when Abseil flags is
  134. # used. Abseil flags rejects all unknown flags, while the builtin
  135. # GTest flags implementation interprets an unknown flag with a
  136. # '--gtest_' prefix as a request for help.
  137. if HAS_ABSL_FLAGS:
  138. self.TestUnknownFlagWithAbseil(UNKNOWN_GTEST_PREFIXED_FLAG)
  139. else:
  140. self.TestHelpFlag(UNKNOWN_GTEST_PREFIXED_FLAG)
  141. def testRunsTestsWithoutHelpFlag(self):
  142. """Verifies that when no help flag is specified, the tests are run
  143. and the help message is not printed."""
  144. self.TestNonHelpFlag(None)
  145. def testRunsTestsWithGtestInternalFlag(self):
  146. """Verifies that the tests are run and no help message is printed when
  147. a flag starting with Google Test prefix and 'internal_' is supplied."""
  148. self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
  149. if __name__ == '__main__':
  150. if '--has_absl_flags' in sys.argv:
  151. sys.argv.remove('--has_absl_flags')
  152. gtest_test_utils.Main()