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