1 # Copyright 2006, Google Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
30 """Unit test utilities for Google C++ Testing and Mocking Framework."""
31 # Suppresses the 'Import not at the top of the file' lint complaint.
32 # pylint: disable=g-import-not-at-top
38 IS_WINDOWS = os.name == 'nt'
39 IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0]
40 IS_OS2 = os.name == 'os2'
45 import unittest as _test_module
46 # pylint: enable=g-import-not-at-top
48 GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT'
50 # The environment variable for specifying the path to the premature-exit file.
51 PREMATURE_EXIT_FILE_ENV_VAR = 'TEST_PREMATURE_EXIT_FILE'
53 environ = os.environ.copy()
56 def SetEnvVar(env_var, value):
57 """Sets/unsets an environment variable to a given value."""
60 environ[env_var] = value
61 elif env_var in environ:
65 # Here we expose a class from a particular module, depending on the
67 TestCase = _test_module.TestCase
69 # Initially maps a flag to its default value. After
70 # _ParseAndStripGTestFlags() is called, maps a flag to its actual value.
72 'source_dir': os.path.dirname(sys.argv[0]),
73 'build_dir': os.path.dirname(sys.argv[0]),
75 _gtest_flags_are_parsed = False
78 def _ParseAndStripGTestFlags(argv):
79 """Parses and strips Google Test flags from argv. This is idempotent."""
81 global _gtest_flags_are_parsed
82 if _gtest_flags_are_parsed:
85 _gtest_flags_are_parsed = True
86 for flag in _flag_map:
87 # The environment variable overrides the default value.
88 if flag.upper() in os.environ:
89 _flag_map[flag] = os.environ[flag.upper()]
91 # The command line flag overrides the environment variable.
92 i = 1 # Skips the program name.
94 prefix = '--' + flag + '='
95 if argv[i].startswith(prefix):
96 _flag_map[flag] = argv[i][len(prefix) :]
100 # We don't increment i in case we just found a --gtest_* flag
101 # and removed it from argv.
106 """Returns the value of the given flag."""
108 # In case GetFlag() is called before Main(), we always call
109 # _ParseAndStripGTestFlags() here to make sure the --gtest_* flags
111 _ParseAndStripGTestFlags(sys.argv)
113 return _flag_map[flag]
117 """Returns the absolute path of the directory where the .py files are."""
119 return os.path.abspath(GetFlag('source_dir'))
123 """Returns the absolute path of the directory where the test binaries are."""
125 return os.path.abspath(GetFlag('build_dir'))
130 def _RemoveTempDir():
132 shutil.rmtree(_temp_dir, ignore_errors=True)
134 atexit.register(_RemoveTempDir)
140 _temp_dir = tempfile.mkdtemp()
144 def GetTestExecutablePath(executable_name, build_dir=None):
145 """Returns the absolute path of the test binary given its name.
147 The function will print a message and abort the program if the resulting file
151 executable_name: name of the test binary that the test script runs.
152 build_dir: directory where to look for executables, by default the
153 result of GetBuildDir().
156 The absolute path of the test binary.
159 path = os.path.abspath(
160 os.path.join(build_dir or GetBuildDir(), executable_name)
162 if (IS_WINDOWS or IS_CYGWIN or IS_OS2) and not path.endswith('.exe'):
165 if not os.path.exists(path):
167 'Unable to find the test binary "%s". Please make sure to provide\n'
168 'a path to the binary via the --build_dir flag or the BUILD_DIR\n'
169 'environment variable.' % path
171 print(message, file=sys.stderr)
177 def GetExitStatus(exit_code):
178 """Returns the argument to exit(), or -1 if exit() wasn't called.
181 exit_code: the result value of os.system(command).
185 # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns
186 # the argument to exit() directly.
189 # On Unix, os.WEXITSTATUS() must be used to extract the exit status
190 # from the result of os.system().
191 if os.WIFEXITED(exit_code):
192 return os.WEXITSTATUS(exit_code)
199 def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
200 """Changes into a specified directory, if provided, and executes a command.
202 Restores the old directory afterwards.
205 command: The command to run, in the form of sys.argv.
206 working_dir: The directory to change into.
207 capture_stderr: Determines whether to capture stderr in the output member
209 env: Dictionary with environment to pass to the subprocess.
212 An object that represents outcome of the executed process. It has the
213 following attributes:
214 terminated_by_signal True if and only if the child process has been
215 terminated by a signal.
216 exited True if and only if the child process exited
218 exit_code The code with which the child process exited.
219 output Child process's stdout and stderr output
220 combined in a string.
224 stderr = subprocess.STDOUT
226 stderr = subprocess.PIPE
228 p = subprocess.Popen(
230 stdout=subprocess.PIPE,
233 universal_newlines=True,
236 # communicate returns a tuple with the file object for the child's
238 self.output = p.communicate()[0]
239 self._return_code = p.returncode
241 if bool(self._return_code & 0x80000000):
242 self.terminated_by_signal = True
245 self.terminated_by_signal = False
247 self.exit_code = self._return_code
251 """Runs the unit test."""
253 # We must call _ParseAndStripGTestFlags() before calling
254 # unittest.main(). Otherwise the latter will be confused by the
256 _ParseAndStripGTestFlags(sys.argv)
257 # The tested binaries should not be writing XML output files unless the
258 # script explicitly instructs them to.
259 if GTEST_OUTPUT_VAR_NAME in os.environ:
260 del os.environ[GTEST_OUTPUT_VAR_NAME]