30 """Unit test utilities for Google C++ Testing and Mocking Framework."""
37 IS_WINDOWS = os.name ==
'nt'
38 IS_CYGWIN = os.name ==
'posix' and 'CYGWIN' in os.uname()[0]
39 IS_OS2 = os.name ==
'os2'
44 import unittest
as _test_module
48 _SUBPROCESS_MODULE_AVAILABLE =
True
51 _SUBPROCESS_MODULE_AVAILABLE =
False
54 GTEST_OUTPUT_VAR_NAME =
'GTEST_OUTPUT'
57 PREMATURE_EXIT_FILE_ENV_VAR =
'TEST_PREMATURE_EXIT_FILE'
59 environ = os.environ.copy()
63 """Sets/unsets an environment variable to a given value."""
66 environ[env_var] = value
67 elif env_var
in environ:
74 TestCase = _test_module.TestCase
78 _flag_map = {
'source_dir': os.path.dirname(sys.argv[0]),
79 'build_dir': os.path.dirname(sys.argv[0])}
80 _gtest_flags_are_parsed =
False
84 """Parses and strips Google Test flags from argv. This is idempotent."""
88 global _gtest_flags_are_parsed
89 if _gtest_flags_are_parsed:
92 _gtest_flags_are_parsed =
True
93 for flag
in _flag_map:
95 if flag.upper()
in os.environ:
96 _flag_map[flag] = os.environ[flag.upper()]
101 prefix =
'--' + flag +
'='
102 if argv[i].startswith(prefix):
103 _flag_map[flag] = argv[i][
len(prefix):]
113 """Returns the value of the given flag."""
120 return _flag_map[flag]
124 """Returns the absolute path of the directory where the .py files are."""
126 return os.path.abspath(
GetFlag(
'source_dir'))
130 """Returns the absolute path of the directory where the test binaries are."""
132 return os.path.abspath(
GetFlag(
'build_dir'))
139 shutil.rmtree(_temp_dir, ignore_errors=
True)
141 atexit.register(_RemoveTempDir)
147 _temp_dir = tempfile.mkdtemp()
152 """Returns the absolute path of the test binary given its name.
154 The function will print a message and abort the program if the resulting file
158 executable_name: name of the test binary that the test script runs.
159 build_dir: directory where to look for executables, by default
160 the result of GetBuildDir().
163 The absolute path of the test binary.
166 path = os.path.abspath(os.path.join(build_dir
or GetBuildDir(),
168 if (IS_WINDOWS
or IS_CYGWIN
or IS_OS2)
and not path.endswith(
'.exe'):
171 if not os.path.exists(path):
173 'Unable to find the test binary "%s". Please make sure to provide\n'
174 'a path to the binary via the --build_dir flag or the BUILD_DIR\n'
175 'environment variable.' % path)
176 print >> sys.stderr, message
183 """Returns the argument to exit(), or -1 if exit() wasn't called.
186 exit_code: the result value of os.system(command).
196 if os.WIFEXITED(exit_code):
197 return os.WEXITSTATUS(exit_code)
203 def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
204 """Changes into a specified directory, if provided, and executes a command.
206 Restores the old directory afterwards.
209 command: The command to run, in the form of sys.argv.
210 working_dir: The directory to change into.
211 capture_stderr: Determines whether to capture stderr in the output member
213 env: Dictionary with environment to pass to the subprocess.
216 An object that represents outcome of the executed process. It has the
217 following attributes:
218 terminated_by_signal True iff the child process has been terminated
220 signal Sygnal that terminated the child process.
221 exited True iff the child process exited normally.
222 exit_code The code with which the child process exited.
223 output Child process's stdout and stderr output
224 combined in a string.
234 if _SUBPROCESS_MODULE_AVAILABLE:
236 stderr = subprocess.STDOUT
238 stderr = subprocess.PIPE
240 p = subprocess.Popen(command,
241 stdout=subprocess.PIPE, stderr=stderr,
242 cwd=working_dir, universal_newlines=
True, env=env)
248 old_dir = os.getcwd()
250 def _ReplaceEnvDict(dest, src):
254 for key
in dest.keys():
263 old_environ = os.environ.copy()
264 _ReplaceEnvDict(os.environ, env)
267 if working_dir
is not None:
268 os.chdir(working_dir)
270 p = popen2.Popen4(command)
272 p = popen2.Popen3(command)
274 self.
output = p.fromchild.read()
282 _ReplaceEnvDict(os.environ, old_environ)
286 if os.WIFSIGNALED(ret_code):
302 """Runs the unit test."""
310 if GTEST_OUTPUT_VAR_NAME
in os.environ:
311 del os.environ[GTEST_OUTPUT_VAR_NAME]