32 """Unit test utilities for Google C++ Testing Framework.""" 34 __author__ =
'wan@google.com (Zhanyong Wan)' 42 _test_module = unittest
48 _SUBPROCESS_MODULE_AVAILABLE =
True 51 _SUBPROCESS_MODULE_AVAILABLE =
False 54 GTEST_OUTPUT_VAR_NAME =
'GTEST_OUTPUT' 56 IS_WINDOWS = os.name ==
'nt' 57 IS_CYGWIN = os.name ==
'posix' and 'CYGWIN' in os.uname()[0]
60 PREMATURE_EXIT_FILE_ENV_VAR =
'TEST_PREMATURE_EXIT_FILE' 62 environ = os.environ.copy()
66 """Sets/unsets an environment variable to a given value.""" 69 environ[env_var] = value
70 elif env_var
in environ:
77 TestCase = _test_module.TestCase
81 _flag_map = {
'source_dir': os.path.dirname(sys.argv[0]),
82 'build_dir': os.path.dirname(sys.argv[0])}
83 _gtest_flags_are_parsed =
False 87 """Parses and strips Google Test flags from argv. This is idempotent.""" 91 global _gtest_flags_are_parsed
92 if _gtest_flags_are_parsed:
95 _gtest_flags_are_parsed =
True 96 for flag
in _flag_map:
98 if flag.upper()
in os.environ:
99 _flag_map[flag] = os.environ[flag.upper()]
104 prefix =
'--' + flag +
'=' 105 if argv[i].startswith(prefix):
106 _flag_map[flag] = argv[i][len(prefix):]
116 """Returns the value of the given flag.""" 123 return _flag_map[flag]
127 """Returns the absolute path of the directory where the .py files are.""" 129 return os.path.abspath(
GetFlag(
'source_dir'))
133 """Returns the absolute path of the directory where the test binaries are.""" 135 return os.path.abspath(
GetFlag(
'build_dir'))
142 shutil.rmtree(_temp_dir, ignore_errors=
True)
144 atexit.register(_RemoveTempDir)
148 """Returns a directory for temporary files.""" 152 _temp_dir = tempfile.mkdtemp()
157 """Returns the absolute path of the test binary given its name. 159 The function will print a message and abort the program if the resulting file 163 executable_name: name of the test binary that the test script runs. 164 build_dir: directory where to look for executables, by default 165 the result of GetBuildDir(). 168 The absolute path of the test binary. 171 path = os.path.abspath(os.path.join(build_dir
or GetBuildDir(),
173 if (IS_WINDOWS
or IS_CYGWIN)
and not path.endswith(
'.exe'):
176 if not os.path.exists(path):
178 'Unable to find the test binary. Please make sure to provide path\n' 179 'to the binary via the --build_dir flag or the BUILD_DIR\n' 180 'environment variable.')
181 print(message, file=sys.stderr)
188 """Returns the argument to exit(), or -1 if exit() wasn't called. 191 exit_code: the result value of os.system(command). 201 if os.WIFEXITED(exit_code):
202 return os.WEXITSTATUS(exit_code)
208 def __init__(self, command, working_dir=None, capture_stderr=True, env=None):
209 """Changes into a specified directory, if provided, and executes a command. 211 Restores the old directory afterwards. 214 command: The command to run, in the form of sys.argv. 215 working_dir: The directory to change into. 216 capture_stderr: Determines whether to capture stderr in the output member 218 env: Dictionary with environment to pass to the subprocess. 221 An object that represents outcome of the executed process. It has the 222 following attributes: 223 terminated_by_signal True iff the child process has been terminated 225 signal Sygnal that terminated the child process. 226 exited True iff the child process exited normally. 227 exit_code The code with which the child process exited. 228 output Child process's stdout and stderr output 229 combined in a string. 239 if _SUBPROCESS_MODULE_AVAILABLE:
241 stderr = subprocess.STDOUT
243 stderr = subprocess.PIPE
245 p = subprocess.Popen(command,
246 stdout=subprocess.PIPE, stderr=stderr,
247 cwd=working_dir, universal_newlines=
True, env=env)
253 old_dir = os.getcwd()
255 def _ReplaceEnvDict(dest, src):
259 for key
in list(dest.keys()):
268 old_environ = os.environ.copy()
269 _ReplaceEnvDict(os.environ, env)
272 if working_dir
is not None:
273 os.chdir(working_dir)
275 p = popen2.Popen4(command)
277 p = popen2.Popen3(command)
279 self.
output = p.fromchild.read()
287 _ReplaceEnvDict(os.environ, old_environ)
291 if os.WIFSIGNALED(ret_code):
307 """Runs the unit test.""" 317 if GTEST_OUTPUT_VAR_NAME
in os.environ:
318 del os.environ[GTEST_OUTPUT_VAR_NAME]
def _ParseAndStripGTestFlags(argv)
def GetTestExecutablePath(executable_name, build_dir=None)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
def __init__(self, command, working_dir=None, capture_stderr=True, env=None)
def GetExitStatus(exit_code)
def SetEnvVar(env_var, value)