"""
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.
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):
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."""
"""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)
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
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))
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,
- "%s was killed by signal %d" % (gtest_prog_name, signal))
- exit_code = status >> 8
- self.assertEquals(expected_exit_code, exit_code,
- "'%s' exited with code %s, which doesn't match "
- "the expected exit code %s."
- % (command, exit_code, expected_exit_code))
+ if os.WIFSIGNALED(status):
+ signal = os.WTERMSIG(status)
+ self.assert_(False,
+ "%s was killed by signal %d" % (gtest_prog_name, signal))
+ 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."
+ % (command, exit_code, expected_exit_code))
expected = minidom.parseString(expected_xml)
actual = minidom.parse(xml_path)