11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python
21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2009, Google Inc.
41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# All rights reserved.
51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Redistribution and use in source and binary forms, with or without
71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# modification, are permitted provided that the following conditions are
81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# met:
91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions of source code must retain the above copyright
111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# notice, this list of conditions and the following disclaimer.
121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Redistributions in binary form must reproduce the above
131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# copyright notice, this list of conditions and the following disclaimer
141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# in the documentation and/or other materials provided with the
151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# distribution.
161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#     * Neither the name of Google Inc. nor the names of its
171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# contributors may be used to endorse or promote products derived from
181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# this software without specific prior written permission.
191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#
201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""Tests Google Test's throw-on-failure mode with exceptions disabled.
331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaThis script invokes gtest_throw_on_failure_test_ (a program written with
351be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGoogle Test) with different environments and command line flags.
361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania"""
371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'wan@google.com (Zhanyong Wan)'
391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os
4141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotimport gtest_test_utils
421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Constants.
451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The command line flag for enabling/disabling the throw-on-failure mode.
471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTHROW_ON_FAILURE = 'gtest_throw_on_failure'
481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Path to the gtest_throw_on_failure_test_ program, compiled with
501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# exceptions disabled.
5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotEXE_PATH = gtest_test_utils.GetTestExecutablePath(
5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot    'gtest_throw_on_failure_test_')
531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Utilities.
561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef SetEnvVar(env_var, value):
591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Sets an environment variable to a given value; unsets it when the
601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  given value is None.
611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """
621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  env_var = env_var.upper()
641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  if value is not None:
651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    os.environ[env_var] = value
661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  elif env_var in os.environ:
671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    del os.environ[env_var]
681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef Run(command):
711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Runs a command; returns True/False if its exit code is/isn't 0."""
721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  print 'Running "%s". . .' % ' '.join(command)
7441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  p = gtest_test_utils.Subprocess(command)
7541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot  return p.exited and p.exit_code == 0
761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The tests.  TODO(wan@google.com): refactor the class to share common
791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# logic with code in gtest_break_on_failure_unittest.py.
8041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass ThrowOnFailureTest(gtest_test_utils.TestCase):
811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  """Tests the throw-on-failure mode."""
821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def RunAndVerify(self, env_var_value, flag_value, should_fail):
841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Runs gtest_throw_on_failure_test_ and verifies that it does
851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    (or does not) exit with a non-zero code.
861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    Args:
881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      env_var_value:    value of the GTEST_BREAK_ON_FAILURE environment
891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        variable; None if the variable should be unset.
901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      flag_value:       value of the --gtest_break_on_failure flag;
911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                        None if the flag should not be present.
921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      should_fail:      True iff the program is expected to fail.
931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """
941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    SetEnvVar(THROW_ON_FAILURE, env_var_value)
961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if env_var_value is None:
981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      env_var_value_msg = ' is not set'
991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      env_var_value_msg = '=' + env_var_value
1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if flag_value is None:
1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      flag = ''
1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    elif flag_value == '0':
1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      flag = '--%s=0' % THROW_ON_FAILURE
1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      flag = '--%s' % THROW_ON_FAILURE
1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    command = [EXE_PATH]
1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if flag:
1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      command.append(flag)
1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    if should_fail:
1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      should_or_not = 'should'
1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    else:
1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania      should_or_not = 'should not'
1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    failed = not Run(command)
1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    SetEnvVar(THROW_ON_FAILURE, None)
1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    msg = ('when %s%s, an assertion failure in "%s" %s cause a non-zero '
1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania           'exit code.' %
1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania           (THROW_ON_FAILURE, env_var_value_msg, ' '.join(command),
1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania            should_or_not))
1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.assert_(failed == should_fail, msg)
1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testDefaultBehavior(self):
1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests the behavior of the default mode."""
1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value=None, flag_value=None, should_fail=False)
1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testThrowOnFailureEnvVar(self):
1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests using the GTEST_THROW_ON_FAILURE environment variable."""
1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='0',
1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value=None,
1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=False)
1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='1',
1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value=None,
1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=True)
1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testThrowOnFailureFlag(self):
1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests using the --gtest_throw_on_failure flag."""
1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value=None,
1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='0',
1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=False)
1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value=None,
1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='1',
1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=True)
1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  def testThrowOnFailureFlagOverridesEnvVar(self):
1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    """Tests that --gtest_throw_on_failure overrides GTEST_THROW_ON_FAILURE."""
1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='0',
1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='0',
1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=False)
1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='0',
1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='1',
1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=True)
1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='1',
1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='0',
1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=False)
1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania    self.RunAndVerify(env_var_value='1',
1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      flag_value='1',
1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania                      should_fail=True)
1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania
1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == '__main__':
1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania  gtest_test_utils.Main()
172