From e79c3ccb73d68543e8ad98d69179dee74abff7ab Mon Sep 17 00:00:00 2001 From: shiqian Date: Thu, 18 Sep 2008 21:18:11 +0000 Subject: [PATCH] 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_break_on_failure_unittest.py | 5 +---- test/gtest_color_test.py | 2 +- test/gtest_test_utils.py | 20 ++++++++++++++++++++ test/gtest_uninitialized_test.py | 8 +------- test/gtest_xml_outfiles_test.py | 2 +- test/gtest_xml_output_unittest.py | 20 +++++++++++--------- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/test/gtest_break_on_failure_unittest.py b/test/gtest_break_on_failure_unittest.py index 674ef11d..88716c9c 100755 --- a/test/gtest_break_on_failure_unittest.py +++ b/test/gtest_break_on_failure_unittest.py @@ -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. diff --git a/test/gtest_color_test.py b/test/gtest_color_test.py index 71891768..5260a899 100755 --- a/test/gtest_color_test.py +++ b/test/gtest_color_test.py @@ -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): 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.""" diff --git a/test/gtest_uninitialized_test.py b/test/gtest_uninitialized_test.py index d553bbf9..037daa8f 100755 --- a/test/gtest_uninitialized_test.py +++ b/test/gtest_uninitialized_test.py @@ -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) diff --git a/test/gtest_xml_outfiles_test.py b/test/gtest_xml_outfiles_test.py index df0b95bc..c76e1f78 100755 --- a/test/gtest_xml_outfiles_test.py +++ b/test/gtest_xml_outfiles_test.py @@ -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 diff --git a/test/gtest_xml_output_unittest.py b/test/gtest_xml_output_unittest.py index af021a9f..013e7397 100755 --- a/test/gtest_xml_output_unittest.py +++ b/test/gtest_xml_output_unittest.py @@ -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,14 +147,16 @@ 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, - "%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)