diff options
Diffstat (limited to 'QMTest')
-rw-r--r-- | QMTest/TestCmd.py | 70 | ||||
-rw-r--r-- | QMTest/TestCmdTests.py | 70 | ||||
-rw-r--r-- | QMTest/TestCommon.py | 31 | ||||
-rw-r--r-- | QMTest/TestSCons.py | 28 | ||||
-rw-r--r-- | QMTest/TestSCons_time.py | 2 |
5 files changed, 75 insertions, 126 deletions
diff --git a/QMTest/TestCmd.py b/QMTest/TestCmd.py index 708fdc4..0c42ab5 100644 --- a/QMTest/TestCmd.py +++ b/QMTest/TestCmd.py @@ -654,68 +654,20 @@ else: +import subprocess + try: - import subprocess -except ImportError: - # The subprocess module doesn't exist in this version of Python, - # so we're going to cobble up something that looks just enough - # like its API for our purposes below. - import popen2 - subprocess = types.ModuleType('subprocess') - - subprocess.PIPE = 'PIPE' - subprocess.STDOUT = 'STDOUT' - subprocess.mswindows = (sys.platform == 'win32') - - class Popen(popen2.Popen3, popen2.Popen4): - universal_newlines = 1 - def __init__(self, command, **kw): - if kw.get('stderr') == 'STDOUT': - popen2.Popen4.__init__(self, command, 1) - else: - popen2.Popen3.__init__(self, command, 1) - self.stdin = self.tochild - self.stdout = self.fromchild - self.stderr = self.childerr - def communicate(self, input=None): - if input: - self.stdin.write(input) - self.stdin.close() - out = self.stdout.read() - if self.stderr is None: - err = None - else: - err = self.stderr.read() - self.stdout.close() - if self.stderr is not None: - self.stderr.close() - self.returncode = self.wait() - return (out, err) + subprocess.Popen.terminate +except AttributeError: + if sys.platform == 'win32': + import win32process + def terminate(self): + win32process.TerminateProcess(self._handle, 1) + else: def terminate(self): os.kill(self.pid, signal.SIGTERM) - def wait(self, *args, **kw): - resultcode = popen2.Popen3.wait(self, *args, **kw) - if os.WIFSIGNALED(resultcode): - return (- os.WTERMSIG(resultcode)) - elif os.WIFEXITED(resultcode): - return os.WEXITSTATUS(resultcode) - else: - return None - - subprocess.Popen = Popen -else: - try: - subprocess.Popen.terminate - except AttributeError: - if sys.platform == 'win32': - import win32process - def terminate(self): - win32process.TerminateProcess(self._handle, 1) - else: - def terminate(self): - os.kill(self.pid, signal.SIGTERM) - method = types.MethodType(terminate, None, subprocess.Popen) - setattr(subprocess.Popen, 'terminate', method) + method = types.MethodType(terminate, None, subprocess.Popen) + setattr(subprocess.Popen, 'terminate', method) diff --git a/QMTest/TestCmdTests.py b/QMTest/TestCmdTests.py index 1fe328c..1044ed1 100644 --- a/QMTest/TestCmdTests.py +++ b/QMTest/TestCmdTests.py @@ -58,68 +58,20 @@ def _clear_dict(dict, *keys): except KeyError: pass +import subprocess + try: - import subprocess -except ImportError: - # The subprocess module doesn't exist in this version of Python, - # so we're going to cobble up something that looks just enough - # like its API for our purposes below. - import popen2 - subprocess = types.ModuleType('subprocess') - - subprocess.PIPE = 'PIPE' - subprocess.STDOUT = 'STDOUT' - subprocess.mswindows = (sys.platform == 'win32') - - class Popen(popen2.Popen3, popen2.Popen4): - universal_newlines = 1 - def __init__(self, command, **kw): - if kw.get('stderr') == 'STDOUT': - popen2.Popen4.__init__(self, command, 1) - else: - popen2.Popen3.__init__(self, command, 1) - self.stdin = self.tochild - self.stdout = self.fromchild - self.stderr = self.childerr - def communicate(self, input=None): - if input: - self.stdin.write(input) - self.stdin.close() - out = self.stdout.read() - if self.stderr is None: - err = None - else: - err = self.stderr.read() - self.stdout.close() - if self.stderr is not None: - self.stderr.close() - self.returncode = self.wait() - return (out, err) + subprocess.Popen.terminate +except AttributeError: + if sys.platform == 'win32': + import win32process + def terminate(self): + win32process.TerminateProcess(self._handle, 1) + else: def terminate(self): os.kill(self.pid, signal.SIGTERM) - def wait(self, *args, **kw): - resultcode = popen2.Popen3.wait(self, *args, **kw) - if os.WIFEXITED(resultcode): - return os.WEXITSTATUS(resultcode) - elif os.WIFSIGNALED(resultcode): - return os.WTERMSIG(resultcode) - else: - return None - - subprocess.Popen = Popen -else: - try: - subprocess.Popen.terminate - except AttributeError: - if sys.platform == 'win32': - import win32process - def terminate(self): - win32process.TerminateProcess(self._handle, 1) - else: - def terminate(self): - os.kill(self.pid, signal.SIGTERM) - method = types.MethodType(terminate, None, subprocess.Popen) - setattr(subprocess.Popen, 'terminate', method) + method = types.MethodType(terminate, None, subprocess.Popen) + setattr(subprocess.Popen, 'terminate', method) class ExitError(Exception): pass diff --git a/QMTest/TestCommon.py b/QMTest/TestCommon.py index 90aaed2..6d47149 100644 --- a/QMTest/TestCommon.py +++ b/QMTest/TestCommon.py @@ -286,11 +286,17 @@ class TestCommon(TestCmd): print "Unwritable files: `%s'" % "', `".join(unwritable) self.fail_test(missing + unwritable) - def must_contain(self, file, required, mode = 'rb'): + def must_contain(self, file, required, mode = 'rb', find = None): """Ensures that the specified file contains the required text. """ file_contents = self.read(file, mode) - contains = (file_contents.find(required) != -1) + if find is None: + def find(o, l): + try: + return o.index(l) + except ValueError: + return None + contains = find(file_contents, required) if not contains: print "File `%s' does not contain required string." % file print self.banner('Required string ') @@ -317,6 +323,9 @@ class TestCommon(TestCmd): except ValueError: return None missing = [] + if is_List(output): + output = '\n'.join(output) + for line in lines: if find(output, line) is None: missing.append(line) @@ -415,9 +424,9 @@ class TestCommon(TestCmd): sys.stdout.flush() self.fail_test() - def must_contain_lines(self, lines, output, title=None): + def must_contain_lines(self, lines, output, title=None, find = None): # Deprecated; retain for backwards compatibility. - return self.must_contain_all_lines(output, lines, title) + return self.must_contain_all_lines(output, lines, title, find) def must_exist(self, *files): """Ensures that the specified file(s) must exist. An individual @@ -467,11 +476,17 @@ class TestCommon(TestCmd): self.diff(expect, file_contents, 'contents ') raise - def must_not_contain(self, file, banned, mode = 'rb'): + def must_not_contain(self, file, banned, mode = 'rb', find = None): """Ensures that the specified file doesn't contain the banned text. """ file_contents = self.read(file, mode) - contains = (file_contents.find(banned) != -1) + if find is None: + def find(o, l): + try: + return o.index(l) + except ValueError: + return None + contains = find(file_contents, banned) if contains: print "File `%s' contains banned string." % file print self.banner('Banned string ') @@ -512,8 +527,8 @@ class TestCommon(TestCmd): sys.stdout.write(output) self.fail_test() - def must_not_contain_lines(self, lines, output, title=None): - return self.must_not_contain_any_line(output, lines, title) + def must_not_contain_lines(self, lines, output, title=None, find=None): + return self.must_not_contain_any_line(output, lines, title, find) def must_not_exist(self, *files): """Ensures that the specified file(s) must not exist. diff --git a/QMTest/TestSCons.py b/QMTest/TestSCons.py index 296af67..ea18757 100644 --- a/QMTest/TestSCons.py +++ b/QMTest/TestSCons.py @@ -108,7 +108,35 @@ def re_escape(str): str = str.replace(c, '\\' + c) return str +# +# Helper functions that we use as a replacement to the default re.match +# when searching for special strings in stdout/stderr. +# +def search_re(out, l): + """ Search the regular expression 'l' in the output 'out' + and return the start index when successful. + """ + m = re.search(l, out) + if m: + return m.start() + + return None + +def search_re_in_list(out, l): + """ Search the regular expression 'l' in each line of + the given string list 'out' and return the line's index + when successful. + """ + for idx, o in enumerate(out): + m = re.search(l, o) + if m: + return idx + + return None +# +# Helpers for handling Python version numbers +# def python_version_string(): return sys.version.split()[0] diff --git a/QMTest/TestSCons_time.py b/QMTest/TestSCons_time.py index 4a759c0..abe8ccf 100644 --- a/QMTest/TestSCons_time.py +++ b/QMTest/TestSCons_time.py @@ -21,6 +21,8 @@ import sys from TestCommon import * from TestCommon import __all__ +# some of the scons_time tests may need regex-based matching: +from TestSCons import search_re, search_re_in_list __all__.extend([ 'TestSCons_time', ]) |