Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """Unit test for Google Test's break-on-failure mode.
00033
00034 A user can ask Google Test to seg-fault when an assertion fails, using
00035 either the GTEST_BREAK_ON_FAILURE environment variable or the
00036 --gtest_break_on_failure flag. This script tests such functionality
00037 by invoking gtest_break_on_failure_unittest_ (a program written with
00038 Google Test) with different environments and command line flags.
00039 """
00040
00041 __author__ = 'wan@google.com (Zhanyong Wan)'
00042
00043 import gtest_test_utils
00044 import os
00045 import sys
00046
00047
00048
00049
00050 IS_WINDOWS = os.name == 'nt'
00051
00052
00053 BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
00054
00055
00056 BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
00057
00058
00059 THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
00060
00061
00062 CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
00063
00064
00065 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
00066 'gtest_break_on_failure_unittest_')
00067
00068
00069 environ = gtest_test_utils.environ
00070 SetEnvVar = gtest_test_utils.SetEnvVar
00071
00072
00073
00074
00075
00076
00077 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
00078
00079
00080 def Run(command):
00081 """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
00082
00083 p = gtest_test_utils.Subprocess(command, env=environ)
00084 if p.terminated_by_signal:
00085 return 1
00086 else:
00087 return 0
00088
00089
00090
00091
00092
00093 class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
00094 """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
00095 the --gtest_break_on_failure flag to turn assertion failures into
00096 segmentation faults.
00097 """
00098
00099 def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
00100 """Runs gtest_break_on_failure_unittest_ and verifies that it does
00101 (or does not) have a seg-fault.
00102
00103 Args:
00104 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
00105 variable; None if the variable should be unset.
00106 flag_value: value of the --gtest_break_on_failure flag;
00107 None if the flag should not be present.
00108 expect_seg_fault: 1 if the program is expected to generate a seg-fault;
00109 0 otherwise.
00110 """
00111
00112 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
00113
00114 if env_var_value is None:
00115 env_var_value_msg = ' is not set'
00116 else:
00117 env_var_value_msg = '=' + env_var_value
00118
00119 if flag_value is None:
00120 flag = ''
00121 elif flag_value == '0':
00122 flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
00123 else:
00124 flag = '--%s' % BREAK_ON_FAILURE_FLAG
00125
00126 command = [EXE_PATH]
00127 if flag:
00128 command.append(flag)
00129
00130 if expect_seg_fault:
00131 should_or_not = 'should'
00132 else:
00133 should_or_not = 'should not'
00134
00135 has_seg_fault = Run(command)
00136
00137 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
00138
00139 msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
00140 (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
00141 should_or_not))
00142 self.assert_(has_seg_fault == expect_seg_fault, msg)
00143
00144 def testDefaultBehavior(self):
00145 """Tests the behavior of the default mode."""
00146
00147 self.RunAndVerify(env_var_value=None,
00148 flag_value=None,
00149 expect_seg_fault=0)
00150
00151 def testEnvVar(self):
00152 """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
00153
00154 self.RunAndVerify(env_var_value='0',
00155 flag_value=None,
00156 expect_seg_fault=0)
00157 self.RunAndVerify(env_var_value='1',
00158 flag_value=None,
00159 expect_seg_fault=1)
00160
00161 def testFlag(self):
00162 """Tests using the --gtest_break_on_failure flag."""
00163
00164 self.RunAndVerify(env_var_value=None,
00165 flag_value='0',
00166 expect_seg_fault=0)
00167 self.RunAndVerify(env_var_value=None,
00168 flag_value='1',
00169 expect_seg_fault=1)
00170
00171 def testFlagOverridesEnvVar(self):
00172 """Tests that the flag overrides the environment variable."""
00173
00174 self.RunAndVerify(env_var_value='0',
00175 flag_value='0',
00176 expect_seg_fault=0)
00177 self.RunAndVerify(env_var_value='0',
00178 flag_value='1',
00179 expect_seg_fault=1)
00180 self.RunAndVerify(env_var_value='1',
00181 flag_value='0',
00182 expect_seg_fault=0)
00183 self.RunAndVerify(env_var_value='1',
00184 flag_value='1',
00185 expect_seg_fault=1)
00186
00187 def testBreakOnFailureOverridesThrowOnFailure(self):
00188 """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
00189
00190 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
00191 try:
00192 self.RunAndVerify(env_var_value=None,
00193 flag_value='1',
00194 expect_seg_fault=1)
00195 finally:
00196 SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
00197
00198 if IS_WINDOWS:
00199 def testCatchExceptionsDoesNotInterfere(self):
00200 """Tests that gtest_catch_exceptions doesn't interfere."""
00201
00202 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
00203 try:
00204 self.RunAndVerify(env_var_value='1',
00205 flag_value='1',
00206 expect_seg_fault=1)
00207 finally:
00208 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
00209
00210
00211 if __name__ == '__main__':
00212 gtest_test_utils.Main()