michael@0: #!/usr/bin/env python michael@0: # michael@0: # Copyright 2006, Google Inc. michael@0: # All rights reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: """Unit test utilities for Google C++ Testing Framework.""" michael@0: michael@0: __author__ = 'wan@google.com (Zhanyong Wan)' michael@0: michael@0: import atexit michael@0: import os michael@0: import shutil michael@0: import sys michael@0: import tempfile michael@0: import unittest michael@0: _test_module = unittest michael@0: michael@0: # Suppresses the 'Import not at the top of the file' lint complaint. michael@0: # pylint: disable-msg=C6204 michael@0: try: michael@0: import subprocess michael@0: _SUBPROCESS_MODULE_AVAILABLE = True michael@0: except: michael@0: import popen2 michael@0: _SUBPROCESS_MODULE_AVAILABLE = False michael@0: # pylint: enable-msg=C6204 michael@0: michael@0: GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT' michael@0: michael@0: IS_WINDOWS = os.name == 'nt' michael@0: IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0] michael@0: michael@0: # Here we expose a class from a particular module, depending on the michael@0: # environment. The comment suppresses the 'Invalid variable name' lint michael@0: # complaint. michael@0: TestCase = _test_module.TestCase # pylint: disable-msg=C6409 michael@0: michael@0: # Initially maps a flag to its default value. After michael@0: # _ParseAndStripGTestFlags() is called, maps a flag to its actual value. michael@0: _flag_map = {'source_dir': os.path.dirname(sys.argv[0]), michael@0: 'build_dir': os.path.dirname(sys.argv[0])} michael@0: _gtest_flags_are_parsed = False michael@0: michael@0: michael@0: def _ParseAndStripGTestFlags(argv): michael@0: """Parses and strips Google Test flags from argv. This is idempotent.""" michael@0: michael@0: # Suppresses the lint complaint about a global variable since we need it michael@0: # here to maintain module-wide state. michael@0: global _gtest_flags_are_parsed # pylint: disable-msg=W0603 michael@0: if _gtest_flags_are_parsed: michael@0: return michael@0: michael@0: _gtest_flags_are_parsed = True michael@0: for flag in _flag_map: michael@0: # The environment variable overrides the default value. michael@0: if flag.upper() in os.environ: michael@0: _flag_map[flag] = os.environ[flag.upper()] michael@0: michael@0: # The command line flag overrides the environment variable. michael@0: i = 1 # Skips the program name. michael@0: while i < len(argv): michael@0: prefix = '--' + flag + '=' michael@0: if argv[i].startswith(prefix): michael@0: _flag_map[flag] = argv[i][len(prefix):] michael@0: del argv[i] michael@0: break michael@0: else: michael@0: # We don't increment i in case we just found a --gtest_* flag michael@0: # and removed it from argv. michael@0: i += 1 michael@0: michael@0: michael@0: def GetFlag(flag): michael@0: """Returns the value of the given flag.""" michael@0: michael@0: # In case GetFlag() is called before Main(), we always call michael@0: # _ParseAndStripGTestFlags() here to make sure the --gtest_* flags michael@0: # are parsed. michael@0: _ParseAndStripGTestFlags(sys.argv) michael@0: michael@0: return _flag_map[flag] michael@0: michael@0: michael@0: def GetSourceDir(): michael@0: """Returns the absolute path of the directory where the .py files are.""" michael@0: michael@0: return os.path.abspath(GetFlag('source_dir')) michael@0: michael@0: michael@0: def GetBuildDir(): michael@0: """Returns the absolute path of the directory where the test binaries are.""" michael@0: michael@0: return os.path.abspath(GetFlag('build_dir')) michael@0: michael@0: michael@0: _temp_dir = None michael@0: michael@0: def _RemoveTempDir(): michael@0: if _temp_dir: michael@0: shutil.rmtree(_temp_dir, ignore_errors=True) michael@0: michael@0: atexit.register(_RemoveTempDir) michael@0: michael@0: michael@0: def GetTempDir(): michael@0: """Returns a directory for temporary files.""" michael@0: michael@0: global _temp_dir michael@0: if not _temp_dir: michael@0: _temp_dir = tempfile.mkdtemp() michael@0: return _temp_dir michael@0: michael@0: michael@0: def GetTestExecutablePath(executable_name, build_dir=None): michael@0: """Returns the absolute path of the test binary given its name. michael@0: michael@0: The function will print a message and abort the program if the resulting file michael@0: doesn't exist. michael@0: michael@0: Args: michael@0: executable_name: name of the test binary that the test script runs. michael@0: build_dir: directory where to look for executables, by default michael@0: the result of GetBuildDir(). michael@0: michael@0: Returns: michael@0: The absolute path of the test binary. michael@0: """ michael@0: michael@0: path = os.path.abspath(os.path.join(build_dir or GetBuildDir(), michael@0: executable_name)) michael@0: if (IS_WINDOWS or IS_CYGWIN) and not path.endswith('.exe'): michael@0: path += '.exe' michael@0: michael@0: if not os.path.exists(path): michael@0: message = ( michael@0: 'Unable to find the test binary. Please make sure to provide path\n' michael@0: 'to the binary via the --build_dir flag or the BUILD_DIR\n' michael@0: 'environment variable.') michael@0: print >> sys.stderr, message michael@0: sys.exit(1) michael@0: michael@0: return path michael@0: michael@0: michael@0: def GetExitStatus(exit_code): michael@0: """Returns the argument to exit(), or -1 if exit() wasn't called. michael@0: michael@0: Args: michael@0: exit_code: the result value of os.system(command). michael@0: """ michael@0: michael@0: if os.name == 'nt': michael@0: # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns michael@0: # the argument to exit() directly. michael@0: return exit_code michael@0: else: michael@0: # On Unix, os.WEXITSTATUS() must be used to extract the exit status michael@0: # from the result of os.system(). michael@0: if os.WIFEXITED(exit_code): michael@0: return os.WEXITSTATUS(exit_code) michael@0: else: michael@0: return -1 michael@0: michael@0: michael@0: class Subprocess: michael@0: def __init__(self, command, working_dir=None, capture_stderr=True, env=None): michael@0: """Changes into a specified directory, if provided, and executes a command. michael@0: michael@0: Restores the old directory afterwards. michael@0: michael@0: Args: michael@0: command: The command to run, in the form of sys.argv. michael@0: working_dir: The directory to change into. michael@0: capture_stderr: Determines whether to capture stderr in the output member michael@0: or to discard it. michael@0: env: Dictionary with environment to pass to the subprocess. michael@0: michael@0: Returns: michael@0: An object that represents outcome of the executed process. It has the michael@0: following attributes: michael@0: terminated_by_signal True iff the child process has been terminated michael@0: by a signal. michael@0: signal Sygnal that terminated the child process. michael@0: exited True iff the child process exited normally. michael@0: exit_code The code with which the child process exited. michael@0: output Child process's stdout and stderr output michael@0: combined in a string. michael@0: """ michael@0: michael@0: # The subprocess module is the preferrable way of running programs michael@0: # since it is available and behaves consistently on all platforms, michael@0: # including Windows. But it is only available starting in python 2.4. michael@0: # In earlier python versions, we revert to the popen2 module, which is michael@0: # available in python 2.0 and later but doesn't provide required michael@0: # functionality (Popen4) under Windows. This allows us to support Mac michael@0: # OS X 10.4 Tiger, which has python 2.3 installed. michael@0: if _SUBPROCESS_MODULE_AVAILABLE: michael@0: if capture_stderr: michael@0: stderr = subprocess.STDOUT michael@0: else: michael@0: stderr = subprocess.PIPE michael@0: michael@0: p = subprocess.Popen(command, michael@0: stdout=subprocess.PIPE, stderr=stderr, michael@0: cwd=working_dir, universal_newlines=True, env=env) michael@0: # communicate returns a tuple with the file obect for the child's michael@0: # output. michael@0: self.output = p.communicate()[0] michael@0: self._return_code = p.returncode michael@0: else: michael@0: old_dir = os.getcwd() michael@0: michael@0: def _ReplaceEnvDict(dest, src): michael@0: # Changes made by os.environ.clear are not inheritable by child michael@0: # processes until Python 2.6. To produce inheritable changes we have michael@0: # to delete environment items with the del statement. michael@0: for key in dest.keys(): michael@0: del dest[key] michael@0: dest.update(src) michael@0: michael@0: # When 'env' is not None, backup the environment variables and replace michael@0: # them with the passed 'env'. When 'env' is None, we simply use the michael@0: # current 'os.environ' for compatibility with the subprocess.Popen michael@0: # semantics used above. michael@0: if env is not None: michael@0: old_environ = os.environ.copy() michael@0: _ReplaceEnvDict(os.environ, env) michael@0: michael@0: try: michael@0: if working_dir is not None: michael@0: os.chdir(working_dir) michael@0: if capture_stderr: michael@0: p = popen2.Popen4(command) michael@0: else: michael@0: p = popen2.Popen3(command) michael@0: p.tochild.close() michael@0: self.output = p.fromchild.read() michael@0: ret_code = p.wait() michael@0: finally: michael@0: os.chdir(old_dir) michael@0: michael@0: # Restore the old environment variables michael@0: # if they were replaced. michael@0: if env is not None: michael@0: _ReplaceEnvDict(os.environ, old_environ) michael@0: michael@0: # Converts ret_code to match the semantics of michael@0: # subprocess.Popen.returncode. michael@0: if os.WIFSIGNALED(ret_code): michael@0: self._return_code = -os.WTERMSIG(ret_code) michael@0: else: # os.WIFEXITED(ret_code) should return True here. michael@0: self._return_code = os.WEXITSTATUS(ret_code) michael@0: michael@0: if self._return_code < 0: michael@0: self.terminated_by_signal = True michael@0: self.exited = False michael@0: self.signal = -self._return_code michael@0: else: michael@0: self.terminated_by_signal = False michael@0: self.exited = True michael@0: self.exit_code = self._return_code michael@0: michael@0: michael@0: def Main(): michael@0: """Runs the unit test.""" michael@0: michael@0: # We must call _ParseAndStripGTestFlags() before calling michael@0: # unittest.main(). Otherwise the latter will be confused by the michael@0: # --gtest_* flags. michael@0: _ParseAndStripGTestFlags(sys.argv) michael@0: # The tested binaries should not be writing XML output files unless the michael@0: # script explicitly instructs them to. michael@0: # TODO(vladl@google.com): Move this into Subprocess when we implement michael@0: # passing environment into it as a parameter. michael@0: if GTEST_OUTPUT_VAR_NAME in os.environ: michael@0: del os.environ[GTEST_OUTPUT_VAR_NAME] michael@0: michael@0: _test_module.main()