xref: /freebsd/contrib/googletest/googletest/test/googletest-break-on-failure-unittest.py (revision 28f6c2f292806bf31230a959bc4b19d7081669a7)
1#!/usr/bin/env python
2#
3# Copyright 2006, 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
32"""Unit test for Google Test's break-on-failure mode.
33
34A user can ask Google Test to seg-fault when an assertion fails, using
35either the GTEST_BREAK_ON_FAILURE environment variable or the
36--gtest_break_on_failure flag.  This script tests such functionality
37by invoking googletest-break-on-failure-unittest_ (a program written with
38Google Test) with different environments and command line flags.
39"""
40
41import os
42from googletest.test import gtest_test_utils
43
44# Constants.
45
46IS_WINDOWS = os.name == 'nt'
47
48# The environment variable for enabling/disabling the break-on-failure mode.
49BREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE'
50
51# The command line flag for enabling/disabling the break-on-failure mode.
52BREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure'
53
54# The environment variable for enabling/disabling the throw-on-failure mode.
55THROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE'
56
57# The environment variable for enabling/disabling the catch-exceptions mode.
58CATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS'
59
60# Path to the googletest-break-on-failure-unittest_ program.
61EXE_PATH = gtest_test_utils.GetTestExecutablePath(
62    'googletest-break-on-failure-unittest_'
63)
64
65
66environ = gtest_test_utils.environ
67SetEnvVar = gtest_test_utils.SetEnvVar
68
69# Tests in this file run a Google-Test-based test program and expect it
70# to terminate prematurely.  Therefore they are incompatible with
71# the premature-exit-file protocol by design.  Unset the
72# premature-exit filepath to prevent Google Test from creating
73# the file.
74SetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None)
75
76
77def Run(command):
78  """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise."""
79
80  p = gtest_test_utils.Subprocess(command, env=environ)
81  if p.terminated_by_signal:
82    return 1
83  else:
84    return 0
85
86
87# The tests.
88
89
90class GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase):
91  """Unit test for Google Test's break-on-failure mode.
92
93  Tests using the GTEST_BREAK_ON_FAILURE environment variable or
94  the --gtest_break_on_failure flag to turn assertion failures into
95  segmentation faults.
96  """
97
98  def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault):
99    """Runs googletest-break-on-failure-unittest_ and verifies its behavior.
100
101    Runs googletest-break-on-failure-unittest_ and verifies that it does
102    (or does not) have a seg-fault.
103
104    Args:
105      env_var_value:    value of the GTEST_BREAK_ON_FAILURE environment
106        variable; None if the variable should be unset.
107      flag_value:       value of the --gtest_break_on_failure flag; None if the
108        flag should not be present.
109      expect_seg_fault: 1 if the program is expected to generate a seg-fault; 0
110        otherwise.
111    """
112
113    SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value)
114
115    if env_var_value is None:
116      env_var_value_msg = ' is not set'
117    else:
118      env_var_value_msg = '=' + env_var_value
119
120    if flag_value is None:
121      flag = ''
122    elif flag_value == '0':
123      flag = '--%s=0' % BREAK_ON_FAILURE_FLAG
124    else:
125      flag = '--%s' % BREAK_ON_FAILURE_FLAG
126
127    command = [EXE_PATH]
128    if flag:
129      command.append(flag)
130
131    if expect_seg_fault:
132      should_or_not = 'should'
133    else:
134      should_or_not = 'should not'
135
136    has_seg_fault = Run(command)
137
138    SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None)
139
140    msg = 'when %s%s, an assertion failure in "%s" %s cause a seg-fault.' % (
141        BREAK_ON_FAILURE_ENV_VAR,
142        env_var_value_msg,
143        ' '.join(command),
144        should_or_not,
145    )
146    self.assertTrue(has_seg_fault == expect_seg_fault, msg)
147
148  def testDefaultBehavior(self):
149    """Tests the behavior of the default mode."""
150
151    self.RunAndVerify(env_var_value=None, flag_value=None, expect_seg_fault=0)
152
153  def testEnvVar(self):
154    """Tests using the GTEST_BREAK_ON_FAILURE environment variable."""
155
156    self.RunAndVerify(env_var_value='0', flag_value=None, expect_seg_fault=0)
157    self.RunAndVerify(env_var_value='1', flag_value=None, expect_seg_fault=1)
158
159  def testFlag(self):
160    """Tests using the --gtest_break_on_failure flag."""
161
162    self.RunAndVerify(env_var_value=None, flag_value='0', expect_seg_fault=0)
163    self.RunAndVerify(env_var_value=None, flag_value='1', expect_seg_fault=1)
164
165  def testFlagOverridesEnvVar(self):
166    """Tests that the flag overrides the environment variable."""
167
168    self.RunAndVerify(env_var_value='0', flag_value='0', expect_seg_fault=0)
169    self.RunAndVerify(env_var_value='0', flag_value='1', expect_seg_fault=1)
170    self.RunAndVerify(env_var_value='1', flag_value='0', expect_seg_fault=0)
171    self.RunAndVerify(env_var_value='1', flag_value='1', expect_seg_fault=1)
172
173  def testBreakOnFailureOverridesThrowOnFailure(self):
174    """Tests that gtest_break_on_failure overrides gtest_throw_on_failure."""
175
176    SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1')
177    try:
178      self.RunAndVerify(env_var_value=None, flag_value='1', expect_seg_fault=1)
179    finally:
180      SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None)
181
182  if IS_WINDOWS:
183
184    def testCatchExceptionsDoesNotInterfere(self):
185      """Tests that gtest_catch_exceptions doesn't interfere."""
186
187      SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1')
188      try:
189        self.RunAndVerify(env_var_value='1', flag_value='1', expect_seg_fault=1)
190      finally:
191        SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None)
192
193
194if __name__ == '__main__':
195  gtest_test_utils.Main()
196