From d201456903f3ecae1f7794edfab0d5678e642265 Mon Sep 17 00:00:00 2001 From: shiqian Date: Thu, 3 Jul 2008 22:38:12 +0000 Subject: Initial import. --- test/gtest_test_utils.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100755 test/gtest_test_utils.py (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py new file mode 100755 index 00000000..6c158871 --- /dev/null +++ b/test/gtest_test_utils.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# +# Copyright 2006, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +"""Unit test utilities for Google C++ Testing Framework.""" + +__author__ = 'wan@google.com (Zhanyong Wan)' + +import os +import sys +import unittest + + +# Initially maps a flag to its default value. After +# _ParseAndStripGTestFlags() is called, maps a flag to its actual +# value. +_flag_map = {'gtest_source_dir': os.path.dirname(sys.argv[0]), + 'gtest_build_dir': os.path.dirname(sys.argv[0])} +_gtest_flags_are_parsed = False + + +def _ParseAndStripGTestFlags(argv): + """Parses and strips Google Test flags from argv. This is idempotent.""" + + global _gtest_flags_are_parsed + if _gtest_flags_are_parsed: + return + + _gtest_flags_are_parsed = True + for flag in _flag_map: + # The environment variable overrides the default value. + if flag.upper() in os.environ: + _flag_map[flag] = os.environ[flag.upper()] + + # The command line flag overrides the environment variable. + i = 1 # Skips the program name. + while i < len(argv): + prefix = '--' + flag + '=' + if argv[i].startswith(prefix): + _flag_map[flag] = argv[i][len(prefix):] + del argv[i] + break + else: + # We don't increment i in case we just found a --gtest_* flag + # and removed it from argv. + i += 1 + + +def GetFlag(flag): + """Returns the value of the given flag.""" + + # In case GetFlag() is called before Main(), we always call + # _ParseAndStripGTestFlags() here to make sure the --gtest_* flags + # are parsed. + _ParseAndStripGTestFlags(sys.argv) + + return _flag_map[flag] + + +def GetSourceDir(): + """Returns the absolute path of the directory where the .py files are.""" + + return os.path.abspath(GetFlag('gtest_source_dir')) + + +def GetBuildDir(): + """Returns the absolute path of the directory where the test binaries are.""" + + return os.path.abspath(GetFlag('gtest_build_dir')) + + +def Main(): + """Runs the unit test.""" + + # We must call _ParseAndStripGTestFlags() before calling + # unittest.main(). Otherwise the latter will be confused by the + # --gtest_* flags. + _ParseAndStripGTestFlags(sys.argv) + unittest.main() -- cgit v1.2.3 From e79c3ccb73d68543e8ad98d69179dee74abff7ab Mon Sep 17 00:00:00 2001 From: shiqian Date: Thu, 18 Sep 2008 21:18:11 +0000 Subject: Makes the Python tests more portable by calling standard functions to interpret the result of os.system(). This could fix the broken Python tests on some users' machines. --- test/gtest_test_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 6c158871..f454774d 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -96,6 +96,26 @@ def GetBuildDir(): return os.path.abspath(GetFlag('gtest_build_dir')) +def GetExitStatus(exit_code): + """Returns the argument to exit(), or -1 if exit() wasn't called. + + Args: + exit_code: the result value of os.system(command). + """ + + if os.name == 'nt': + # On Windows, os.WEXITSTATUS() doesn't work and os.system() returns + # the argument to exit() directly. + return exit_code + else: + # On Unix, os.WEXITSTATUS() must be used to extract the exit status + # from the result of os.system(). + if os.WIFEXITED(exit_code): + return os.WEXITSTATUS(exit_code) + else: + return -1 + + def Main(): """Runs the unit test.""" -- cgit v1.2.3 From 514265c415e072caf92fb4eed57aacdfea9964f1 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Sat, 22 Nov 2008 02:26:23 +0000 Subject: Fixed two of the failing tests mentioned in issue 9 --- test/gtest_test_utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index f454774d..a3f0138e 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -116,6 +116,31 @@ def GetExitStatus(exit_code): return -1 +def RunCommandSuppressOutput(command, working_dir=None): + """Changes into a specified directory, if provided, and executes a command. + Restores the old directory afterwards. + + Args: + command: A command to run. + working_dir: A directory to change into. + """ + + old_dir = None + try: + if working_dir is not None: + old_dir = os.getcwd() + os.chdir(working_dir) + f = os.popen(command, 'r') + f.read() + ret_code = f.close() + finally: + if old_dir is not None: + os.chdir(old_dir) + if ret_code is None: + ret_code = 0 + return ret_code + + def Main(): """Runs the unit test.""" -- cgit v1.2.3 From 95536ab53bba952d748f6c1535ba9a3b2ff7e294 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 26 Nov 2008 20:02:45 +0000 Subject: Fixed gtest_break_on_failure_unittest on Ubuntu 8.04 and Windows --- test/gtest_test_utils.py | 87 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 22 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index a3f0138e..8ee99c08 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -37,6 +37,13 @@ import os import sys import unittest +try: + import subprocess + _SUBPROCESS_MODULE_AVAILABLE = True +except: + import popen2 + _SUBPROCESS_MODULE_AVAILABLE = False + # Initially maps a flag to its default value. After # _ParseAndStripGTestFlags() is called, maps a flag to its actual @@ -116,29 +123,65 @@ def GetExitStatus(exit_code): return -1 -def RunCommandSuppressOutput(command, working_dir=None): - """Changes into a specified directory, if provided, and executes a command. - Restores the old directory afterwards. - - Args: - command: A command to run. - working_dir: A directory to change into. - """ - - old_dir = None - try: - if working_dir is not None: +class Subprocess: + def __init__(this, command, working_dir=None): + """Changes into a specified directory, if provided, and executes a command. + Restores the old directory afterwards. Execution results are returned + via the following attributes: + terminated_by_sygnal True iff the child process has been terminated + by a signal. + signal Sygnal that terminated the child process. + exited True iff the child process exited normally. + exit_code The code with which the child proces exited. + output Child process's stdout and stderr output + combined in a string. + + Args: + command: A command to run. + working_dir: A directory to change into. + """ + + # The subprocess module is the preferrable way of running programs + # since it is available and behaves consistently on all platforms, + # including Windows. But it is only available starting in python 2.4. + # In earlier python versions, we revert to the popen2 module, which is + # available in python 2.0 and later but doesn't provide required + # functionality (Popen4) under Windows. This allows us to support Mac + # OS X 10.4 Tiger, which has python 2.3 installed. + if _SUBPROCESS_MODULE_AVAILABLE: + p = subprocess.Popen(command, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + cwd=working_dir, universal_newlines=True) + # communicate returns a tuple with the file obect for the child's + # output. + this.output = p.communicate()[0] + this._return_code = p.returncode + else: old_dir = os.getcwd() - os.chdir(working_dir) - f = os.popen(command, 'r') - f.read() - ret_code = f.close() - finally: - if old_dir is not None: - os.chdir(old_dir) - if ret_code is None: - ret_code = 0 - return ret_code + try: + if working_dir is not None: + os.chdir(working_dir) + p = popen2.Popen4(command) + p.tochild.close() + this.output = p.fromchild.read() + ret_code = p.wait() + finally: + os.chdir(old_dir) + # Converts ret_code to match the semantics of + # subprocess.Popen.returncode. + if os.WIFSIGNALED(ret_code): + this._return_code = -os.WTERMSIG(ret_code) + else: # os.WIFEXITED(ret_code) should return True here. + this._return_code = os.WEXITSTATUS(ret_code) + + if this._return_code < 0: + this.terminated_by_signal = True + this.exited = False + this.signal = -this._return_code + else: + this.terminated_by_signal = False + this.exited = True + this.exit_code = this._return_code def Main(): -- cgit v1.2.3 From 87d23e45f096c91c9e722b20bf15b733dbab0f80 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 11 Mar 2009 22:18:52 +0000 Subject: Implements the --help flag; fixes tests on Windows. --- test/gtest_test_utils.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 8ee99c08..8f55b075 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -124,7 +124,7 @@ def GetExitStatus(exit_code): class Subprocess: - def __init__(this, command, working_dir=None): + def __init__(self, command, working_dir=None): """Changes into a specified directory, if provided, and executes a command. Restores the old directory afterwards. Execution results are returned via the following attributes: @@ -137,7 +137,7 @@ class Subprocess: combined in a string. Args: - command: A command to run. + command: A command to run, in the form of sys.argv. working_dir: A directory to change into. """ @@ -154,8 +154,8 @@ class Subprocess: cwd=working_dir, universal_newlines=True) # communicate returns a tuple with the file obect for the child's # output. - this.output = p.communicate()[0] - this._return_code = p.returncode + self.output = p.communicate()[0] + self._return_code = p.returncode else: old_dir = os.getcwd() try: @@ -163,25 +163,25 @@ class Subprocess: os.chdir(working_dir) p = popen2.Popen4(command) p.tochild.close() - this.output = p.fromchild.read() + self.output = p.fromchild.read() ret_code = p.wait() finally: os.chdir(old_dir) # Converts ret_code to match the semantics of # subprocess.Popen.returncode. if os.WIFSIGNALED(ret_code): - this._return_code = -os.WTERMSIG(ret_code) + self._return_code = -os.WTERMSIG(ret_code) else: # os.WIFEXITED(ret_code) should return True here. - this._return_code = os.WEXITSTATUS(ret_code) + self._return_code = os.WEXITSTATUS(ret_code) - if this._return_code < 0: - this.terminated_by_signal = True - this.exited = False - this.signal = -this._return_code + if self._return_code < 0: + self.terminated_by_signal = True + self.exited = False + self.signal = -self._return_code else: - this.terminated_by_signal = False - this.exited = True - this.exit_code = this._return_code + self.terminated_by_signal = False + self.exited = True + self.exit_code = self._return_code def Main(): -- cgit v1.2.3 From 7fa242a44b47ce74d7246440b14571f7a5dd1b17 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Thu, 9 Apr 2009 02:57:38 +0000 Subject: Makes the Python tests more stable (by Vlad Losev); fixes a memory leak in GetThreadCount() on Mac (by Vlad Losev); improves fuse_gtest_files.py to support fusing Google Mock files (by Zhanyong Wan). --- test/gtest_test_utils.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 8f55b075..7dc8c421 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -44,10 +44,10 @@ except: import popen2 _SUBPROCESS_MODULE_AVAILABLE = False +IS_WINDOWS = os.name == 'nt' -# Initially maps a flag to its default value. After -# _ParseAndStripGTestFlags() is called, maps a flag to its actual -# value. +# Initially maps a flag to its default value. After +# _ParseAndStripGTestFlags() is called, maps a flag to its actual value. _flag_map = {'gtest_source_dir': os.path.dirname(sys.argv[0]), 'gtest_build_dir': os.path.dirname(sys.argv[0])} _gtest_flags_are_parsed = False @@ -103,6 +103,38 @@ def GetBuildDir(): return os.path.abspath(GetFlag('gtest_build_dir')) +def GetTestExecutablePath(executable_name): + """Returns the absolute path of the test binary given its name. + + The function will print a message and abort the program if the resulting file + doesn't exist. + + Args: + executable_name: name of the test binary that the test script runs. + + Returns: + The absolute path of the test binary. + """ + + path = os.path.abspath(os.path.join(GetBuildDir(), executable_name)) + if IS_WINDOWS and not path.endswith('.exe'): + path += '.exe' + + if not os.path.exists(path): + message = ( + 'Unable to find the test binary. Please make sure to provide path\n' + 'to the binary via the --gtest_build_dir flag or the GTEST_BUILD_DIR\n' + 'environment variable. For convenient use, invoke this script via\n' + 'mk_test.py.\n' + # TODO(vladl@google.com): change mk_test.py to test.py after renaming + # the file. + 'Please run mk_test.py -h for help.') + print >> sys.stderr, message + sys.exit(1) + + return path + + def GetExitStatus(exit_code): """Returns the argument to exit(), or -1 if exit() wasn't called. -- cgit v1.2.3 From 532dc2de35f2cef191bc91c3587a9f8f4974756f Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 17 Jun 2009 21:06:27 +0000 Subject: Implements a subset of TR1 tuple needed by gtest and gmock (by Zhanyong Wan); cleaned up the Python tests (by Vlad Losev); made run_tests.py invokable from any directory (by Vlad Losev). --- test/gtest_test_utils.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 7dc8c421..45b25cd6 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -33,19 +33,32 @@ __author__ = 'wan@google.com (Zhanyong Wan)' +import atexit import os +import shutil import sys +import tempfile import unittest +_test_module = unittest +# Suppresses the 'Import not at the top of the file' lint complaint. +# pylint: disable-msg=C6204 try: import subprocess _SUBPROCESS_MODULE_AVAILABLE = True except: import popen2 _SUBPROCESS_MODULE_AVAILABLE = False +# pylint: enable-msg=C6204 + IS_WINDOWS = os.name == 'nt' +# Here we expose a class from a particular module, depending on the +# environment. The comment suppresses the 'Invalid variable name' lint +# complaint. +TestCase = _test_module.TestCase # pylint: disable-msg=C6409 + # Initially maps a flag to its default value. After # _ParseAndStripGTestFlags() is called, maps a flag to its actual value. _flag_map = {'gtest_source_dir': os.path.dirname(sys.argv[0]), @@ -56,7 +69,9 @@ _gtest_flags_are_parsed = False def _ParseAndStripGTestFlags(argv): """Parses and strips Google Test flags from argv. This is idempotent.""" - global _gtest_flags_are_parsed + # Suppresses the lint complaint about a global variable since we need it + # here to maintain module-wide state. + global _gtest_flags_are_parsed # pylint: disable-msg=W0603 if _gtest_flags_are_parsed: return @@ -103,6 +118,24 @@ def GetBuildDir(): return os.path.abspath(GetFlag('gtest_build_dir')) +_temp_dir = None + +def _RemoveTempDir(): + if _temp_dir: + shutil.rmtree(_temp_dir, ignore_errors=True) + +atexit.register(_RemoveTempDir) + + +def GetTempDir(): + """Returns a directory for temporary files.""" + + global _temp_dir + if not _temp_dir: + _temp_dir = tempfile.mkdtemp() + return _temp_dir + + def GetTestExecutablePath(executable_name): """Returns the absolute path of the test binary given its name. @@ -223,4 +256,4 @@ def Main(): # unittest.main(). Otherwise the latter will be confused by the # --gtest_* flags. _ParseAndStripGTestFlags(sys.argv) - unittest.main() + _test_module.main() -- cgit v1.2.3 From 046efb852bef27fe2a22dc632fdaeb909d5b0086 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 19 Jun 2009 21:23:56 +0000 Subject: Fixes the broken run_tests_test (by Vlad Losev). --- test/gtest_test_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 45b25cd6..5b28fe49 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -53,6 +53,7 @@ except: IS_WINDOWS = os.name == 'nt' +IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0] # Here we expose a class from a particular module, depending on the # environment. The comment suppresses the 'Invalid variable name' lint @@ -150,7 +151,7 @@ def GetTestExecutablePath(executable_name): """ path = os.path.abspath(os.path.join(GetBuildDir(), executable_name)) - if IS_WINDOWS and not path.endswith('.exe'): + if (IS_WINDOWS or IS_CYGWIN) and not path.endswith('.exe'): path += '.exe' if not os.path.exists(path): -- cgit v1.2.3 From b2db677c9905a34ca6454aa526f7a0cc5cfaeca1 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Wed, 1 Jul 2009 04:58:05 +0000 Subject: Reduces the flakiness of gtest-port_test on Mac; improves the Python tests; hides methods that we don't want to publish; makes win-dbg8 the default scons configuration (all by Vlad Losev). --- test/gtest_test_utils.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 5b28fe49..385662ad 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -190,7 +190,7 @@ def GetExitStatus(exit_code): class Subprocess: - def __init__(self, command, working_dir=None): + def __init__(self, command, working_dir=None, capture_stderr=True): """Changes into a specified directory, if provided, and executes a command. Restores the old directory afterwards. Execution results are returned via the following attributes: @@ -203,8 +203,10 @@ class Subprocess: combined in a string. Args: - command: A command to run, in the form of sys.argv. - working_dir: A directory to change into. + command: The command to run, in the form of sys.argv. + working_dir: The directory to change into. + capture_stderr: Determines whether to capture stderr in the output member + or to discard it. """ # The subprocess module is the preferrable way of running programs @@ -215,8 +217,13 @@ class Subprocess: # functionality (Popen4) under Windows. This allows us to support Mac # OS X 10.4 Tiger, which has python 2.3 installed. if _SUBPROCESS_MODULE_AVAILABLE: + if capture_stderr: + stderr = subprocess.STDOUT + else: + stderr = subprocess.PIPE + p = subprocess.Popen(command, - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, stderr=stderr, cwd=working_dir, universal_newlines=True) # communicate returns a tuple with the file obect for the child's # output. @@ -227,7 +234,10 @@ class Subprocess: try: if working_dir is not None: os.chdir(working_dir) - p = popen2.Popen4(command) + if capture_stderr: + p = popen2.Popen4(command) + else: + p = popen2.Popen3(command) p.tochild.close() self.output = p.fromchild.read() ret_code = p.wait() -- cgit v1.2.3 From 24ccb2c3e079dc0387e9a48cf6414ba982af8a45 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Tue, 17 Nov 2009 22:41:27 +0000 Subject: Blocks test binaries from inheriting GTEST_OUTPUT variable when invoked from Python test scripts (fixes issue 223). --- test/gtest_test_utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 385662ad..591cdb82 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -51,6 +51,7 @@ except: _SUBPROCESS_MODULE_AVAILABLE = False # pylint: enable-msg=C6204 +GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT' IS_WINDOWS = os.name == 'nt' IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0] @@ -267,4 +268,11 @@ def Main(): # unittest.main(). Otherwise the latter will be confused by the # --gtest_* flags. _ParseAndStripGTestFlags(sys.argv) + # The tested binaries should not be writing XML output files unless the + # script explicitly instructs them to. + # TODO(vladl@google.com): Move this into Subprocess when we implement + # passing environment into it as a parameter. + if GTEST_OUTPUT_VAR_NAME in os.environ: + del os.environ[GTEST_OUTPUT_VAR_NAME] + _test_module.main() -- cgit v1.2.3 From 2e075a7f60da95cd02a3935fda49d222a435d56a Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Tue, 24 Nov 2009 20:19:45 +0000 Subject: Refactors run_tests.py s.t. it can be shared by gmock (by Vlad Losev); Fixes a warning in gtest-tuple_test.cc on Cygwin (by Vlad Losev). --- test/gtest_test_utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 591cdb82..19b5b228 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -138,7 +138,7 @@ def GetTempDir(): return _temp_dir -def GetTestExecutablePath(executable_name): +def GetTestExecutablePath(executable_name, build_dir=None): """Returns the absolute path of the test binary given its name. The function will print a message and abort the program if the resulting file @@ -146,12 +146,15 @@ def GetTestExecutablePath(executable_name): Args: executable_name: name of the test binary that the test script runs. + build_dir: directory where to look for executables, by default + the result of GetBuildDir(). Returns: The absolute path of the test binary. """ - path = os.path.abspath(os.path.join(GetBuildDir(), executable_name)) + path = os.path.abspath(os.path.join(build_dir or GetBuildDir(), + executable_name)) if (IS_WINDOWS or IS_CYGWIN) and not path.endswith('.exe'): path += '.exe' -- cgit v1.2.3 From 6f50ccf32c31cb6e287cdfee149429ac3029bbdb Mon Sep 17 00:00:00 2001 From: vladlosev Date: Mon, 15 Feb 2010 21:31:41 +0000 Subject: Google Test's Python tests now pass on Solaris. --- test/gtest_test_utils.py | 50 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 19b5b228..e0f5973e 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -194,23 +194,28 @@ def GetExitStatus(exit_code): class Subprocess: - def __init__(self, command, working_dir=None, capture_stderr=True): + def __init__(self, command, working_dir=None, capture_stderr=True, env=None): """Changes into a specified directory, if provided, and executes a command. - Restores the old directory afterwards. Execution results are returned - via the following attributes: - terminated_by_sygnal True iff the child process has been terminated - by a signal. - signal Sygnal that terminated the child process. - exited True iff the child process exited normally. - exit_code The code with which the child proces exited. - output Child process's stdout and stderr output - combined in a string. + + Restores the old directory afterwards. Args: command: The command to run, in the form of sys.argv. working_dir: The directory to change into. capture_stderr: Determines whether to capture stderr in the output member or to discard it. + env: Dictionary with environment to pass to the subprocess. + + Returns: + An object that represents outcome of the executed process. It has the + following attributes: + terminated_by_signal True iff the child process has been terminated + by a signal. + signal Sygnal that terminated the child process. + exited True iff the child process exited normally. + exit_code The code with which the child process exited. + output Child process's stdout and stderr output + combined in a string. """ # The subprocess module is the preferrable way of running programs @@ -228,13 +233,30 @@ class Subprocess: p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=stderr, - cwd=working_dir, universal_newlines=True) + cwd=working_dir, universal_newlines=True, env=env) # communicate returns a tuple with the file obect for the child's # output. self.output = p.communicate()[0] self._return_code = p.returncode else: old_dir = os.getcwd() + + def _ReplaceEnvDict(dest, src): + # Changes made by os.environ.clear are not inheritable by child + # processes until Python 2.6. To produce inheritable changes we have + # to delete environment items with the del statement. + for key in dest: + del dest[key] + dest.update(src) + + # When 'env' is not None, backup the environment variables and replace + # them with the passed 'env'. When 'env' is None, we simply use the + # current 'os.environ' for compatibility with the subprocess.Popen + # semantics used above. + if env is not None: + old_environ = os.environ.copy() + _ReplaceEnvDict(os.environ, env) + try: if working_dir is not None: os.chdir(working_dir) @@ -247,6 +269,12 @@ class Subprocess: ret_code = p.wait() finally: os.chdir(old_dir) + + # Restore the old environment variables + # if they were replaced. + if env is not None: + _ReplaceEnvDict(os.environ, old_environ) + # Converts ret_code to match the semantics of # subprocess.Popen.returncode. if os.WIFSIGNALED(ret_code): -- cgit v1.2.3 From 3678a248d35723d5e18c7c2a78d7da5b4f5a3e57 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 13 May 2010 18:05:20 +0000 Subject: Renames test script flags. --- test/gtest_test_utils.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index e0f5973e..e7ee9d9c 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -63,8 +63,8 @@ TestCase = _test_module.TestCase # pylint: disable-msg=C6409 # Initially maps a flag to its default value. After # _ParseAndStripGTestFlags() is called, maps a flag to its actual value. -_flag_map = {'gtest_source_dir': os.path.dirname(sys.argv[0]), - 'gtest_build_dir': os.path.dirname(sys.argv[0])} +_flag_map = {'source_dir': os.path.dirname(sys.argv[0]), + 'build_dir': os.path.dirname(sys.argv[0])} _gtest_flags_are_parsed = False @@ -111,13 +111,13 @@ def GetFlag(flag): def GetSourceDir(): """Returns the absolute path of the directory where the .py files are.""" - return os.path.abspath(GetFlag('gtest_source_dir')) + return os.path.abspath(GetFlag('source_dir')) def GetBuildDir(): """Returns the absolute path of the directory where the test binaries are.""" - return os.path.abspath(GetFlag('gtest_build_dir')) + return os.path.abspath(GetFlag('build_dir')) _temp_dir = None @@ -161,7 +161,7 @@ def GetTestExecutablePath(executable_name, build_dir=None): if not os.path.exists(path): message = ( 'Unable to find the test binary. Please make sure to provide path\n' - 'to the binary via the --gtest_build_dir flag or the GTEST_BUILD_DIR\n' + 'to the binary via the --build_dir flag or the BUILD_DIR\n' 'environment variable. For convenient use, invoke this script via\n' 'mk_test.py.\n' # TODO(vladl@google.com): change mk_test.py to test.py after renaming -- cgit v1.2.3 From 6642ca8cd46cf905b45e8f71532922df4d03800d Mon Sep 17 00:00:00 2001 From: vladlosev Date: Thu, 10 Feb 2011 23:14:49 +0000 Subject: Updates an outdated error message. --- test/gtest_test_utils.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index e7ee9d9c..4e897bd3 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -162,11 +162,7 @@ def GetTestExecutablePath(executable_name, build_dir=None): message = ( 'Unable to find the test binary. Please make sure to provide path\n' 'to the binary via the --build_dir flag or the BUILD_DIR\n' - 'environment variable. For convenient use, invoke this script via\n' - 'mk_test.py.\n' - # TODO(vladl@google.com): change mk_test.py to test.py after renaming - # the file. - 'Please run mk_test.py -h for help.') + 'environment variable.') print >> sys.stderr, message sys.exit(1) -- cgit v1.2.3 From cf3f92ef93ffc35ec1efe8b3b1d65b2624d84de5 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Tue, 16 Aug 2011 00:47:22 +0000 Subject: Fixes a user reported test break (modifying a dict while iterating). --- test/gtest_test_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 4e897bd3..6dd8db4b 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -241,7 +241,7 @@ class Subprocess: # Changes made by os.environ.clear are not inheritable by child # processes until Python 2.6. To produce inheritable changes we have # to delete environment items with the del statement. - for key in dest: + for key in dest.keys(): del dest[key] dest.update(src) -- cgit v1.2.3 From c306ef2e14483dbf4f047a3e1ca3f86111b800ca Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Fri, 6 Sep 2013 22:50:25 +0000 Subject: supports a protocol for catching tests that prematurely exit --- test/gtest_test_utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 6dd8db4b..28884bdc 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -56,6 +56,21 @@ GTEST_OUTPUT_VAR_NAME = 'GTEST_OUTPUT' IS_WINDOWS = os.name == 'nt' IS_CYGWIN = os.name == 'posix' and 'CYGWIN' in os.uname()[0] +# The environment variable for specifying the path to the premature-exit file. +PREMATURE_EXIT_FILE_ENV_VAR = 'TEST_PREMATURE_EXIT_FILE' + +environ = os.environ.copy() + + +def SetEnvVar(env_var, value): + """Sets/unsets an environment variable to a given value.""" + + if value is not None: + environ[env_var] = value + elif env_var in environ: + del environ[env_var] + + # Here we expose a class from a particular module, depending on the # environment. The comment suppresses the 'Invalid variable name' lint # complaint. -- cgit v1.2.3 From 7d1051ce2b3be67d27b200d0050a7ec88c18621b Mon Sep 17 00:00:00 2001 From: kosak Date: Mon, 13 Jan 2014 22:24:15 +0000 Subject: Make Google Test build cleanly on Visual Studio 2010, 2012, 2013. Also improve an error message in gtest_test_utils.py. --- test/gtest_test_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/gtest_test_utils.py') diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py index 28884bdc..7e3cbcaf 100755 --- a/test/gtest_test_utils.py +++ b/test/gtest_test_utils.py @@ -175,9 +175,9 @@ def GetTestExecutablePath(executable_name, build_dir=None): if not os.path.exists(path): message = ( - 'Unable to find the test binary. Please make sure to provide path\n' - 'to the binary via the --build_dir flag or the BUILD_DIR\n' - 'environment variable.') + 'Unable to find the test binary "%s". Please make sure to provide\n' + 'a path to the binary via the --build_dir flag or the BUILD_DIR\n' + 'environment variable.' % path) print >> sys.stderr, message sys.exit(1) -- cgit v1.2.3