Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest_throw_on_failure_test.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file gtest_throw_on_failure_test.py
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 
34 This script invokes gtest_throw_on_failure_test_ (a program written with
35 Google Test) with different environments and command line flags.
36 """
37 
38 __author__ = 'wan@google.com (Zhanyong Wan)'
39 
40 import os
41 import gtest_test_utils
42 
43 
44 # Constants.
45 
46 # The command line flag for enabling/disabling the throw-on-failure mode.
47 THROW_ON_FAILURE = 'gtest_throw_on_failure'
48 
49 # Path to the gtest_throw_on_failure_test_ program, compiled with
50 # exceptions disabled.
52  'gtest_throw_on_failure_test_')
53 
54 
55 # Utilities.
56 
57 
58 def SetEnvVar(env_var, value):
59  """Sets an environment variable to a given value; unsets it when the
60  given value is None.
61  """
62 
63  env_var = env_var.upper()
64  if value is not None:
65  os.environ[env_var] = value
66  elif env_var in os.environ:
67  del os.environ[env_var]
68 
69 
70 def Run(command):
71  """Runs a command; returns True/False if its exit code is/isn't 0."""
72 
73  print('Running "%s". . .' % ' '.join(command))
74  p = gtest_test_utils.Subprocess(command)
75  return p.exited and p.exit_code == 0
76 
77 
78 # The tests. TODO(wan@google.com): refactor the class to share common
79 # logic with code in gtest_break_on_failure_unittest.py.
81  """Tests the throw-on-failure mode."""
82 
83  def RunAndVerify(self, env_var_value, flag_value, should_fail):
84  """Runs gtest_throw_on_failure_test_ and verifies that it does
85  (or does not) exit with a non-zero code.
86 
87  Args:
88  env_var_value: value of the GTEST_BREAK_ON_FAILURE environment
89  variable; None if the variable should be unset.
90  flag_value: value of the --gtest_break_on_failure flag;
91  None if the flag should not be present.
92  should_fail: True iff the program is expected to fail.
93  """
94 
95  SetEnvVar(THROW_ON_FAILURE, env_var_value)
96 
97  if env_var_value is None:
98  env_var_value_msg = ' is not set'
99  else:
100  env_var_value_msg = '=' + env_var_value
101 
102  if flag_value is None:
103  flag = ''
104  elif flag_value == '0':
105  flag = '--%s=0' % THROW_ON_FAILURE
106  else:
107  flag = '--%s' % THROW_ON_FAILURE
108 
109  command = [EXE_PATH]
110  if flag:
111  command.append(flag)
112 
113  if should_fail:
114  should_or_not = 'should'
115  else:
116  should_or_not = 'should not'
117 
118  failed = not Run(command)
119 
120  SetEnvVar(THROW_ON_FAILURE, None)
121 
122  msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero '
123  'exit code.' %
124  (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command),
125  should_or_not))
126  self.assert_(failed == should_fail, msg)
127 
129  """Tests the behavior of the default mode."""
130 
131  self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
132 
134  """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
135 
136  self.RunAndVerify(env_var_value='0',
137  flag_value=None,
138  should_fail=False)
139  self.RunAndVerify(env_var_value='1',
140  flag_value=None,
141  should_fail=True)
142 
144  """Tests using the --gtest_throw_on_failure flag."""
145 
146  self.RunAndVerify(env_var_value=None,
147  flag_value='0',
148  should_fail=False)
149  self.RunAndVerify(env_var_value=None,
150  flag_value='1',
151  should_fail=True)
152 
154  """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
155 
156  self.RunAndVerify(env_var_value='0',
157  flag_value='0',
158  should_fail=False)
159  self.RunAndVerify(env_var_value='0',
160  flag_value='1',
161  should_fail=True)
162  self.RunAndVerify(env_var_value='1',
163  flag_value='0',
164  should_fail=False)
165  self.RunAndVerify(env_var_value='1',
166  flag_value='1',
167  should_fail=True)
168 
169 
170 if __name__ == '__main__':