zhanyong.wan | a89034c | 2009-09-22 16:18:42 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 2 | # |
| 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 | |
| 36 | import os |
| 37 | import sys |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 38 | |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame^] | 39 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 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 |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 52 | |
| 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 | |
| 62 | def _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 | |
| 89 | def 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 | |
| 100 | def 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 | |
| 106 | def 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.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 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 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 128 | def 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.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame^] | 148 | # Suppresses the "Invalid const name" lint complaint |
| 149 | # pylint: disable-msg=C6409 |
| 150 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 151 | # Exposes Subprocess from gtest_test_utils. |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame^] | 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 |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 158 | |
| 159 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 160 | def Main(): |
| 161 | """Runs the unit test.""" |
| 162 | |
| 163 | # We must call _ParseAndStripGMockFlags() before calling |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 164 | # gtest_test_utils.Main(). Otherwise unittest.main it calls will be |
| 165 | # confused by the --gmock_* flags. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 166 | _ParseAndStripGMockFlags(sys.argv) |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 167 | gtest_test_utils.Main() |