OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2006, Google Inc. | 3 # Copyright 2006, Google Inc. |
4 # All rights reserved. | 4 # All rights reserved. |
5 # | 5 # |
6 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
7 # modification, are permitted provided that the following conditions are | 7 # modification, are permitted provided that the following conditions are |
8 # met: | 8 # met: |
9 # | 9 # |
10 # * Redistributions of source code must retain the above copyright | 10 # * Redistributions of source code must retain the above copyright |
(...skipping 17 matching lines...) Expand all Loading... |
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | 31 |
32 """Unit test utilities for Google C++ Mocking Framework.""" | 32 """Unit test utilities for Google C++ Mocking Framework.""" |
33 | 33 |
34 __author__ = 'wan@google.com (Zhanyong Wan)' | 34 __author__ = 'wan@google.com (Zhanyong Wan)' |
35 | 35 |
36 import os | 36 import os |
37 import sys | 37 import sys |
38 import unittest | 38 |
| 39 |
| 40 # Determines path to gtest_test_utils and imports it. |
| 41 SCRIPT_DIR = os.path.dirname(__file__) or '.' |
| 42 |
| 43 # isdir resolves symbolic links. |
| 44 gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test') |
| 45 if os.path.isdir(gtest_tests_util_dir): |
| 46 GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir |
| 47 else: |
| 48 GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test') |
| 49 |
| 50 sys.path.append(GTEST_TESTS_UTIL_DIR) |
| 51 import gtest_test_utils # pylint: disable-msg=C6204 |
39 | 52 |
40 | 53 |
41 # Initially maps a flag to its default value. After | 54 # Initially maps a flag to its default value. After |
42 # _ParseAndStripGMockFlags() is called, maps a flag to its actual | 55 # _ParseAndStripGMockFlags() is called, maps a flag to its actual |
43 # value. | 56 # value. |
44 _flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]), | 57 _flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]), |
45 'gmock_build_dir': os.path.dirname(sys.argv[0])} | 58 'gmock_build_dir': os.path.dirname(sys.argv[0])} |
46 _gmock_flags_are_parsed = False | 59 _gmock_flags_are_parsed = False |
47 | 60 |
48 | 61 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 102 |
90 return os.path.abspath(GetFlag('gmock_source_dir')) | 103 return os.path.abspath(GetFlag('gmock_source_dir')) |
91 | 104 |
92 | 105 |
93 def GetBuildDir(): | 106 def GetBuildDir(): |
94 """Returns the absolute path of the directory where the test binaries are.""" | 107 """Returns the absolute path of the directory where the test binaries are.""" |
95 | 108 |
96 return os.path.abspath(GetFlag('gmock_build_dir')) | 109 return os.path.abspath(GetFlag('gmock_build_dir')) |
97 | 110 |
98 | 111 |
| 112 def 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 |
99 def GetExitStatus(exit_code): | 128 def GetExitStatus(exit_code): |
100 """Returns the argument to exit(), or -1 if exit() wasn't called. | 129 """Returns the argument to exit(), or -1 if exit() wasn't called. |
101 | 130 |
102 Args: | 131 Args: |
103 exit_code: the result value of os.system(command). | 132 exit_code: the result value of os.system(command). |
104 """ | 133 """ |
105 | 134 |
106 if os.name == 'nt': | 135 if os.name == 'nt': |
107 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns | 136 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns |
108 # the argument to exit() directly. | 137 # the argument to exit() directly. |
109 return exit_code | 138 return exit_code |
110 else: | 139 else: |
111 # On Unix, os.WEXITSTATUS() must be used to extract the exit status | 140 # On Unix, os.WEXITSTATUS() must be used to extract the exit status |
112 # from the result of os.system(). | 141 # from the result of os.system(). |
113 if os.WIFEXITED(exit_code): | 142 if os.WIFEXITED(exit_code): |
114 return os.WEXITSTATUS(exit_code) | 143 return os.WEXITSTATUS(exit_code) |
115 else: | 144 else: |
116 return -1 | 145 return -1 |
117 | 146 |
118 | 147 |
| 148 # Suppresses the "Invalid const name" lint complaint |
| 149 # pylint: disable-msg=C6409 |
| 150 |
| 151 # Exposes Subprocess from gtest_test_utils. |
| 152 Subprocess = gtest_test_utils.Subprocess |
| 153 |
| 154 # Exposes TestCase from gtest_test_utils. |
| 155 TestCase = gtest_test_utils.TestCase |
| 156 |
| 157 # pylint: enable-msg=C6409 |
| 158 |
| 159 |
119 def Main(): | 160 def Main(): |
120 """Runs the unit test.""" | 161 """Runs the unit test.""" |
121 | 162 |
122 # We must call _ParseAndStripGMockFlags() before calling | 163 # We must call _ParseAndStripGMockFlags() before calling |
123 # unittest.main(). Otherwise the latter will be confused by the | 164 # gtest_test_utils.Main(). Otherwise unittest.main it calls will be |
124 # --gmock_* flags. | 165 # confused by the --gmock_* flags. |
125 _ParseAndStripGMockFlags(sys.argv) | 166 _ParseAndStripGMockFlags(sys.argv) |
126 unittest.main() | 167 gtest_test_utils.Main() |
OLD | NEW |