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.

172 lines
5.7 KiB

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