30 """Unit test utilities for Google C++ Testing and Mocking Framework."""
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
48 GTEST_OUTPUT_VAR_NAME =
'GTEST_OUTPUT'
51 PREMATURE_EXIT_FILE_ENV_VAR =
'TEST_PREMATURE_EXIT_FILE'
53 environ = os.environ.copy()
57 """Sets/unsets an environment variable to a given value."""
60 environ[env_var] = value
61 elif env_var
in environ:
68 TestCase = _test_module.TestCase
72 _flag_map = {
'source_dir': os.path.dirname(sys.argv[0]),
73 'build_dir': os.path.dirname(sys.argv[0])}
74 _gtest_flags_are_parsed =
False
78 """Parses and strips Google Test flags from argv. This is idempotent."""
82 global _gtest_flags_are_parsed
83 if _gtest_flags_are_parsed:
86 _gtest_flags_are_parsed =
True
87 for flag
in _flag_map:
89 if flag.upper()
in os.environ:
90 _flag_map[flag] = os.environ[flag.upper()]
95 prefix =
'--' + flag +
'='
96 if argv[i].startswith(prefix):
97 _flag_map[flag] = argv[i][
len(prefix):]
107 """Returns the value of the given flag."""
114 return _flag_map[flag]
118 """Returns the absolute path of the directory where the .py files are."""
120 return os.path.abspath(
GetFlag(
'source_dir'))
124 """Returns the absolute path of the directory where the test binaries are."""
126 return os.path.abspath(
GetFlag(
'build_dir'))
133 shutil.rmtree(_temp_dir, ignore_errors=
True)
135 atexit.register(_RemoveTempDir)
141 _temp_dir = tempfile.mkdtemp()
146 """Returns the absolute path of the test binary given its name.
148 The function will print a message and abort the program if the resulting file
152 executable_name: name of the test binary that the test script runs.
153 build_dir: directory where to look for executables, by default
154 the result of GetBuildDir().
157 The absolute path of the test binary.
160 path = os.path.abspath(os.path.join(build_dir
or GetBuildDir(),
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)
170 print(message, file=sys.stderr)
177 """Returns the argument to exit(), or -1 if exit() wasn't called.
180 exit_code: the result value of os.system(command).
190 if os.WIFEXITED(exit_code):
191 return os.WEXITSTATUS(exit_code)
197 def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
198 """Changes into a specified directory, if provided, and executes a command.
200 Restores the old directory afterwards.
203 command: The command to run, in the form of sys.argv.
204 working_dir: The directory to change into.
205 capture_stderr: Determines whether to capture stderr in the output member
207 env: Dictionary with environment to pass to the subprocess.
210 An object that represents outcome of the executed process. It has the
211 following attributes:
212 terminated_by_signal True if and only if the child process has been
213 terminated by a signal.
214 exited True if and only if the child process exited
216 exit_code The code with which the child process exited.
217 output Child process's stdout and stderr output
218 combined in a string.
222 stderr = subprocess.STDOUT
224 stderr = subprocess.PIPE
226 p = subprocess.Popen(command,
227 stdout=subprocess.PIPE, stderr=stderr,
228 cwd=working_dir, universal_newlines=
True, env=env)
231 self.
output = p.communicate()[0]
244 """Runs the unit test."""
252 if GTEST_OUTPUT_VAR_NAME
in os.environ:
253 del os.environ[GTEST_OUTPUT_VAR_NAME]