gtest_break_on_failure_unittest.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2006, 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. """Unit test for Google Test's break-on-failure mode.
  32. A user can ask Google Test to seg-fault when an assertion fails, using
  33. either the GTEST_BREAK_ON_FAILURE environment variable or the
  34. --gtest_break_on_failure flag. This script tests such functionality
  35. by invoking gtest_break_on_failure_unittest_ (a program written with
  36. Google Test) with different environments and command line flags.
  37. """
  38. __author__ = 'wan@google.com (Zhanyong Wan)'
  39. import gtest_test_utils
  40. import os
  41. import sys
  42. # Constants.
  43. IS_WINDOWS = os.name == 'nt'
  44. # The environment variable for enabling/disabling the break-on-failure mode.
  45. BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
  46. # The command line flag for enabling/disabling the break-on-failure mode.
  47. BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
  48. # The environment variable for enabling/disabling the throw-on-failure mode.
  49. THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
  50. # The environment variable for enabling/disabling the catch-exceptions mode.
  51. CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
  52. # Path to the gtest_break_on_failure_unittest_ program.
  53. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  54. 'gtest_break_on_failure_unittest_')
  55. environ = gtest_test_utils.environ
  56. SetEnvVar = gtest_test_utils.SetEnvVar
  57. # Tests in this file run a Google-Test-based test program and expect it
  58. # to terminate prematurely. Therefore they are incompatible with
  59. # the premature-exit-file protocol by design. Unset the
  60. # premature-exit filepath to prevent Google Test from creating
  61. # the file.
  62. SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
  63. def Run(command):
  64. """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
  65. p = gtest_test_utils.Subprocess(command, env=environ)
  66. if p.terminated_by_signal:
  67. return 1
  68. else:
  69. return 0
  70. # The tests.
  71. class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
  72. """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
  73. the --gtest_break_on_failure flag to turn assertion failures into
  74. segmentation faults.
  75. """
  76. def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
  77. """Runs gtest_break_on_failure_unittest_ and verifies that it does
  78. (or does not) have a seg-fault.
  79. Args:
  80. env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
  81. variable; None if the variable should be unset.
  82. flag_value: value of the --gtest_break_on_failure flag;
  83. None if the flag should not be present.
  84. expect_seg_fault: 1 if the program is expected to generate a seg-fault;
  85. 0 otherwise.
  86. """
  87. SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
  88. if env_var_value is None:
  89. env_var_value_msg = ' is not set'
  90. else:
  91. env_var_value_msg = '=' + env_var_value
  92. if flag_value is None:
  93. flag = ''
  94. elif flag_value == '0':
  95. flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
  96. else:
  97. flag = '--%s' % BREAK_ON_FAILURE_FLAG
  98. command = [EXE_PATH]
  99. if flag:
  100. command.append(flag)
  101. if expect_seg_fault:
  102. should_or_not = 'should'
  103. else:
  104. should_or_not = 'should not'
  105. has_seg_fault = Run(command)
  106. SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
  107. msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
  108. (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
  109. should_or_not))
  110. self.assert_(has_seg_fault == expect_seg_fault, msg)
  111. def testDefaultBehavior(self):
  112. """Tests the behavior of the default mode."""
  113. self.RunAndVerify(env_var_value=None,
  114. flag_value=None,
  115. expect_seg_fault=0)
  116. def testEnvVar(self):
  117. """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
  118. self.RunAndVerify(env_var_value='0',
  119. flag_value=None,
  120. expect_seg_fault=0)
  121. self.RunAndVerify(env_var_value='1',
  122. flag_value=None,
  123. expect_seg_fault=1)
  124. def testFlag(self):
  125. """Tests using the --gtest_break_on_failure flag."""
  126. self.RunAndVerify(env_var_value=None,
  127. flag_value='0',
  128. expect_seg_fault=0)
  129. self.RunAndVerify(env_var_value=None,
  130. flag_value='1',
  131. expect_seg_fault=1)
  132. def testFlagOverridesEnvVar(self):
  133. """Tests that the flag overrides the environment variable."""
  134. self.RunAndVerify(env_var_value='0',
  135. flag_value='0',
  136. expect_seg_fault=0)
  137. self.RunAndVerify(env_var_value='0',
  138. flag_value='1',
  139. expect_seg_fault=1)
  140. self.RunAndVerify(env_var_value='1',
  141. flag_value='0',
  142. expect_seg_fault=0)
  143. self.RunAndVerify(env_var_value='1',
  144. flag_value='1',
  145. expect_seg_fault=1)
  146. def testBreakOnFailureOverridesThrowOnFailure(self):
  147. """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
  148. SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
  149. try:
  150. self.RunAndVerify(env_var_value=None,
  151. flag_value='1',
  152. expect_seg_fault=1)
  153. finally:
  154. SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
  155. if IS_WINDOWS:
  156. def testCatchExceptionsDoesNotInterfere(self):
  157. """Tests that gtest_catch_exceptions doesn't interfere."""
  158. SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
  159. try:
  160. self.RunAndVerify(env_var_value='1',
  161. flag_value='1',
  162. expect_seg_fault=1)
  163. finally:
  164. SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
  165. if __name__ == '__main__':
  166. gtest_test_utils.Main()