11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#!/usr/bin/env python 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Copyright 2006, 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"""Unit test for Google Test's break-on-failure mode. 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaA user can ask Google Test to seg-fault when an assertion fails, using 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaeither the GTEST_BREAK_ON_FAILURE environment variable or the 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania--gtest_break_on_failure flag. This script tests such functionality 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaby invoking gtest_break_on_failure_unittest_ (a program written with 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaGoogle Test) with different environments and command line flags. 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania""" 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania__author__ = 'wan@google.com (Zhanyong Wan)' 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport gtest_test_utils 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport os 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaimport sys 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Constants. 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5041d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotIS_WINDOWS = os.name == 'nt' 5141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The environment variable for enabling/disabling the break-on-failure mode. 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaBREAK_ON_FAILURE_ENV_VAR = 'GTEST_BREAK_ON_FAILURE' 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The command line flag for enabling/disabling the break-on-failure mode. 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaBREAK_ON_FAILURE_FLAG = 'gtest_break_on_failure' 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The environment variable for enabling/disabling the throw-on-failure mode. 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTHROW_ON_FAILURE_ENV_VAR = 'GTEST_THROW_ON_FAILURE' 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# The environment variable for enabling/disabling the catch-exceptions mode. 6241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotCATCH_EXCEPTIONS_ENV_VAR = 'GTEST_CATCH_EXCEPTIONS' 6341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# Path to the gtest_break_on_failure_unittest_ program. 6541d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotEXE_PATH = gtest_test_utils.GetTestExecutablePath( 6641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 'gtest_break_on_failure_unittest_') 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 69fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesenviron = gtest_test_utils.environ 70fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesSetEnvVar = gtest_test_utils.SetEnvVar 71fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 72fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# Tests in this file run a Google-Test-based test program and expect it 73fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# to terminate prematurely. Therefore they are incompatible with 74fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# the premature-exit-file protocol by design. Unset the 75fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# premature-exit filepath to prevent Google Test from creating 76fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes# the file. 77fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesSetEnvVar(gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR, None) 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniadef Run(command): 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Runs a command; returns 1 if it was killed by a signal, or 0 otherwise.""" 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot p = gtest_test_utils.Subprocess(command, env=environ) 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if p.terminated_by_signal: 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 1 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else: 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 0 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania# The tests. 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass GTestBreakOnFailureUnitTest(gtest_test_utils.TestCase): 941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests using the GTEST_BREAK_ON_FAILURE environment variable or 951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania the --gtest_break_on_failure flag to turn assertion failures into 961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania segmentation faults. 971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """ 981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def RunAndVerify(self, env_var_value, flag_value, expect_seg_fault): 1001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Runs gtest_break_on_failure_unittest_ and verifies that it does 1011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (or does not) have a seg-fault. 1021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Args: 1041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania env_var_value: value of the GTEST_BREAK_ON_FAILURE environment 1051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania variable; None if the variable should be unset. 1061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value: value of the --gtest_break_on_failure flag; 1071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania None if the flag should not be present. 1081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault: 1 if the program is expected to generate a seg-fault; 1091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 0 otherwise. 1101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """ 1111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, env_var_value) 1131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if env_var_value is None: 1151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania env_var_value_msg = ' is not set' 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else: 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania env_var_value_msg = '=' + env_var_value 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if flag_value is None: 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag = '' 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania elif flag_value == '0': 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag = '--%s=0' % BREAK_ON_FAILURE_FLAG 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else: 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag = '--%s' % BREAK_ON_FAILURE_FLAG 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania command = [EXE_PATH] 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if flag: 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania command.append(flag) 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if expect_seg_fault: 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_or_not = 'should' 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania else: 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_or_not = 'should not' 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania has_seg_fault = Run(command) 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetEnvVar(BREAK_ON_FAILURE_ENV_VAR, None) 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania msg = ('when %s%s, an assertion failure in "%s" %s cause a seg-fault.' % 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania (BREAK_ON_FAILURE_ENV_VAR, env_var_value_msg, ' '.join(command), 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania should_or_not)) 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.assert_(has_seg_fault == expect_seg_fault, msg) 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def testDefaultBehavior(self): 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests the behavior of the default mode.""" 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value=None, 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value=None, 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=0) 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def testEnvVar(self): 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests using the GTEST_BREAK_ON_FAILURE environment variable.""" 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='0', 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value=None, 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=0) 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='1', 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value=None, 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=1) 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def testFlag(self): 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests using the --gtest_break_on_failure flag.""" 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value=None, 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='0', 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=0) 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value=None, 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='1', 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=1) 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def testFlagOverridesEnvVar(self): 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests that the flag overrides the environment variable.""" 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='0', 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='0', 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=0) 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='0', 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='1', 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=1) 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='1', 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='0', 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=0) 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value='1', 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='1', 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=1) 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania def testBreakOnFailureOverridesThrowOnFailure(self): 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania """Tests that gtest_break_on_failure overrides gtest_throw_on_failure.""" 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetEnvVar(THROW_ON_FAILURE_ENV_VAR, '1') 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try: 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania self.RunAndVerify(env_var_value=None, 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania flag_value='1', 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania expect_seg_fault=1) 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania finally: 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SetEnvVar(THROW_ON_FAILURE_ENV_VAR, None) 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 19841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot if IS_WINDOWS: 19941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot def testCatchExceptionsDoesNotInterfere(self): 20041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot """Tests that gtest_catch_exceptions doesn't interfere.""" 20141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 20241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, '1') 20341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot try: 20441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot self.RunAndVerify(env_var_value='1', 20541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot flag_value='1', 20641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot expect_seg_fault=1) 20741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot finally: 20841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SetEnvVar(CATCH_EXCEPTIONS_ENV_VAR, None) 20941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 21041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaif __name__ == '__main__': 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania gtest_test_utils.Main() 213