Patchwork [020,of,179,tests-refactor] run-tests: make linematch a static method of TTest

login
register
mail settings
Submitter Gregory Szorc
Date May 2, 2014, 6:37 p.m.
Message ID <2ccbf07cec1272ca459f.1399055857@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/4515/
State Accepted
Commit 56610da39b485f411814ae900ea376504d323c96
Headers show

Comments

Gregory Szorc - May 2, 2014, 6:37 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1397949109 25200
#      Sat Apr 19 16:11:49 2014 -0700
# Branch stable
# Node ID 2ccbf07cec1272ca459f84810323c28e53044745
# Parent  48daa5fba4cc8b681cc19f19a5f8aec69b3d7e22
run-tests: make linematch a static method of TTest

linematch only applies to t tests. It makes sense to move everything t
test related to the TTest class.

Patch

diff --git a/tests/run-tests.py b/tests/run-tests.py
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -724,32 +724,16 @@  def globmatch(el, l):
         elif c == '?':
             res += '.'
         elif c == '/' and os.altsep:
             res += '[/\\\\]'
         else:
             res += re.escape(c)
     return rematch(res, l)
 
-def linematch(el, l):
-    if el == l: # perfect match (fast)
-        return True
-    if el:
-        if el.endswith(" (esc)\n"):
-            el = el[:-7].decode('string-escape') + '\n'
-        if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
-            return True
-        if el.endswith(" (re)\n"):
-            return rematch(el[:-6], l)
-        if el.endswith(" (glob)\n"):
-            return globmatch(el[:-8], l)
-        if os.altsep and l.replace('\\', '/') == el:
-            return '+glob'
-    return False
-
 class TTest(Test):
     """A "t test" is a test backed by a .t file."""
 
     def _run(self, testtmp, replacements, env):
         f = open(self._path)
         lines = f.readlines()
         f.close()
 
@@ -912,17 +896,17 @@  class TTest(Test):
                 if not lout.endswith('\n'):
                     lout += ' (no-eol)\n'
 
                 # Find the expected output at the current position.
                 el = None
                 if expected.get(pos, None):
                     el = expected[pos].pop(0)
 
-                r = linematch(el, lout)
+                r = TTest.linematch(el, lout)
                 if isinstance(r, str):
                     if r == '+glob':
                         lout = el[:-1] + ' (glob)\n'
                         r = '' # Warn only this line.
                     elif r == '-glob':
                         lout = ''.join(el.rsplit(' (glob)', 1))
                         r = '' # Warn only this line.
                     else:
@@ -952,16 +936,33 @@  class TTest(Test):
         if pos in after:
             postout += after.pop(pos)
 
         if warnonly == 2:
             exitcode = False # Set exitcode to warned.
 
         return exitcode, postout
 
+    @staticmethod
+    def linematch(el, l):
+        if el == l: # perfect match (fast)
+            return True
+        if el:
+            if el.endswith(" (esc)\n"):
+                el = el[:-7].decode('string-escape') + '\n'
+            if el == l or os.name == 'nt' and el[:-1] + '\r\n' == l:
+                return True
+            if el.endswith(" (re)\n"):
+                return rematch(el[:-6], l)
+            if el.endswith(" (glob)\n"):
+                return globmatch(el[:-8], l)
+            if os.altsep and l.replace('\\', '/') == el:
+                return '+glob'
+        return False
+
 wifexited = getattr(os, "WIFEXITED", lambda x: False)
 def run(cmd, wd, options, replacements, env):
     """Run command in a sub-process, capturing the output (stdout and stderr).
     Return a tuple (exitcode, output).  output is None in debug mode."""
     # TODO: Use subprocess.Popen if we're running on Python 2.4
     if options.debug:
         proc = subprocess.Popen(cmd, shell=True, cwd=wd, env=env)
         ret = proc.wait()
diff --git a/tests/test-run-tests.py b/tests/test-run-tests.py
--- a/tests/test-run-tests.py
+++ b/tests/test-run-tests.py
@@ -24,17 +24,17 @@  def lm(expected, output):
         missing newline
         >>> try: lm('single backslash\n', 'single \backslash\n')
         ... except AssertionError, ex: print ex
         single backslash or unknown char
     """
     assert expected.endswith('\n') and output.endswith('\n'), 'missing newline'
     assert not re.search(r'[^ \w\\/\r\n()*?]', expected + output), \
            'single backslash or unknown char'
-    match = run_tests.linematch(expected, output)
+    match = run_tests.TTest.linematch(expected, output)
     if isinstance(match, str):
         return 'special: ' + match
     else:
         return bool(match) # do not return match object
 
 def wintests():
     r"""test matching like running on windows