gtest_help_test.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 gtest_test_utils
  40. IS_LINUX = os.name == 'posix' and os.uname()[0] == 'Linux'
  41. IS_WINDOWS = os.name == 'nt'
  42. PROGRAM_PATH = gtest_test_utils.GetTestExecutablePath('gtest_help_test_')
  43. FLAG_PREFIX = '--gtest_'
  44. DEATH_TEST_STYLE_FLAG = FLAG_PREFIX + 'death_test_style'
  45. STREAM_RESULT_TO_FLAG = FLAG_PREFIX + 'stream_result_to'
  46. UNKNOWN_FLAG = FLAG_PREFIX + 'unknown_flag_for_testing'
  47. LIST_TESTS_FLAG = FLAG_PREFIX + 'list_tests'
  48. INCORRECT_FLAG_VARIANTS = [re.sub('^--', '-', LIST_TESTS_FLAG),
  49. re.sub('^--', '/', LIST_TESTS_FLAG),
  50. re.sub('_', '-', LIST_TESTS_FLAG)]
  51. INTERNAL_FLAG_FOR_TESTING = FLAG_PREFIX + 'internal_flag_for_testing'
  52. SUPPORTS_DEATH_TESTS = "DeathTest" in gtest_test_utils.Subprocess(
  53. [PROGRAM_PATH, LIST_TESTS_FLAG]).output
  54. # The help message must match this regex.
  55. HELP_REGEX = re.compile(
  56. FLAG_PREFIX + r'list_tests.*' +
  57. FLAG_PREFIX + r'filter=.*' +
  58. FLAG_PREFIX + r'also_run_disabled_tests.*' +
  59. FLAG_PREFIX + r'repeat=.*' +
  60. FLAG_PREFIX + r'shuffle.*' +
  61. FLAG_PREFIX + r'random_seed=.*' +
  62. FLAG_PREFIX + r'color=.*' +
  63. FLAG_PREFIX + r'print_time.*' +
  64. FLAG_PREFIX + r'output=.*' +
  65. FLAG_PREFIX + r'break_on_failure.*' +
  66. FLAG_PREFIX + r'throw_on_failure.*' +
  67. FLAG_PREFIX + r'catch_exceptions=0.*',
  68. re.DOTALL)
  69. def RunWithFlag(flag):
  70. """Runs gtest_help_test_ with the given flag.
  71. Returns:
  72. the exit code and the text output as a tuple.
  73. Args:
  74. flag: the command-line flag to pass to gtest_help_test_, or None.
  75. """
  76. if flag is None:
  77. command = [PROGRAM_PATH]
  78. else:
  79. command = [PROGRAM_PATH, flag]
  80. child = gtest_test_utils.Subprocess(command)
  81. return child.exit_code, child.output
  82. class GTestHelpTest(gtest_test_utils.TestCase):
  83. """Tests the --help flag and its equivalent forms."""
  84. def TestHelpFlag(self, flag):
  85. """Verifies correct behavior when help flag is specified.
  86. The right message must be printed and the tests must
  87. skipped when the given flag is specified.
  88. Args:
  89. flag: A flag to pass to the binary or None.
  90. """
  91. exit_code, output = RunWithFlag(flag)
  92. self.assertEquals(0, exit_code)
  93. self.assert_(HELP_REGEX.search(output), output)
  94. if IS_LINUX:
  95. self.assert_(STREAM_RESULT_TO_FLAG in output, output)
  96. else:
  97. self.assert_(STREAM_RESULT_TO_FLAG not in output, output)
  98. if SUPPORTS_DEATH_TESTS and not IS_WINDOWS:
  99. self.assert_(DEATH_TEST_STYLE_FLAG in output, output)
  100. else:
  101. self.assert_(DEATH_TEST_STYLE_FLAG not in output, output)
  102. def TestNonHelpFlag(self, flag):
  103. """Verifies correct behavior when no help flag is specified.
  104. Verifies that when no help flag is specified, the tests are run
  105. and the help message is not printed.
  106. Args:
  107. flag: A flag to pass to the binary or None.
  108. """
  109. exit_code, output = RunWithFlag(flag)
  110. self.assert_(exit_code != 0)
  111. self.assert_(not HELP_REGEX.search(output), output)
  112. def testPrintsHelpWithFullFlag(self):
  113. self.TestHelpFlag('--help')
  114. def testPrintsHelpWithShortFlag(self):
  115. self.TestHelpFlag('-h')
  116. def testPrintsHelpWithQuestionFlag(self):
  117. self.TestHelpFlag('-?')
  118. def testPrintsHelpWithWindowsStyleQuestionFlag(self):
  119. self.TestHelpFlag('/?')
  120. def testPrintsHelpWithUnrecognizedGoogleTestFlag(self):
  121. self.TestHelpFlag(UNKNOWN_FLAG)
  122. def testPrintsHelpWithIncorrectFlagStyle(self):
  123. for incorrect_flag in INCORRECT_FLAG_VARIANTS:
  124. self.TestHelpFlag(incorrect_flag)
  125. def testRunsTestsWithoutHelpFlag(self):
  126. """Verifies that when no help flag is specified, the tests are run
  127. and the help message is not printed."""
  128. self.TestNonHelpFlag(None)
  129. def testRunsTestsWithGtestInternalFlag(self):
  130. """Verifies that the tests are run and no help message is printed when
  131. a flag starting with Google Test prefix and 'internal_' is supplied."""
  132. self.TestNonHelpFlag(INTERNAL_FLAG_FOR_TESTING)
  133. if __name__ == '__main__':
  134. gtest_test_utils.Main()