32 """Unit test for Google Test's break-on-failure mode.
34 A user can ask Google Test to seg-fault when an assertion fails, using
35 either the GTEST_BREAK_ON_FAILURE environment variable or the
36 --gtest_break_on_failure flag. This script tests such functionality
37 by invoking gtest_break_on_failure_unittest_ (a program written with
38 Google Test) with different environments and command line flags.
41 __author__ =
'wan@google.com (Zhanyong Wan)'
43 import gtest_test_utils
50 IS_WINDOWS = os.name ==
'nt'
53 BREAK_ON_FAILURE_ENV_VAR =
'GTEST_BREAK_ON_FAILURE'
56 BREAK_ON_FAILURE_FLAG =
'gtest_break_on_failure'
59 THROW_ON_FAILURE_ENV_VAR =
'GTEST_THROW_ON_FAILURE'
62 CATCH_EXCEPTIONS_ENV_VAR =
'GTEST_CATCH_EXCEPTIONS'
66 'gtest_break_on_failure_unittest_')
69 environ = gtest_test_utils.environ
70 SetEnvVar = gtest_test_utils.SetEnvVar
77 SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR,
None)
81 """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
84 if p.terminated_by_signal:
94 """Tests using the GTEST_BREAK_ON_FAILURE environment variable or
95 the --gtest_break_on_failure flag to turn assertion failures into
99 def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
100 """Runs gtest_break_on_failure_unittest_ and verifies that it does
101 (or does not) have a seg-fault.
104 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
105 variable; None if the variable should be unset.
106 flag_value: value of the --gtest_break_on_failure flag;
107 None if the flag should not be present.
108 expect_seg_fault: 1 if the program is expected to generate a seg-fault;
112 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
114 if env_var_value
is None:
115 env_var_value_msg =
' is not set'
117 env_var_value_msg =
'=' + env_var_value
119 if flag_value
is None:
121 elif flag_value ==
'0':
122 flag =
'--%s=0' % BREAK_ON_FAILURE_FLAG
124 flag =
'--%s' % BREAK_ON_FAILURE_FLAG
131 should_or_not =
'should'
133 should_or_not =
'should not'
135 has_seg_fault =
Run(command)
137 SetEnvVar(BREAK_ON_FAILURE_ENV_VAR,
None)
139 msg = (
'when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
140 (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg,
' '.
join(command),
142 self.assert_(has_seg_fault == expect_seg_fault, msg)
145 """Tests the behavior of the default mode."""
152 """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
162 """Tests using the --gtest_break_on_failure flag."""
172 """Tests that the flag overrides the environment variable."""
188 """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
196 SetEnvVar(THROW_ON_FAILURE_ENV_VAR,
None)
200 """Tests that gtest_catch_exceptions doesn't interfere."""
208 SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR,
None)
211 if __name__ ==
'__main__':