blob: 1983c53bc3166be7e92c4f398e40e38d5d58f58c [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001# 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
32__author__ = 'wan@google.com (Zhanyong Wan)'
33
34import os
35import sys
zhanyong.wan19eb9e92009-11-24 20:23:18 +000036
zhanyong.wanf6d6a222009-12-01 19:42:25 +000037
zhanyong.wan19eb9e92009-11-24 20:23:18 +000038# Determines path to gtest_test_utils and imports it.
39SCRIPT_DIR = os.path.dirname(__file__) or '.'
40
41# isdir resolves symbolic links.
Gennadiy Civil6c0c3892018-01-26 16:30:57 -050042gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../googletest/test')
zhanyong.wan19eb9e92009-11-24 20:23:18 +000043if os.path.isdir(gtest_tests_util_dir):
44 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
45else:
Gennadiy Civil6c0c3892018-01-26 16:30:57 -050046 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../googletest/test')
zhanyong.wan19eb9e92009-11-24 20:23:18 +000047
48sys.path.append(GTEST_TESTS_UTIL_DIR)
49import gtest_test_utils # pylint: disable-msg=C6204
shiqiane35fdd92008-12-10 05:08:54 +000050
51
shiqiane35fdd92008-12-10 05:08:54 +000052def GetSourceDir():
53 """Returns the absolute path of the directory where the .py files are."""
54
vladloseve2e8ba42010-05-13 18:16:03 +000055 return gtest_test_utils.GetSourceDir()
shiqiane35fdd92008-12-10 05:08:54 +000056
57
zhanyong.wan19eb9e92009-11-24 20:23:18 +000058def GetTestExecutablePath(executable_name):
59 """Returns the absolute path of the test binary given its name.
60
61 The function will print a message and abort the program if the resulting file
62 doesn't exist.
63
64 Args:
65 executable_name: name of the test binary that the test script runs.
66
67 Returns:
68 The absolute path of the test binary.
69 """
70
vladloseve2e8ba42010-05-13 18:16:03 +000071 return gtest_test_utils.GetTestExecutablePath(executable_name)
zhanyong.wan19eb9e92009-11-24 20:23:18 +000072
73
shiqiane35fdd92008-12-10 05:08:54 +000074def GetExitStatus(exit_code):
75 """Returns the argument to exit(), or -1 if exit() wasn't called.
76
77 Args:
78 exit_code: the result value of os.system(command).
79 """
80
81 if os.name == 'nt':
82 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
83 # the argument to exit() directly.
84 return exit_code
85 else:
86 # On Unix, os.WEXITSTATUS() must be used to extract the exit status
87 # from the result of os.system().
88 if os.WIFEXITED(exit_code):
89 return os.WEXITSTATUS(exit_code)
90 else:
91 return -1
92
93
zhanyong.wanf6d6a222009-12-01 19:42:25 +000094# Suppresses the "Invalid const name" lint complaint
95# pylint: disable-msg=C6409
96
zhanyong.wan45fef502013-09-06 22:52:14 +000097# Exposes utilities from gtest_test_utils.
zhanyong.wanf6d6a222009-12-01 19:42:25 +000098Subprocess = gtest_test_utils.Subprocess
zhanyong.wanf6d6a222009-12-01 19:42:25 +000099TestCase = gtest_test_utils.TestCase
zhanyong.wan45fef502013-09-06 22:52:14 +0000100environ = gtest_test_utils.environ
101SetEnvVar = gtest_test_utils.SetEnvVar
102PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR
zhanyong.wanf6d6a222009-12-01 19:42:25 +0000103
104# pylint: enable-msg=C6409
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000105
106
shiqiane35fdd92008-12-10 05:08:54 +0000107def Main():
108 """Runs the unit test."""
109
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000110 gtest_test_utils.Main()