3 # Copyright 2009, 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 """Tests Google Test's throw-on-failure mode with exceptions disabled.
34 This script invokes googletest-throw-on-failure-test_ (a program written with
35 Google Test) with different environments and command line flags.
39 from googletest.test import gtest_test_utils
44 # The command line flag for enabling/disabling the throw-on-failure mode.
45 THROW_ON_FAILURE = 'gtest_throw_on_failure'
47 # Path to the googletest-throw-on-failure-test_ program, compiled with
48 # exceptions disabled.
49 EXE_PATH = gtest_test_utils.GetTestExecutablePath(
50 'googletest-throw-on-failure-test_'
57 def SetEnvVar(env_var, value):
58 """Sets an environment variable.
60 Sets an environment variable to a given value; unsets it when the
64 env_var: environment variable.
68 env_var = env_var.upper()
70 os.environ[env_var] = value
71 elif env_var in os.environ:
72 del os.environ[env_var]
76 """Runs a command; returns True/False if its exit code is/isn't 0."""
78 print('Running "%s". . .' % ' '.join(command))
79 p = gtest_test_utils.Subprocess(command)
80 return p.exited and p.exit_code == 0
84 class ThrowOnFailureTest(gtest_test_utils.TestCase):
85 """Tests the throw-on-failure mode."""
87 def RunAndVerify(self, env_var_value, flag_value, should_fail):
88 """Runs googletest-throw-on-failure-test_ and verifies its behavior.
90 Runs googletest-throw-on-failure-test_ and verifies that it does
91 (or does not) exit with a non-zero code.
94 env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
95 variable; None if the variable should be unset.
96 flag_value: value of the --gtest_break_on_failure flag; None if the
97 flag should not be present.
98 should_fail: True if and only if the program is expected to fail.
101 SetEnvVar(THROW_ON_FAILURE, env_var_value)
103 if env_var_value is None:
104 env_var_value_msg = ' is not set'
106 env_var_value_msg = '=' + env_var_value
108 if flag_value is None:
110 elif flag_value == '0':
111 flag = '--%s=0' % THROW_ON_FAILURE
113 flag = '--%s' % THROW_ON_FAILURE
120 should_or_not = 'should'
122 should_or_not = 'should not'
124 failed = not Run(command)
126 SetEnvVar(THROW_ON_FAILURE, None)
129 'when %s%s, an assertion failure in "%s" %s cause a non-zero exit code.'
137 self.assertTrue(failed == should_fail, msg)
139 def testDefaultBehavior(self):
140 """Tests the behavior of the default mode."""
142 self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
144 def testThrowOnFailureEnvVar(self):
145 """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
147 self.RunAndVerify(env_var_value='0', flag_value=None, should_fail=False)
148 self.RunAndVerify(env_var_value='1', flag_value=None, should_fail=True)
150 def testThrowOnFailureFlag(self):
151 """Tests using the --gtest_throw_on_failure flag."""
153 self.RunAndVerify(env_var_value=None, flag_value='0', should_fail=False)
154 self.RunAndVerify(env_var_value=None, flag_value='1', should_fail=True)
156 def testThrowOnFailureFlagOverridesEnvVar(self):
157 """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
159 self.RunAndVerify(env_var_value='0', flag_value='0', should_fail=False)
160 self.RunAndVerify(env_var_value='0', flag_value='1', should_fail=True)
161 self.RunAndVerify(env_var_value='1', flag_value='0', should_fail=False)
162 self.RunAndVerify(env_var_value='1', flag_value='1', should_fail=True)
165 if __name__ == '__main__':
166 gtest_test_utils.Main()