googletest-throw-on-failure-test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Google Test's throw-on-failure mode with exceptions disabled.
  32. This script invokes googletest-throw-on-failure-test_ (a program written with
  33. Google Test) with different environments and command line flags.
  34. """
  35. import os
  36. import gtest_test_utils
  37. # Constants.
  38. # The command line flag for enabling/disabling the throw-on-failure mode.
  39. THROW_ON_FAILURE = 'gtest_throw_on_failure'
  40. # Path to the googletest-throw-on-failure-test_ program, compiled with
  41. # exceptions disabled.
  42. EXE_PATH = gtest_test_utils.GetTestExecutablePath(
  43. 'googletest-throw-on-failure-test_')
  44. # Utilities.
  45. def SetEnvVar(env_var, value):
  46. """Sets an environment variable to a given value; unsets it when the
  47. given value is None.
  48. """
  49. env_var = env_var.upper()
  50. if value is not None:
  51. os.environ[env_var] = value
  52. elif env_var in os.environ:
  53. del os.environ[env_var]
  54. def Run(command):
  55. """Runs a command; returns True/False if its exit code is/isn't 0."""
  56. print 'Running "%s". . .' % ' '.join(command)
  57. p = gtest_test_utils.Subprocess(command)
  58. return p.exited and p.exit_code == 0
  59. # The tests. FIXME: refactor the class to share common
  60. # logic with code in googletest-break-on-failure-unittest.py.
  61. class ThrowOnFailureTest(gtest_test_utils.TestCase):
  62. """Tests the throw-on-failure mode."""
  63. def RunAndVerify(self, env_var_value, flag_value, should_fail):
  64. """Runs googletest-throw-on-failure-test_ and verifies that it does
  65. (or does not) exit with a non-zero code.
  66. Args:
  67. env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
  68. variable; None if the variable should be unset.
  69. flag_value: value of the --gtest_break_on_failure flag;
  70. None if the flag should not be present.
  71. should_fail: True iff the program is expected to fail.
  72. """
  73. SetEnvVar(THROW_ON_FAILURE, env_var_value)
  74. if env_var_value is None:
  75. env_var_value_msg = ' is not set'
  76. else:
  77. env_var_value_msg = '=' + env_var_value
  78. if flag_value is None:
  79. flag = ''
  80. elif flag_value == '0':
  81. flag = '--%s=0' % THROW_ON_FAILURE
  82. else:
  83. flag = '--%s' % THROW_ON_FAILURE
  84. command = [EXE_PATH]
  85. if flag:
  86. command.append(flag)
  87. if should_fail:
  88. should_or_not = 'should'
  89. else:
  90. should_or_not = 'should not'
  91. failed = not Run(command)
  92. SetEnvVar(THROW_ON_FAILURE, None)
  93. msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero '
  94. 'exit code.' %
  95. (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command),
  96. should_or_not))
  97. self.assert_(failed == should_fail, msg)
  98. def testDefaultBehavior(self):
  99. """Tests the behavior of the default mode."""
  100. self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
  101. def testThrowOnFailureEnvVar(self):
  102. """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
  103. self.RunAndVerify(env_var_value='0',
  104. flag_value=None,
  105. should_fail=False)
  106. self.RunAndVerify(env_var_value='1',
  107. flag_value=None,
  108. should_fail=True)
  109. def testThrowOnFailureFlag(self):
  110. """Tests using the --gtest_throw_on_failure flag."""
  111. self.RunAndVerify(env_var_value=None,
  112. flag_value='0',
  113. should_fail=False)
  114. self.RunAndVerify(env_var_value=None,
  115. flag_value='1',
  116. should_fail=True)
  117. def testThrowOnFailureFlagOverridesEnvVar(self):
  118. """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
  119. self.RunAndVerify(env_var_value='0',
  120. flag_value='0',
  121. should_fail=False)
  122. self.RunAndVerify(env_var_value='0',
  123. flag_value='1',
  124. should_fail=True)
  125. self.RunAndVerify(env_var_value='1',
  126. flag_value='0',
  127. should_fail=False)
  128. self.RunAndVerify(env_var_value='1',
  129. flag_value='1',
  130. should_fail=True)
  131. if __name__ == '__main__':
  132. gtest_test_utils.Main()