shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | # 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 | |
| 34 | import os |
| 35 | import sys |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 36 | |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 37 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 38 | # Determines path to gtest_test_utils and imports it. |
| 39 | SCRIPT_DIR = os.path.dirname(__file__) or '.' |
| 40 | |
| 41 | # isdir resolves symbolic links. |
Gennadiy Civil | 6c0c389 | 2018-01-26 16:30:57 -0500 | [diff] [blame^] | 42 | gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../googletest/test') |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 43 | if os.path.isdir(gtest_tests_util_dir): |
| 44 | GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir |
| 45 | else: |
Gennadiy Civil | 6c0c389 | 2018-01-26 16:30:57 -0500 | [diff] [blame^] | 46 | GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../googletest/test') |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 47 | |
| 48 | sys.path.append(GTEST_TESTS_UTIL_DIR) |
| 49 | import gtest_test_utils # pylint: disable-msg=C6204 |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 50 | |
| 51 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 52 | def GetSourceDir(): |
| 53 | """Returns the absolute path of the directory where the .py files are.""" |
| 54 | |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame] | 55 | return gtest_test_utils.GetSourceDir() |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 56 | |
| 57 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 58 | def 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 | |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame] | 71 | return gtest_test_utils.GetTestExecutablePath(executable_name) |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 72 | |
| 73 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | def 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.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 94 | # Suppresses the "Invalid const name" lint complaint |
| 95 | # pylint: disable-msg=C6409 |
| 96 | |
zhanyong.wan | 45fef50 | 2013-09-06 22:52:14 +0000 | [diff] [blame] | 97 | # Exposes utilities from gtest_test_utils. |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 98 | Subprocess = gtest_test_utils.Subprocess |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 99 | TestCase = gtest_test_utils.TestCase |
zhanyong.wan | 45fef50 | 2013-09-06 22:52:14 +0000 | [diff] [blame] | 100 | environ = gtest_test_utils.environ |
| 101 | SetEnvVar = gtest_test_utils.SetEnvVar |
| 102 | PREMATURE_EXIT_FILE_ENV_VAR = gtest_test_utils.PREMATURE_EXIT_FILE_ENV_VAR |
zhanyong.wan | f6d6a22 | 2009-12-01 19:42:25 +0000 | [diff] [blame] | 103 | |
| 104 | # pylint: enable-msg=C6409 |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 105 | |
| 106 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 107 | def Main(): |
| 108 | """Runs the unit test.""" |
| 109 | |
zhanyong.wan | 19eb9e9 | 2009-11-24 20:23:18 +0000 | [diff] [blame] | 110 | gtest_test_utils.Main() |