googletest-break-on-failure-unittest.py 6.8 KB

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