gmock_test_utils.py revision 13481
113481Sgiacomo.travaglini@arm.com#!/usr/bin/env python 213481Sgiacomo.travaglini@arm.com# 313481Sgiacomo.travaglini@arm.com# Copyright 2006, Google Inc. 413481Sgiacomo.travaglini@arm.com# All rights reserved. 513481Sgiacomo.travaglini@arm.com# 613481Sgiacomo.travaglini@arm.com# Redistribution and use in source and binary forms, with or without 713481Sgiacomo.travaglini@arm.com# modification, are permitted provided that the following conditions are 813481Sgiacomo.travaglini@arm.com# met: 913481Sgiacomo.travaglini@arm.com# 1013481Sgiacomo.travaglini@arm.com# * Redistributions of source code must retain the above copyright 1113481Sgiacomo.travaglini@arm.com# notice, this list of conditions and the following disclaimer. 1213481Sgiacomo.travaglini@arm.com# * Redistributions in binary form must reproduce the above 1313481Sgiacomo.travaglini@arm.com# copyright notice, this list of conditions and the following disclaimer 1413481Sgiacomo.travaglini@arm.com# in the documentation and/or other materials provided with the 1513481Sgiacomo.travaglini@arm.com# distribution. 1613481Sgiacomo.travaglini@arm.com# * Neither the name of Google Inc. nor the names of its 1713481Sgiacomo.travaglini@arm.com# contributors may be used to endorse or promote products derived from 1813481Sgiacomo.travaglini@arm.com# this software without specific prior written permission. 1913481Sgiacomo.travaglini@arm.com# 2013481Sgiacomo.travaglini@arm.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2113481Sgiacomo.travaglini@arm.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2213481Sgiacomo.travaglini@arm.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2313481Sgiacomo.travaglini@arm.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2413481Sgiacomo.travaglini@arm.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2513481Sgiacomo.travaglini@arm.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2613481Sgiacomo.travaglini@arm.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2713481Sgiacomo.travaglini@arm.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2813481Sgiacomo.travaglini@arm.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2913481Sgiacomo.travaglini@arm.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3013481Sgiacomo.travaglini@arm.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3113481Sgiacomo.travaglini@arm.com 3213481Sgiacomo.travaglini@arm.com"""Unit test utilities for Google C++ Mocking Framework.""" 3313481Sgiacomo.travaglini@arm.com 3413481Sgiacomo.travaglini@arm.com__author__ = 'wan@google.com (Zhanyong Wan)' 3513481Sgiacomo.travaglini@arm.com 3613481Sgiacomo.travaglini@arm.comimport os 3713481Sgiacomo.travaglini@arm.comimport sys 3813481Sgiacomo.travaglini@arm.com 3913481Sgiacomo.travaglini@arm.com 4013481Sgiacomo.travaglini@arm.com# Determines path to gtest_test_utils and imports it. 4113481Sgiacomo.travaglini@arm.comSCRIPT_DIR = os.path.dirname(__file__) or '.' 4213481Sgiacomo.travaglini@arm.com 4313481Sgiacomo.travaglini@arm.com# isdir resolves symbolic links. 4413481Sgiacomo.travaglini@arm.comgtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test') 4513481Sgiacomo.travaglini@arm.comif os.path.isdir(gtest_tests_util_dir): 4613481Sgiacomo.travaglini@arm.com GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir 4713481Sgiacomo.travaglini@arm.comelse: 4813481Sgiacomo.travaglini@arm.com GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test') 4913481Sgiacomo.travaglini@arm.com 5013481Sgiacomo.travaglini@arm.comsys.path.append(GTEST_TESTS_UTIL_DIR) 5113481Sgiacomo.travaglini@arm.comimport gtest_test_utils # pylint: disable-msg=C6204 5213481Sgiacomo.travaglini@arm.com 5313481Sgiacomo.travaglini@arm.com 5413481Sgiacomo.travaglini@arm.comdef GetSourceDir(): 5513481Sgiacomo.travaglini@arm.com """Returns the absolute path of the directory where the .py files are.""" 5613481Sgiacomo.travaglini@arm.com 5713481Sgiacomo.travaglini@arm.com return gtest_test_utils.GetSourceDir() 5813481Sgiacomo.travaglini@arm.com 5913481Sgiacomo.travaglini@arm.com 6013481Sgiacomo.travaglini@arm.comdef GetTestExecutablePath(executable_name): 6113481Sgiacomo.travaglini@arm.com """Returns the absolute path of the test binary given its name. 6213481Sgiacomo.travaglini@arm.com 6313481Sgiacomo.travaglini@arm.com The function will print a message and abort the program if the resulting file 6413481Sgiacomo.travaglini@arm.com doesn't exist. 6513481Sgiacomo.travaglini@arm.com 6613481Sgiacomo.travaglini@arm.com Args: 6713481Sgiacomo.travaglini@arm.com executable_name: name of the test binary that the test script runs. 6813481Sgiacomo.travaglini@arm.com 6913481Sgiacomo.travaglini@arm.com Returns: 7013481Sgiacomo.travaglini@arm.com The absolute path of the test binary. 7113481Sgiacomo.travaglini@arm.com """ 7213481Sgiacomo.travaglini@arm.com 7313481Sgiacomo.travaglini@arm.com return gtest_test_utils.GetTestExecutablePath(executable_name) 7413481Sgiacomo.travaglini@arm.com 7513481Sgiacomo.travaglini@arm.com 7613481Sgiacomo.travaglini@arm.comdef GetExitStatus(exit_code): 7713481Sgiacomo.travaglini@arm.com """Returns the argument to exit(), or -1 if exit() wasn't called. 7813481Sgiacomo.travaglini@arm.com 7913481Sgiacomo.travaglini@arm.com Args: 8013481Sgiacomo.travaglini@arm.com exit_code: the result value of os.system(command). 8113481Sgiacomo.travaglini@arm.com """ 8213481Sgiacomo.travaglini@arm.com 8313481Sgiacomo.travaglini@arm.com if os.name == 'nt': 8413481Sgiacomo.travaglini@arm.com # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns 8513481Sgiacomo.travaglini@arm.com # the argument to exit() directly. 8613481Sgiacomo.travaglini@arm.com return exit_code 8713481Sgiacomo.travaglini@arm.com else: 8813481Sgiacomo.travaglini@arm.com # On Unix, os.WEXITSTATUS() must be used to extract the exit status 8913481Sgiacomo.travaglini@arm.com # from the result of os.system(). 9013481Sgiacomo.travaglini@arm.com if os.WIFEXITED(exit_code): 9113481Sgiacomo.travaglini@arm.com return os.WEXITSTATUS(exit_code) 9213481Sgiacomo.travaglini@arm.com else: 9313481Sgiacomo.travaglini@arm.com return -1 9413481Sgiacomo.travaglini@arm.com 9513481Sgiacomo.travaglini@arm.com 9613481Sgiacomo.travaglini@arm.com# Suppresses the "Invalid const name" lint complaint 9713481Sgiacomo.travaglini@arm.com# pylint: disable-msg=C6409 9813481Sgiacomo.travaglini@arm.com 9913481Sgiacomo.travaglini@arm.com# Exposes utilities from gtest_test_utils. 10013481Sgiacomo.travaglini@arm.comSubprocess = gtest_test_utils.Subprocess 10113481Sgiacomo.travaglini@arm.comTestCase = gtest_test_utils.TestCase 10213481Sgiacomo.travaglini@arm.comenviron = gtest_test_utils.environ 10313481Sgiacomo.travaglini@arm.comSetEnvVar = gtest_test_utils.SetEnvVar 10413481Sgiacomo.travaglini@arm.comPREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR 10513481Sgiacomo.travaglini@arm.com 10613481Sgiacomo.travaglini@arm.com# pylint: enable-msg=C6409 10713481Sgiacomo.travaglini@arm.com 10813481Sgiacomo.travaglini@arm.com 10913481Sgiacomo.travaglini@arm.comdef Main(): 11013481Sgiacomo.travaglini@arm.com """Runs the unit test.""" 11113481Sgiacomo.travaglini@arm.com 11213481Sgiacomo.travaglini@arm.com gtest_test_utils.Main() 113