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.

This commit is contained in:
shiqian 2008-09-18 21:18:11 +00:00
parent f6b0dc0b40
commit e79c3ccb73
6 changed files with 35 additions and 22 deletions

View File

@ -78,10 +78,7 @@ def Run(command):
"""
exit_code = os.system(command)
# On Unix-like systems, the lowest 8 bits of the exit code is the
# signal number that killed the process (or 0 if it wasn't killed by
# a signal).
return (exit_code & 255) != 0
return os.WIFSIGNALED(exit_code)
# The unit test.

View File

@ -62,7 +62,7 @@ def UsesColor(term, color_env_var, color_flag):
cmd = COMMAND
if color_flag is not None:
cmd += ' --%s=%s' % (COLOR_FLAG, color_flag)
return os.system(cmd)
return gtest_test_utils.GetExitStatus(os.system(cmd))
class GTestColorTest(unittest.TestCase):

View File

@ -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."""

View File

@ -81,13 +81,7 @@ def TestExitCodeAndOutput(command):
"""Runs the given command and verifies its exit code and output."""
# Verifies that 'command' exits with code 1.
if IS_WINDOWS:
# On Windows, os.system(command) returns the exit code of 'command'.
AssertEq(1, os.system(command))
else:
# On Unix-like system, os.system(command) returns 256 times the
# exit code of 'command'.
AssertEq(256, os.system(command))
AssertEq(1, gtest_test_utils.GetExitStatus(os.system(command)))
output = GetOutput(command)
Assert('InitGoogleTest' in output)

View File

@ -103,7 +103,7 @@ class GTestXMLOutFilesTest(gtest_xml_test_utils.GTestXMLTestCase):
command = "cd %s && %s --gtest_output=xml:%s &> /dev/null" % (
tempfile.mkdtemp(), gtest_prog_path, self.output_dir_)
status = os.system(command)
self.assertEquals(0, status)
self.assertEquals(0, gtest_test_utils.GetExitStatus(status))
# TODO(wan@google.com): libtool causes the built test binary to be
# named lt-gtest_xml_outfiles_test_ instead of

View File

@ -128,7 +128,7 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
status = os.system("cd %s && %s %s=xml &> /dev/null"
% (temp_dir, gtest_prog_path,
GTEST_OUTPUT_FLAG))
self.assertEquals(0, status)
self.assertEquals(0, gtest_test_utils.GetExitStatus(status))
self.assert_(os.path.isfile(output_file))
@ -147,10 +147,12 @@ class GTestXMLOutputUnitTest(gtest_xml_test_utils.GTestXMLTestCase):
command = ("%s %s=xml:%s &> /dev/null"
% (gtest_prog_path, GTEST_OUTPUT_FLAG, xml_path))
status = os.system(command)
signal = status & 0xff
self.assertEquals(0, signal,
if os.WIFSIGNALED(status):
signal = os.WTERMSIG(status)
self.assert_(False,
"%s was killed by signal %d" % (gtest_prog_name, signal))
exit_code = status >> 8
else:
exit_code = gtest_test_utils.GetExitStatus(status)
self.assertEquals(expected_exit_code, exit_code,
"'%s' exited with code %s, which doesn't match "
"the expected exit code %s."