blob: ae15a1089962aaa8750bc1bb828a22c77c5df236 [file] [log] [blame]
zhanyong.wana89034c2009-09-22 16:18:42 +00001#!/usr/bin/env python
shiqiane35fdd92008-12-10 05:08:54 +00002#
3# Copyright 2006, Google Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10# * Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# * Redistributions in binary form must reproduce the above
13# copyright notice, this list of conditions and the following disclaimer
14# in the documentation and/or other materials provided with the
15# distribution.
16# * Neither the name of Google Inc. nor the names of its
17# contributors may be used to endorse or promote products derived from
18# this software without specific prior written permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32"""Unit test utilities for Google C++ Mocking Framework."""
33
34__author__ = 'wan@google.com (Zhanyong Wan)'
35
36import os
37import sys
zhanyong.wan19eb9e92009-11-24 20:23:18 +000038
39# Determines path to gtest_test_utils and imports it.
40SCRIPT_DIR = os.path.dirname(__file__) or '.'
41
42# isdir resolves symbolic links.
43gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test')
44if os.path.isdir(gtest_tests_util_dir):
45 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
46else:
47 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test')
48
49sys.path.append(GTEST_TESTS_UTIL_DIR)
50import gtest_test_utils # pylint: disable-msg=C6204
shiqiane35fdd92008-12-10 05:08:54 +000051
52
53# Initially maps a flag to its default value. After
54# _ParseAndStripGMockFlags() is called, maps a flag to its actual
55# value.
56_flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]),
57 'gmock_build_dir': os.path.dirname(sys.argv[0])}
58_gmock_flags_are_parsed = False
59
60
61def _ParseAndStripGMockFlags(argv):
62 """Parses and strips Google Test flags from argv. This is idempotent."""
63
64 global _gmock_flags_are_parsed
65 if _gmock_flags_are_parsed:
66 return
67
68 _gmock_flags_are_parsed = True
69 for flag in _flag_map:
70 # The environment variable overrides the default value.
71 if flag.upper() in os.environ:
72 _flag_map[flag] = os.environ[flag.upper()]
73
74 # The command line flag overrides the environment variable.
75 i = 1 # Skips the program name.
76 while i < len(argv):
77 prefix = '--' + flag + '='
78 if argv[i].startswith(prefix):
79 _flag_map[flag] = argv[i][len(prefix):]
80 del argv[i]
81 break
82 else:
83 # We don't increment i in case we just found a --gmock_* flag
84 # and removed it from argv.
85 i += 1
86
87
88def GetFlag(flag):
89 """Returns the value of the given flag."""
90
91 # In case GetFlag() is called before Main(), we always call
92 # _ParseAndStripGMockFlags() here to make sure the --gmock_* flags
93 # are parsed.
94 _ParseAndStripGMockFlags(sys.argv)
95
96 return _flag_map[flag]
97
98
99def GetSourceDir():
100 """Returns the absolute path of the directory where the .py files are."""
101
102 return os.path.abspath(GetFlag('gmock_source_dir'))
103
104
105def GetBuildDir():
106 """Returns the absolute path of the directory where the test binaries are."""
107
108 return os.path.abspath(GetFlag('gmock_build_dir'))
109
110
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000111def GetTestExecutablePath(executable_name):
112 """Returns the absolute path of the test binary given its name.
113
114 The function will print a message and abort the program if the resulting file
115 doesn't exist.
116
117 Args:
118 executable_name: name of the test binary that the test script runs.
119
120 Returns:
121 The absolute path of the test binary.
122 """
123
124 return gtest_test_utils.GetTestExecutablePath(executable_name, GetBuildDir())
125
126
shiqiane35fdd92008-12-10 05:08:54 +0000127def GetExitStatus(exit_code):
128 """Returns the argument to exit(), or -1 if exit() wasn't called.
129
130 Args:
131 exit_code: the result value of os.system(command).
132 """
133
134 if os.name == 'nt':
135 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
136 # the argument to exit() directly.
137 return exit_code
138 else:
139 # On Unix, os.WEXITSTATUS() must be used to extract the exit status
140 # from the result of os.system().
141 if os.WIFEXITED(exit_code):
142 return os.WEXITSTATUS(exit_code)
143 else:
144 return -1
145
146
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000147# Exposes Subprocess from gtest_test_utils.
148Subprocess = gtest_test_utils.Subprocess # pylint: disable-msg=C6409
149
150
shiqiane35fdd92008-12-10 05:08:54 +0000151def Main():
152 """Runs the unit test."""
153
154 # We must call _ParseAndStripGMockFlags() before calling
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000155 # gtest_test_utils.Main(). Otherwise unittest.main it calls will be
156 # confused by the --gmock_* flags.
shiqiane35fdd92008-12-10 05:08:54 +0000157 _ParseAndStripGMockFlags(sys.argv)
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000158 gtest_test_utils.Main()