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 |
| 38 | import unittest |
| 39 | |
| 40 | |
| 41 | # Initially maps a flag to its default value. After |
| 42 | # _ParseAndStripGMockFlags() is called, maps a flag to its actual |
| 43 | # value. |
| 44 | _flag_map = {'gmock_source_dir': os.path.dirname(sys.argv[0]), |
| 45 | 'gmock_build_dir': os.path.dirname(sys.argv[0])} |
| 46 | _gmock_flags_are_parsed = False |
| 47 | |
| 48 | |
| 49 | def _ParseAndStripGMockFlags(argv): |
| 50 | """Parses and strips Google Test flags from argv. This is idempotent.""" |
| 51 | |
| 52 | global _gmock_flags_are_parsed |
| 53 | if _gmock_flags_are_parsed: |
| 54 | return |
| 55 | |
| 56 | _gmock_flags_are_parsed = True |
| 57 | for flag in _flag_map: |
| 58 | # The environment variable overrides the default value. |
| 59 | if flag.upper() in os.environ: |
| 60 | _flag_map[flag] = os.environ[flag.upper()] |
| 61 | |
| 62 | # The command line flag overrides the environment variable. |
| 63 | i = 1 # Skips the program name. |
| 64 | while i < len(argv): |
| 65 | prefix = '--' + flag + '=' |
| 66 | if argv[i].startswith(prefix): |
| 67 | _flag_map[flag] = argv[i][len(prefix):] |
| 68 | del argv[i] |
| 69 | break |
| 70 | else: |
| 71 | # We don't increment i in case we just found a --gmock_* flag |
| 72 | # and removed it from argv. |
| 73 | i += 1 |
| 74 | |
| 75 | |
| 76 | def GetFlag(flag): |
| 77 | """Returns the value of the given flag.""" |
| 78 | |
| 79 | # In case GetFlag() is called before Main(), we always call |
| 80 | # _ParseAndStripGMockFlags() here to make sure the --gmock_* flags |
| 81 | # are parsed. |
| 82 | _ParseAndStripGMockFlags(sys.argv) |
| 83 | |
| 84 | return _flag_map[flag] |
| 85 | |
| 86 | |
| 87 | def GetSourceDir(): |
| 88 | """Returns the absolute path of the directory where the .py files are.""" |
| 89 | |
| 90 | return os.path.abspath(GetFlag('gmock_source_dir')) |
| 91 | |
| 92 | |
| 93 | def GetBuildDir(): |
| 94 | """Returns the absolute path of the directory where the test binaries are.""" |
| 95 | |
| 96 | return os.path.abspath(GetFlag('gmock_build_dir')) |
| 97 | |
| 98 | |
| 99 | def GetExitStatus(exit_code): |
| 100 | """Returns the argument to exit(), or -1 if exit() wasn't called. |
| 101 | |
| 102 | Args: |
| 103 | exit_code: the result value of os.system(command). |
| 104 | """ |
| 105 | |
| 106 | if os.name == 'nt': |
| 107 | # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns |
| 108 | # the argument to exit() directly. |
| 109 | return exit_code |
| 110 | else: |
| 111 | # On Unix, os.WEXITSTATUS() must be used to extract the exit status |
| 112 | # from the result of os.system(). |
| 113 | if os.WIFEXITED(exit_code): |
| 114 | return os.WEXITSTATUS(exit_code) |
| 115 | else: |
| 116 | return -1 |
| 117 | |
| 118 | |
| 119 | def Main(): |
| 120 | """Runs the unit test.""" |
| 121 | |
| 122 | # We must call _ParseAndStripGMockFlags() before calling |
| 123 | # unittest.main(). Otherwise the latter will be confused by the |
| 124 | # --gmock_* flags. |
| 125 | _ParseAndStripGMockFlags(sys.argv) |
| 126 | unittest.main() |