gmock_test_utils.py revision 13481
112955Sgabeblack@google.com#!/usr/bin/env python
212955Sgabeblack@google.com#
312955Sgabeblack@google.com# Copyright 2006, Google Inc.
412955Sgabeblack@google.com# All rights reserved.
512955Sgabeblack@google.com#
612955Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
712955Sgabeblack@google.com# modification, are permitted provided that the following conditions are
812955Sgabeblack@google.com# met:
912955Sgabeblack@google.com#
1012955Sgabeblack@google.com#     * Redistributions of source code must retain the above copyright
1112955Sgabeblack@google.com# notice, this list of conditions and the following disclaimer.
1212955Sgabeblack@google.com#     * Redistributions in binary form must reproduce the above
1312955Sgabeblack@google.com# copyright notice, this list of conditions and the following disclaimer
1412955Sgabeblack@google.com# in the documentation and/or other materials provided with the
1512955Sgabeblack@google.com# distribution.
1612955Sgabeblack@google.com#     * Neither the name of Google Inc. nor the names of its
1712955Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1812955Sgabeblack@google.com# this software without specific prior written permission.
1912955Sgabeblack@google.com#
2012955Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2112955Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2212955Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2312955Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2412955Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2512955Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2612955Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2712955Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2812955Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2912955Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3012955Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3112955Sgabeblack@google.com
3212955Sgabeblack@google.com"""Unit test utilities for Google C++ Mocking Framework."""
3313193Sgabeblack@google.com
3412955Sgabeblack@google.com__author__ = 'wan@google.com (Zhanyong Wan)'
3512955Sgabeblack@google.com
3612955Sgabeblack@google.comimport os
3712957Sgabeblack@google.comimport sys
3812955Sgabeblack@google.com
3912955Sgabeblack@google.com
4013206Sgabeblack@google.com# Determines path to gtest_test_utils and imports it.
4113063Sgabeblack@google.comSCRIPT_DIR = os.path.dirname(__file__) or '.'
4213206Sgabeblack@google.com
4312955Sgabeblack@google.com# isdir resolves symbolic links.
4412955Sgabeblack@google.comgtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test')
4512955Sgabeblack@google.comif os.path.isdir(gtest_tests_util_dir):
4612955Sgabeblack@google.com  GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
4712955Sgabeblack@google.comelse:
4812955Sgabeblack@google.com  GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test')
4912955Sgabeblack@google.com
5012955Sgabeblack@google.comsys.path.append(GTEST_TESTS_UTIL_DIR)
5112955Sgabeblack@google.comimport gtest_test_utils  # pylint: disable-msg=C6204
5212955Sgabeblack@google.com
5312955Sgabeblack@google.com
5412955Sgabeblack@google.comdef GetSourceDir():
5512955Sgabeblack@google.com  """Returns the absolute path of the directory where the .py files are."""
5612955Sgabeblack@google.com
5712955Sgabeblack@google.com  return gtest_test_utils.GetSourceDir()
5812957Sgabeblack@google.com
5912957Sgabeblack@google.com
6012955Sgabeblack@google.comdef GetTestExecutablePath(executable_name):
6112955Sgabeblack@google.com  """Returns the absolute path of the test binary given its name.
6212955Sgabeblack@google.com
6313303Sgabeblack@google.com  The function will print a message and abort the program if the resulting file
6413303Sgabeblack@google.com  doesn't exist.
6513303Sgabeblack@google.com
6612955Sgabeblack@google.com  Args:
6712955Sgabeblack@google.com    executable_name: name of the test binary that the test script runs.
6812955Sgabeblack@google.com
6912955Sgabeblack@google.com  Returns:
7012955Sgabeblack@google.com    The absolute path of the test binary.
7112955Sgabeblack@google.com  """
7212955Sgabeblack@google.com
7312955Sgabeblack@google.com  return gtest_test_utils.GetTestExecutablePath(executable_name)
7412955Sgabeblack@google.com
7512955Sgabeblack@google.com
7613208Sgabeblack@google.comdef GetExitStatus(exit_code):
7713208Sgabeblack@google.com  """Returns the argument to exit(), or -1 if exit() wasn't called.
7813208Sgabeblack@google.com
7912955Sgabeblack@google.com  Args:
8012955Sgabeblack@google.com    exit_code: the result value of os.system(command).
8112955Sgabeblack@google.com  """
8212955Sgabeblack@google.com
8312955Sgabeblack@google.com  if os.name == 'nt':
8412955Sgabeblack@google.com    # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
8512955Sgabeblack@google.com    # the argument to exit() directly.
8613299Sgabeblack@google.com    return exit_code
8712955Sgabeblack@google.com  else:
8812955Sgabeblack@google.com    # On Unix, os.WEXITSTATUS() must be used to extract the exit status
8912955Sgabeblack@google.com    # from the result of os.system().
9013243Sgabeblack@google.com    if os.WIFEXITED(exit_code):
9112955Sgabeblack@google.com      return os.WEXITSTATUS(exit_code)
9212955Sgabeblack@google.com    else:
9312955Sgabeblack@google.com      return -1
9412955Sgabeblack@google.com
9512955Sgabeblack@google.com
9612955Sgabeblack@google.com# Suppresses the "Invalid const name" lint complaint
9712955Sgabeblack@google.com# pylint: disable-msg=C6409
9812955Sgabeblack@google.com
9912955Sgabeblack@google.com# Exposes utilities from gtest_test_utils.
10012955Sgabeblack@google.comSubprocess = gtest_test_utils.Subprocess
10112955Sgabeblack@google.comTestCase = gtest_test_utils.TestCase
10212955Sgabeblack@google.comenviron = gtest_test_utils.environ
10312955Sgabeblack@google.comSetEnvVar = gtest_test_utils.SetEnvVar
10413206Sgabeblack@google.comPREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR
10513206Sgabeblack@google.com
10613206Sgabeblack@google.com# pylint: enable-msg=C6409
10713206Sgabeblack@google.com
10813206Sgabeblack@google.com
10913208Sgabeblack@google.comdef Main():
11013208Sgabeblack@google.com  """Runs the unit test."""
11113206Sgabeblack@google.com
11213206Sgabeblack@google.com  gtest_test_utils.Main()
11313206Sgabeblack@google.com