blob: fa896a47a969b1ab3c073d1081f40f52d4343c78 [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
zhanyong.wanf6d6a222009-12-01 19:42:25 +000039
zhanyong.wan19eb9e92009-11-24 20:23:18 +000040# Determines path to gtest_test_utils and imports it.
41SCRIPT_DIR = os.path.dirname(__file__) or '.'
42
43# isdir resolves symbolic links.
44gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test')
45if os.path.isdir(gtest_tests_util_dir):
46 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
47else:
48 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test')
49
50sys.path.append(GTEST_TESTS_UTIL_DIR)
51import gtest_test_utils # pylint: disable-msg=C6204
shiqiane35fdd92008-12-10 05:08:54 +000052
53
54# Initially maps a flag to its default value. After
55# _ParseAndStripGMockFlags() is called, maps a flag to its actual
56# value.
57_flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]),
58 'gmock_build_dir': os.path.dirname(sys.argv[0])}
59_gmock_flags_are_parsed = False
60
61
62def _ParseAndStripGMockFlags(argv):
63 """Parses and strips Google Test flags from argv. This is idempotent."""
64
65 global _gmock_flags_are_parsed
66 if _gmock_flags_are_parsed:
67 return
68
69 _gmock_flags_are_parsed = True
70 for flag in _flag_map:
71 # The environment variable overrides the default value.
72 if flag.upper() in os.environ:
73 _flag_map[flag] = os.environ[flag.upper()]
74
75 # The command line flag overrides the environment variable.
76 i = 1 # Skips the program name.
77 while i < len(argv):
78 prefix = '--' + flag + '='
79 if argv[i].startswith(prefix):
80 _flag_map[flag] = argv[i][len(prefix):]
81 del argv[i]
82 break
83 else:
84 # We don't increment i in case we just found a --gmock_* flag
85 # and removed it from argv.
86 i += 1
87
88
89def GetFlag(flag):
90 """Returns the value of the given flag."""
91
92 # In case GetFlag() is called before Main(), we always call
93 # _ParseAndStripGMockFlags() here to make sure the --gmock_* flags
94 # are parsed.
95 _ParseAndStripGMockFlags(sys.argv)
96
97 return _flag_map[flag]
98
99
100def GetSourceDir():
101 """Returns the absolute path of the directory where the .py files are."""
102
103 return os.path.abspath(GetFlag('gmock_source_dir'))
104
105
106def GetBuildDir():
107 """Returns the absolute path of the directory where the test binaries are."""
108
109 return os.path.abspath(GetFlag('gmock_build_dir'))
110
111
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000112def GetTestExecutablePath(executable_name):
113 """Returns the absolute path of the test binary given its name.
114
115 The function will print a message and abort the program if the resulting file
116 doesn't exist.
117
118 Args:
119 executable_name: name of the test binary that the test script runs.
120
121 Returns:
122 The absolute path of the test binary.
123 """
124
125 return gtest_test_utils.GetTestExecutablePath(executable_name, GetBuildDir())
126
127
shiqiane35fdd92008-12-10 05:08:54 +0000128def GetExitStatus(exit_code):
129 """Returns the argument to exit(), or -1 if exit() wasn't called.
130
131 Args:
132 exit_code: the result value of os.system(command).
133 """
134
135 if os.name == 'nt':
136 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
137 # the argument to exit() directly.
138 return exit_code
139 else:
140 # On Unix, os.WEXITSTATUS() must be used to extract the exit status
141 # from the result of os.system().
142 if os.WIFEXITED(exit_code):
143 return os.WEXITSTATUS(exit_code)
144 else:
145 return -1
146
147
zhanyong.wanf6d6a222009-12-01 19:42:25 +0000148# Suppresses the "Invalid const name" lint complaint
149# pylint: disable-msg=C6409
150
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000151# Exposes Subprocess from gtest_test_utils.
zhanyong.wanf6d6a222009-12-01 19:42:25 +0000152Subprocess = gtest_test_utils.Subprocess
153
154# Exposes TestCase from gtest_test_utils.
155TestCase = gtest_test_utils.TestCase
156
157# pylint: enable-msg=C6409
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000158
159
shiqiane35fdd92008-12-10 05:08:54 +0000160def Main():
161 """Runs the unit test."""
162
163 # We must call _ParseAndStripGMockFlags() before calling
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000164 # gtest_test_utils.Main(). Otherwise unittest.main it calls will be
165 # confused by the --gmock_* flags.
shiqiane35fdd92008-12-10 05:08:54 +0000166 _ParseAndStripGMockFlags(sys.argv)
zhanyong.wan19eb9e92009-11-24 20:23:18 +0000167 gtest_test_utils.Main()