xref: /freebsd/contrib/googletest/googlemock/test/gmock_test_utils.py (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1# Copyright 2006, Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Unit test utilities for Google C++ Mocking Framework."""
31
32import os
33import sys
34
35# Determines path to gtest_test_utils and imports it.
36SCRIPT_DIR = os.path.dirname(__file__) or '.'
37
38# isdir resolves symbolic links.
39gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../../googletest/test')
40if os.path.isdir(gtest_tests_util_dir):
41  GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
42else:
43  GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../googletest/test')
44sys.path.append(GTEST_TESTS_UTIL_DIR)
45
46# pylint: disable=C6204
47import gtest_test_utils
48
49
50def GetSourceDir():
51  """Returns the absolute path of the directory where the .py files are."""
52
53  return gtest_test_utils.GetSourceDir()
54
55
56def GetTestExecutablePath(executable_name):
57  """Returns the absolute path of the test binary given its name.
58
59  The function will print a message and abort the program if the resulting file
60  doesn't exist.
61
62  Args:
63    executable_name: name of the test binary that the test script runs.
64
65  Returns:
66    The absolute path of the test binary.
67  """
68
69  return gtest_test_utils.GetTestExecutablePath(executable_name)
70
71
72def GetExitStatus(exit_code):
73  """Returns the argument to exit(), or -1 if exit() wasn't called.
74
75  Args:
76    exit_code: the result value of os.system(command).
77  """
78
79  if os.name == 'nt':
80    # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
81    # the argument to exit() directly.
82    return exit_code
83  else:
84    # On Unix, os.WEXITSTATUS() must be used to extract the exit status
85    # from the result of os.system().
86    if os.WIFEXITED(exit_code):
87      return os.WEXITSTATUS(exit_code)
88    else:
89      return -1
90
91
92# Suppresses the "Invalid const name" lint complaint
93# pylint: disable-msg=C6409
94
95# Exposes utilities from gtest_test_utils.
96Subprocess = gtest_test_utils.Subprocess
97TestCase = gtest_test_utils.TestCase
98environ = gtest_test_utils.environ
99SetEnvVar = gtest_test_utils.SetEnvVar
100PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR
101
102# pylint: enable-msg=C6409
103
104
105def Main():
106  """Runs the unit test."""
107
108  gtest_test_utils.Main()
109