3
# Copyright 2006, Google Inc.
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are
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
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.
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.
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'
52
# The environment variable for enabling/disabling the break-on-failure mode.
53
BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
55
# The command line flag for enabling/disabling the break-on-failure mode.
56
BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
58
# The environment variable for enabling/disabling the throw-on-failure mode.
59
THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
61
# The environment variable for enabling/disabling the catch-exceptions mode.
62
CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
64
# Path to the gtest_break_on_failure_unittest_ program.
65
EXE_PATH = gtest_test_utils.GetTestExecutablePath(
66
'gtest_break_on_failure_unittest_')
72
environ = os.environ.copy()
75
def SetEnvVar(env_var, value):
76
"""Sets an environment variable to a given value; unsets it when the
81
environ[env_var] = value
82
elif env_var in environ:
87
"""Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
89
p = gtest_test_utils.Subprocess(command, env=environ)
90
if p.terminated_by_signal:
99
class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
100
"""Tests using the GTEST_BREAK_ON_FAILURE environment variable or
101
the --gtest_break_on_failure flag to turn assertion failures into
105
def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
106
"""Runs gtest_break_on_failure_unittest_ and verifies that it does
107
(or does not) have a seg-fault.
110
env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
111
variable; None if the variable should be unset.
112
flag_value: value of the --gtest_break_on_failure flag;
113
None if the flag should not be present.
114
expect_seg_fault: 1 if the program is expected to generate a seg-fault;
118
SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
120
if env_var_value is None:
121
env_var_value_msg = ' is not set'
123
env_var_value_msg = '=' + env_var_value
125
if flag_value is None:
127
elif flag_value == '0':
128
flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
130
flag = '--%s' % BREAK_ON_FAILURE_FLAG
137
should_or_not = 'should'
139
should_or_not = 'should not'
141
has_seg_fault = Run(command)
143
SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
145
msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' %
146
(BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command),
148
self.assert_(has_seg_fault == expect_seg_fault, msg)
150
def testDefaultBehavior(self):
151
"""Tests the behavior of the default mode."""
153
self.RunAndVerify(env_var_value=None,
157
def testEnvVar(self):
158
"""Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
160
self.RunAndVerify(env_var_value='0',
163
self.RunAndVerify(env_var_value='1',
168
"""Tests using the --gtest_break_on_failure flag."""
170
self.RunAndVerify(env_var_value=None,
173
self.RunAndVerify(env_var_value=None,
177
def testFlagOverridesEnvVar(self):
178
"""Tests that the flag overrides the environment variable."""
180
self.RunAndVerify(env_var_value='0',
183
self.RunAndVerify(env_var_value='0',
186
self.RunAndVerify(env_var_value='1',
189
self.RunAndVerify(env_var_value='1',
193
def testBreakOnFailureOverridesThrowOnFailure(self):
194
"""Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
196
SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
198
self.RunAndVerify(env_var_value=None,
202
SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
205
def testCatchExceptionsDoesNotInterfere(self):
206
"""Tests that gtest_catch_exceptions doesn't interfere."""
208
SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
210
self.RunAndVerify(env_var_value='1',
214
SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
217
if __name__ == '__main__':
218
gtest_test_utils.Main()