Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2009, 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 | """Tests Google Test's throw-on-failure mode with exceptions disabled. |
| 33 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 34 | This script invokes googletest-throw-on-failure-test_ (a program written with |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 35 | Google Test) with different environments and command line flags. |
| 36 | """ |
| 37 | |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 38 | import os |
| 39 | import gtest_test_utils |
| 40 | |
| 41 | |
| 42 | # Constants. |
| 43 | |
| 44 | # The command line flag for enabling/disabling the throw-on-failure mode. |
| 45 | THROW_ON_FAILURE = 'gtest_throw_on_failure' |
| 46 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 47 | # Path to the googletest-throw-on-failure-test_ program, compiled with |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 48 | # exceptions disabled. |
| 49 | EXE_PATH = gtest_test_utils.GetTestExecutablePath( |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 50 | 'googletest-throw-on-failure-test_') |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | # Utilities. |
| 54 | |
| 55 | |
| 56 | def SetEnvVar(env_var, value): |
| 57 | """Sets an environment variable to a given value; unsets it when the |
| 58 | given value is None. |
| 59 | """ |
| 60 | |
| 61 | env_var = env_var.upper() |
| 62 | if value is not None: |
| 63 | os.environ[env_var] = value |
| 64 | elif env_var in os.environ: |
| 65 | del os.environ[env_var] |
| 66 | |
| 67 | |
| 68 | def Run(command): |
| 69 | """Runs a command; returns True/False if its exit code is/isn't 0.""" |
| 70 | |
| 71 | print 'Running "%s". . .' % ' '.join(command) |
| 72 | p = gtest_test_utils.Subprocess(command) |
| 73 | return p.exited and p.exit_code == 0 |
| 74 | |
| 75 | |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 76 | # The tests. FIXME: refactor the class to share common |
| 77 | # logic with code in googletest-break-on-failure-unittest.py. |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 78 | class ThrowOnFailureTest(gtest_test_utils.TestCase): |
| 79 | """Tests the throw-on-failure mode.""" |
| 80 | |
| 81 | def RunAndVerify(self, env_var_value, flag_value, should_fail): |
Austin Schuh | 889ac43 | 2018-10-29 22:57:02 -0700 | [diff] [blame^] | 82 | """Runs googletest-throw-on-failure-test_ and verifies that it does |
Austin Schuh | 0cbef62 | 2015-09-06 17:34:52 -0700 | [diff] [blame] | 83 | (or does not) exit with a non-zero code. |
| 84 | |
| 85 | Args: |
| 86 | env_var_value: value of the GTEST_BREAK_ON_FAILURE environment |
| 87 | variable; None if the variable should be unset. |
| 88 | flag_value: value of the --gtest_break_on_failure flag; |
| 89 | None if the flag should not be present. |
| 90 | should_fail: True iff the program is expected to fail. |
| 91 | """ |
| 92 | |
| 93 | SetEnvVar(THROW_ON_FAILURE, env_var_value) |
| 94 | |
| 95 | if env_var_value is None: |
| 96 | env_var_value_msg = ' is not set' |
| 97 | else: |
| 98 | env_var_value_msg = '=' + env_var_value |
| 99 | |
| 100 | if flag_value is None: |
| 101 | flag = '' |
| 102 | elif flag_value == '0': |
| 103 | flag = '--%s=0' % THROW_ON_FAILURE |
| 104 | else: |
| 105 | flag = '--%s' % THROW_ON_FAILURE |
| 106 | |
| 107 | command = [EXE_PATH] |
| 108 | if flag: |
| 109 | command.append(flag) |
| 110 | |
| 111 | if should_fail: |
| 112 | should_or_not = 'should' |
| 113 | else: |
| 114 | should_or_not = 'should not' |
| 115 | |
| 116 | failed = not Run(command) |
| 117 | |
| 118 | SetEnvVar(THROW_ON_FAILURE, None) |
| 119 | |
| 120 | msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero ' |
| 121 | 'exit code.' % |
| 122 | (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command), |
| 123 | should_or_not)) |
| 124 | self.assert_(failed == should_fail, msg) |
| 125 | |
| 126 | def testDefaultBehavior(self): |
| 127 | """Tests the behavior of the default mode.""" |
| 128 | |
| 129 | self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False) |
| 130 | |
| 131 | def testThrowOnFailureEnvVar(self): |
| 132 | """Tests using the GTEST_THROW_ON_FAILURE environment variable.""" |
| 133 | |
| 134 | self.RunAndVerify(env_var_value='0', |
| 135 | flag_value=None, |
| 136 | should_fail=False) |
| 137 | self.RunAndVerify(env_var_value='1', |
| 138 | flag_value=None, |
| 139 | should_fail=True) |
| 140 | |
| 141 | def testThrowOnFailureFlag(self): |
| 142 | """Tests using the --gtest_throw_on_failure flag.""" |
| 143 | |
| 144 | self.RunAndVerify(env_var_value=None, |
| 145 | flag_value='0', |
| 146 | should_fail=False) |
| 147 | self.RunAndVerify(env_var_value=None, |
| 148 | flag_value='1', |
| 149 | should_fail=True) |
| 150 | |
| 151 | def testThrowOnFailureFlagOverridesEnvVar(self): |
| 152 | """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE.""" |
| 153 | |
| 154 | self.RunAndVerify(env_var_value='0', |
| 155 | flag_value='0', |
| 156 | should_fail=False) |
| 157 | self.RunAndVerify(env_var_value='0', |
| 158 | flag_value='1', |
| 159 | should_fail=True) |
| 160 | self.RunAndVerify(env_var_value='1', |
| 161 | flag_value='0', |
| 162 | should_fail=False) |
| 163 | self.RunAndVerify(env_var_value='1', |
| 164 | flag_value='1', |
| 165 | should_fail=True) |
| 166 | |
| 167 | |
| 168 | if __name__ == '__main__': |
| 169 | gtest_test_utils.Main() |