summaryrefslogtreecommitdiff
path: root/test/gtest_test_utils.py
diff options
context:
space:
mode:
authorvladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2008-11-26 20:02:45 +0000
committervladlosev <vladlosev@861a406c-534a-0410-8894-cb66d6ee9925>2008-11-26 20:02:45 +0000
commit365a150aeb599922be07d5f62939d0be04f1f19f (patch)
tree16c1ac5e4b98ea73315c0de0b2a8b72ffdd5a722 /test/gtest_test_utils.py
parentfe6a9a48c2a8280439e58c2e9020268a80df89b3 (diff)
downloadgoogletest-365a150aeb599922be07d5f62939d0be04f1f19f.tar.gz
Fixed gtest_break_on_failure_unittest on Ubuntu 8.04 and Windows
git-svn-id: http://googletest.googlecode.com/svn/trunk@137 861a406c-534a-0410-8894-cb66d6ee9925
Diffstat (limited to 'test/gtest_test_utils.py')
-rwxr-xr-xtest/gtest_test_utils.py87
1 files changed, 65 insertions, 22 deletions
diff --git a/test/gtest_test_utils.py b/test/gtest_test_utils.py
index a3f0138..8ee99c0 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():